The function of this code is
to connect to a mysql server with the url address localhost and port 3306. The account number of the mysql server is "root" and the password is "9999". There is a database ok on the mysql server, and there is a table abc in the database. Table abc has two columns in total, and the column names are "id" and "name". Read out all the data in abc.
The following is the quoted content:
<?
$dbh = @mysql_connect("localhost:3306","root","9999");
/* Define the variable dbh, the mysql_connect() function means to connect to the mysql database, "@" means to block errors*/
if(!$dbh){die("error");}
/* The die() function means to send the string in the brackets to the browser and interrupt the PHP program (Script). The parameters in brackets are the string to be sent. */
@mysql_select_db("ok", $dbh);
/* Select a database in the mysql server. The database selected here is named ok */
$q = "SELECT * FROM abc";Web Page Teaching Network http://www.webjx.com
/* Define variable q, "SELECT * FROM abc" is a SQL statement, which means reading data in table abc*/
?>
<br />
<!--========== Method 1 =========-->
<br />
<?
$rs = mysql_query($q, $dbh);
/* Define the variable rs. The meaning of the function mysql_query() is to send the query string for MySQL to perform related processing or execution. Since PHP is executed from right to left, the value of rs is returned after the server runs the mysql_query() function. value*/
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>";
/* Define the quantitative variable (array) row, and use the while loop to write out the data one by one.
The function mysql_fetch_row() means: split the query result $rs single column into array variables.
The positions of $row[0] and $row[1] can be changed*/
echo "</table>";
?>
<br />
<!--========== Method 2 =========-->
<br />
<?
$rs = mysql_query($q, $dbh);
while($row = mysql_fetch_object($rs)) echo "$row->id $row->name <br />";
/* ID and name can change positions*/
?>
<br />
<!--========== Method 3 =========-->
<br />
<?
$rs = mysql_query($q, $dbh);
while($row = mysql_fetch_array($rs)) echo "$row[id] $row[name] <br />";
/* ID and name can change positions*/
?>
<!--========== Method 3 is the fastest =========-->
<?
@mysql_close($dbh);
/* Close the connection to the mysql database */
?>