Home Backend Development PHP Tutorial Three ways to directly read database information_PHP tutorial

Three ways to directly read database information_PHP tutorial

Jul 13, 2016 pm 05:36 PM
localhost url for code information Function address database method yes of read

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*/
? >

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486621.htmlTechArticleThe function of this code is: 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...
on the mysql server
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

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) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

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 set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

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) 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) May 07, 2024 pm 05:55 PM

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 Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Jun 12, 2024 pm 08:38 PM

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 Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

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 Create Agent in one sentence! Robin Li: The era is coming when everyone is a developer Apr 17, 2024 pm 02:28 PM

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 What is GateToken(GT) currency? Introduction to GT coin functions and token economics Jul 15, 2024 pm 04:36 PM

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

See all articles