php reads the mysql database three ways
- 2020-05-09 19:26:29
- OfStack
<?
$dbh = @mysql_connect("localhost:3306","root","000000");
/* Define variables dbh , mysql_connect() The function means join mysql The database , "@" Means to screen error reporting */
if(!$dbh){die("error");}
/* die() The () function sends the string in parentheses to the browser and interrupts it PHP The program (Script) . The argument in parentheses is the string to send. */
@mysql_select_db("ok", $dbh);
/* choose mysql In the server 1 A database , The database name chosen here is ok */
$q = "SELECT * FROM abc";
/* Define variables q, "SELECT * FROM abc" is 1 a SQL statements , That means read the table abc The data in the */
?>
< ! --========= == method 1 ========= ===-- >
<?
$rs = mysql_query($q, $dbh);
/* Define variables rs , function mysql_query() mean : Send out query Word collusion MySQL Do the relevant processing or execution . Due to the php It is executed from right to left
, so ,rs Is the value of the server run mysql_query() The value returned after the 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>";
/* Define the quantity ( An array of )row, And use the while cycle , The data 11 Write out .
function mysql_fetch_row() mean : Query the results $rs Single column into array variable .
$row[0] and $row[1] The position can be changed */
echo "</table>";
?>
< ! --========= = method 2 ========= ===-- >
<?
$rs = mysql_query($q, $dbh);
while($row = mysql_fetch_object($rs)) echo "$row->id $row->name <br />";
/* id and name You can switch places */
?>
< ! --========= = method 3 ========= ===-- >
<?
$rs = mysql_query($q, $dbh);
while($row = mysql_fetch_array($rs)) echo "$row[id] $row[name] <br />";
/* id and name You can switch places */
?>
<!--========= methods 3 The fastest =========-->
<?
@mysql_close($dbh);
/* Close to the mysql Database connection */
?>