It is time to finally connect to a database and use PHP to access data as well as write new data to the database. Before we can do this, there are some installations we will need to complete.
PHP has the ability to connect to many types of Database Management Systems. Most popular are MySQL, PostGreSQL, and SQL Server.
This course is using PostGreSQL as it is a more powerful tool than MySQL (closer to Oracle/SQL Server) The Good news: a very limited number of PHP functions are required
Note: all of the functions shown have to exist in PHP <?php ?≳ tags
In order to access information from any database, you must always perform the following steps:
To complete all of this for this course we only need to use the following PHP provided functions:
pg_connect()
pg_query()
pg_num_rows()
pg_fetch_result()
Performs the all important steps of connecting to a database and returns a PostGreSQL connection object that is used by subsequent functions
$conn = pg_connect("host=127.0.0.1 dbname=DB_NAME user=USERID password=PASSWORD");
127.0.0.1
refers to the computer running the PHP page.
dbname
is the database you are connecting to (in our case userid_db)user
is the postGreSQL user you are connecting as password is the user's PostGreSQL password$conn
is a connection object that will be used below (does NOT have to be called connection)
Used to run CRUD SQL statements against your database. This includes INSERT, SELECT, UPDATE and DELETE. This function will return a result set if a SELECT statement is provided.
$sql = “SELECT title, year FROM movies”;
$results = pg_query($conn, $sql);
// $conn was created with the pg_connect()
$results
is a result set that contains the title and
year of any records in the movies table
Used to determine if any records were returned in a result set and can be used to decide if any further processing required.
$records = pg_num_rows($results);
//$results was created by the pg_query()
$records
is the total number of records in the result, will usually be 0 or above (integer values as it is a count)Used to retrieve any information contained in a result set from a SELECT statement and requires a result set argument, and a reference to a record and a column
$title = pg_fetch_result($results, 0, 0);
//$results was created by the pg_query()
$year = pg_fetch_result($results, 0, 1);
This is putting the first record's (0th) first column
(0th) into a variable named $title
, and the first
record's second column (1st) into a variable
named $year
Another syntax that you can use is the database table's field name versus the index of the field in the SQL SELECT statement.
$title = pg_fetch_result($results, 0, “title”);
//$results was created by the pg_query()
$year = pg_fetch_result($results, 0, “year”);
This is putting the first record's (0th) “title” column into a variable named $title, and the first record's “year” column into a variable named $year
This syntax sometimes makes it easier to see what the code is doing (less cryptic/confusing than a zero'ed index “array”)
Usually (though not always) a SQL SELECT statement will return multiple records. The following gives a way to process them all:
$conn = pg_connect("host=127.0.0.1 dbname=DB_NAME user=USERID password=PASSWORD");
$sql = "SELECT title, year FROM movies";
$results = pg_query($conn, $sql);
$records = pg_num_rows($results);
for ($i = 0; $i < $records; $i++){
$title = pg_fetch_result($results, $i, “title”);
$year = pg_fetch_result($results, $i, “year”);
echo "<p>The movie " . $title . " was released in " . $year ."</p>”;
}
The for loop starts at the 0th record and goes to the (n1) th record displaying each colum