In the course of learning "ten days to learn PHP", when you see the problem of database connection, due to the lack of knowledge, encountered difficulties. So find a friend to help write a PHP applet, posted here, hope for everyone can help. I wrote the notes myself.
The function of this piece of code is:
Connect to a MySQL server with a URL address of localhost and port 3306. The MySQL server account number is "root" and the password is "9999". There is a database on the MySQL server OK, there is a table ABC in the database. Table ABC is a total of two columns, the column name is "id" and "name", the ABC of all the data read out.
?
$DBH = @mysql_connect ("localhost:3306", "root", "9999");
/* Define variable DBH, the mysql_connect () function means to connect to the MySQL database, "@" means to block the error * *
if (! $dbh) {die ("error");}
The/* Die () function means that the string in parentheses is sent to the browser and interrupts the PHP program (Script). The arguments in parentheses are for the string to be sent out. */
@mysql_select_db ("OK", $DBH);
/* Select a database in the MySQL server, this is the selected database named OK * *
$q = "SELECT * FROM ABC";
/* Define variable Q, "SELECT * from ABC" is an SQL statement that means to read data from the table ABC * *
?>
<br/>
<!--========= Method a =========-->
<br/>
?
$rs = mysql_query ($q, $DBH);
/* Define variable RS, function mysql_query () means: Send out query word falsify MySQL do related processing or execution. Since PHP is executed from right to left, the value of RS is the value returned after the server runs the mysql_query () function.
if (! $rs) {die ("Valid result!");}
echo "<table>";
echo "<tr><td>ID</td><td>Name</td></tr>";
while ($row = Mysql_fetch_row ($rs)) echo "<tr><td> $row [0]</td><td> $row [1]</td></tr > ";
/* Defines the quantity (array) row and uses the while loop to write out the data one by one.
function mysql_fetch_row () means to $rs a single column of query results into an array variable.
$row [0] and the position of the $row [1] can be changed/
echo "</table>";
?>
<br/>
<!--========= Method two =========-->
<br/>
?
$rs = mysql_query ($q, $DBH);
while ($row = Mysql_fetch_object ($rs)) echo "$row->id $row->name <br/>";
/* ID and name can be changed to position * *
?>
<br/>
<!--========= Method three =========-->
<br/>
?
$rs = mysql_query ($q, $DBH);
while ($row = Mysql_fetch_array ($rs)) echo "$row [id] $row [name] <br/>";
/* ID and name can be changed to position * *
?>
<!--========= Method three fastest =========-->
?
@mysql_close ($DBH);
/* Close the connection to the MySQL database * *
?>