Use DatabaseLet's use MySql. database. To use a database, the first thing is to connect
to the database server:
$host = "server_name";
$user = "database_user";
$password = "password";
$dbn = "database_name";
mysql_connect($host, $user, $password) or
die ("Cannot connect to the database server"); mysql_select_db($dbn) or
die ("Cannot connect to the database");
After opne the database, we use the mysql_query funcation and
a SQL query:
$q_sql = mysql_query("select * from members");
while ($rs=mysql_fetch_row($q_sql)) {
...
}
Usually, this function is used with mysql_fetch_row or mysql_fetch_array
to get the result of the SQL query row after row in an array:
$q_sql = mysql_query("select * from members");
while ($rs=mysql_fetch_row($q_sql)) {
$lastName = $rs[3];
$firstName = $rs[4];
echo $lastName . ", " . $firstName;
}
Or:
$q_sql = mysql_query("select * from members");
while ($rs=mysql_fetch_array($q_sql)) {
$lastName = $rs["lastName"];
$firstName = $rs["firstName"];
echo $lastName . ", " . $firstName;
}
When we have finished to use the database, we should close the connection
with the server:
mysql_close();
To insert into a table, we use a statement something like below:
$query = "INSERT INTO user_comments(lastName, firstName, email, comments, date)";
$query .= " VALUES ("$lastName", \"$firstName\", \"$email\", \"$comments\", $date)";
mysql_query($query) or die ("cannot insert the message");
name |
description |
mysql_connect |
used to connected to a MySQL server |
mysql_select_db |
used to connect to a database |
mysql_query |
performs the SQL query on a given database |
mysql_close |
close the connection to a database server |
mysql_fetch_row |
gets the result of the query row after row in an enumerated
array |
mysql_fetch_array |
gets the result of the query row after row in an associative
array |
mysql_num_rows |
returns the number of rows of the result |
mysql_data_seek |
moves the internal pointer of a MySQL result to a given
position |
|