There are two ways to manipulate a database using PHP
- Using the Mysql_xxxx () method
- In this way, you need to remove the Extension=php_mysql.dll from the php.ini first.
- Using PDO
- To use this test, you need to remove the Extension=php_pdo_mysql.dll from the php.ini.
The following demo uses the first method:
<!doctype html>PHP$conn=mysql_connect("localhost", "root", "Zxbbugu"); if(!$conn) { die("Could Not Connect:".)Mysql_error()); } mysql_select_db("Test",$conn); //mysql_query ("SET NAMES UTF8"); $result=mysql_query("INSERT into MyTable (Headline, create_time) VALUES (' China ', '".)Date("Y-m-d h:i:s"). "‘);"); if($result< 1) { Echo"Insert error!"; } $query=mysql_query("SELECT * from mytable LIMIT 0;"); while($row=Mysql_fetch_array($query,Mysql_both)) { Echo"<p>",$row["id"], "-",$row["Headline"], "-",$row["Create_time"], "</p>"; } Mysql_close(); ?> </body>Here's how to use PDO:
<!doctype html>PHP$pdo=NewPDO ("Mysql:host=localhost;dbname=test", "root", "Zxbbugu"); $stmt=$pdo->prepare (' SELECT * FROM mytable WHERE ID =: ID '); $stmt->execute (Array(": id" =>1)); foreach($stmt as $row) { Echo $row["Headline"]; } $result=$pdo-exec("INSERT into MyTable (Headline, create_time) VALUES (' China ', '".)Date("Y-m-d h:i:s"). "‘);"); if($result) { $str=sprintf("Add data Completed, lastupdateid=%s",$pdo-Lastinsertid ()); Echo $str; } $rs=$pdo->query ("SELECT * FROM MyTable"); while($row=$rs-Fetch ()) { Echo"<p>",$row["id"], "-",$row["Headline"], "-",$row["Create_time"], "</p>"; } ?> </body>
It is recommended to use the PDO approach, which reduces SQL injection security issues. (PHP5 above is recommended to do database operation using PDO mode)
[PHP]-MySQL database operation