PHP connection MySQL

Source: Internet
Author: User

Traditional writing:

<?php
Echo"This is a test</br>";
Echo"ASDFASDFADSF";
$mysql _server_name="localhost";//Database server name
$mysql _username="Root";//Connect to database user name
$mysql _password="??????";//Connect Database Password
$mysql _database="??????";//the name of the database

//connecting to a database
$conn =mysql_connect ($mysql _server_name, $mysql _username,
$mysql _password);

//SQL statement that extracts information from a table
$strsql ="SELECT * from ' db_table '";
//Execute SQL query
$result =mysql_db_query ($mysql _database, $strsql, $conn);
//Get query Results
$row =mysql_fetch_row ($result);


Echo'<font face= "Verdana" >';
Echo'<table border= "1" cellpadding= "1" cellspacing= "2" >';

//Show Field names
Echo"</b><tr></b>";
for($i =0; $i <mysql_num_fields ($result); $i + +)
{
Echo'<td bgcolor= "#000F00" ><b>'.
Mysql_field_name ($result, $i);
Echo"</b></td></b>";
}
Echo"</tr></b>";
//Navigate to the first record
Mysql_data_seek ($result,0);
//Loop Out Records
while($row =mysql_fetch_row ($result))
{
Echo"<tr></b>";
for($i =0; $i <mysql_num_fields ($result); $i + +)
{
Echo'<td bgcolor= "#00FF00" >';
echo $row [$i];
Echo'</td>';
}
Echo"</tr></b>";
}

Echo"</table></b>";
Echo"</font>";
//Freeing Resources
Mysql_free_result ($result);
//Close Connection
Mysql_close ($conn);
?>



Shorthand:

<?php
$conn =mysql_connect ("localhost","Root","Password");
$result =mysql_db_query ("Database","SELECT * from ' db_table '", $conn);
//Get query Results
$row =mysql_fetch_row ($result);


Echo'<font face= "Verdana" >';
Echo'<table border= "1" cellpadding= "1" cellspacing= "2" >';

//Show Field names
Echo"</b><tr></b>";
for($i =0; $i <mysql_num_fields ($result); $i + +)
{
Echo'<td bgcolor= "#000F00" ><b>'.
Mysql_field_name ($result, $i);
Echo"</b></td></b>";
}
Echo"</tr></b>";
//Navigate to the first record
Mysql_data_seek ($result,0);
//Loop Out Records
while($row =mysql_fetch_row ($result))
{
Echo"<tr></b>";
for($i =0; $i <mysql_num_fields ($result); $i + +)
{
Echo'<td bgcolor= "#00FF00" >';
echo $row [$i];
Echo'</td>';
}
Echo"</tr></b>";
}

Echo"</table></b>";
Echo"</font>";
//Freeing Resources
Mysql_free_result ($result);
//Close Connection
Mysql_close ($conn);
?>

Echo came out of the main table

Reproduced in http://www.cnblogs.com/JamyWong/archive/2009/06/03/1495499.html

PHP connection MySQL

Related Article

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.