1.连接 MYSQL 服务器的函数 mysql_connect();
<?php
$con = mysql_connect("localhost","root","123456");
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}
?>
2.选择数据库
<?php
mysql_select_db("david", $con);
?>
3.执行SQL语句,create,insert into,delete
<?php
//$sql = "CREATE TABLE Persons (FirstName varchar(15),LastName varchar(15),Age int)";
$sql="insert into Persons(FirstName,LastName,Age)VALUES(‘chen‘,‘a‘,33)";
mysql_query($sql,$con);
?>
4.得到记录集
mysql_query,mysql_fetch_array,
<?php
$sql="select * from Persons";
$result=mysql_query($sql,$con);
echo ‘共‘.mysql_num_rows($result).‘条记录‘;
while ($row = mysql_fetch_array($result))
{
//echo $row[‘FirstName‘] . " " . $row[‘LastName‘];
//echo "<br />";
printf ("ID: %s Name: %s", $row[0], $row[1]);
}
?>
时间: 2024-10-27 08:18:22