An example of reading database data using the PHP language

Source: Internet
Author: User
Tags define php language learn php connect mysql database

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

?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.