Three ways to directly read database information_PHP tutorial
Jul 13, 2016 pm 05:36 PM
The function of this code is:
Connect to a mysql server with 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. 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 error reports */
if(!$dbh){die("error");}
/* The die() function means to send the string in 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 name selected here is ok */
$q = "SELECT * FROM abc" ;Webpage Teaching Network http://www.webjx.com
/* Define variable q, "SELECT * FROM abc" is a SQL statement, which means to read the data in table abc*/
?>
<br />
<!--========= Method 1 =========-->
<br />
<?
$rs = mysql_query($q, $dbh);
/* Define the variable rs, the function mysql_query() means: send the query string for MySQL to do related processing or execution. Since PHP is executed from right to left, the value of rs is the value returned by the server after running 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>";
/* Define the quantitative variable (array) row, and use a while loop to write out the data one by one.
The function mysql_fetch_row() means: split the query result $rs into an array variable in a single column.
$row The positions of [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 be replaced Location*/
?>
<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 be changed */
?>
<!--========= Method three is the fastest =========-->
<?
@mysql_close($dbh);
/* Close the connection to the mysql database*/
? >

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts)

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

How to set font size on mobile phone (easily adjust font size on mobile phone)

How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors)

Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing

Detailed tutorial on establishing a database connection using MySQLi in PHP

Create Agent in one sentence! Robin Li: The era is coming when everyone is a developer

What is GateToken(GT) currency? Introduction to GT coin functions and token economics
