Home > Backend Development > PHP Tutorial > Rough steps for PHP to access a database interaction_PHP tutorial

Rough steps for PHP to access a database interaction_PHP tutorial

WBOY
Release: 2016-07-13 17:45:29
Original
1090 people have browsed it

The general steps for PHP to access a database interaction:
1. Open the database connection
mysql_pconnect(host, username, password);
For example:
$useName="root";
$passwd="yb";
$Sserver="127.0.0.1:3306";
$db_link=@mysql_connect($Sserver,$useName,$passwd);
2. Select active database
$db = mysql_select_db("$dbname")
For example: $bresult = mysql_select_db(myphp,$db_link);
3Execute query operation:
mysql_query($sql)
For example: mysql_query("select *from student",$db_link);
4 Get the number drama from the results:
$row = mysql_fetch_array($rst) or die("No more records!"); //Fetch a record
For example: $row=mysql_fetch_row($result)
5 Release memory resources
mysql_free_result($rst) or die("Cannot release result resource!");
For example: mysql_free_result($result)
6. Close the database
@mysql_close($db_link);
Complete example of the entire process:
$useName="root";
$passwd="yb";
$Sserver="127.0.0.1:3306";
$db_link=@mysql_connect($Sserver,$useName,$passwd);
$bresult = mysql_select_db(myphp,$db_link);
$result=mysql_query("select *from student",$db_link);
$i=0;
while($row=mysql_fetch_row($result))
{

print_r("id=".$row[0]."$nbsp;name=".$row[1]."
");
$i++;
}
mysql_data_seek($result,0);
mysql_free_result($result);
print("$i");
@mysql_close($db_link);
?>

Author "ITeamsky-Yang Bo's Technology Space"

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478656.htmlTechArticleThe general steps for PHP to access a database interaction: 1. Open the database connection mysql_pconnect (host, username, password); For example: $useName=root; $passwd=yb; $Sserver=127.0.0.1:3306; $db_...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template