[PHP]-MySQL database operation

Source: Internet
Author: User

There are two ways to manipulate a database using PHP

    1. Using the Mysql_xxxx () method
      1. In this way, you need to remove the Extension=php_mysql.dll from the php.ini first.
    2. Using PDO
      1. 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

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.