PHP Operating Database PDO

Source: Internet
Author: User
Tags dsn getmessage

PHP Operations Database
Load Database Driver

Visit phpinfo.php to see if the database driver has been loaded, for example, the following display has not been loaded into MySQL database driver.


Locate the php.ini configuration file on the C drive to load the MySQL driver, for example, to remove the semicolon.



Connecting to a database

<?php

connecting to a database

$DSN = "mysql:dbname=test;host=127.0.0.1";// Data Source name

$user = "root";//username

$password = "715632";// Password

try{

$pdoConn =new PDO ($dsn, $user, $password);

echo " database connection succeeded ";

}catch (Pdoexception $e) {

echo " database connection failed ". $e->getmessage ();

Exit

}

manipulating databases

<?php

connecting to a database

$DSN = "mysql:dbname=test;host=127.0.0.1";// Data Source name

$user = "root";//username

$password = "715632";// Password

try{

$pdoConn =new PDO ($dsn, $user, $password);

echo " database connection succeeded ". " <br/> ";

}catch (Pdoexception $e) {

echo " database connection failed ". $e->getmessage ();

Exit

}

try{

Insert Operation

/* $sql = "INSERT into Contacts (Name,telno,email) VALUES (?,?,?)";

$ptmt = $pdoConn->prepare ($sql);

$name = "Liujun";

$telno = "347535420";

$email = "[email protected]";

$ptmt->bindparam (1, $name);

$ptmt->bindparam (2, $telno);

$ptmt->bindparam (3, $email);

$result = $ptmt->execute ();

echo " affects line count:". $result;

*/

Query Operations

$sql = "SELECT * from Contacts";

$ptmt = $pdoConn->prepare ($sql);

$result = $ptmt->execute ();

if ($result ==1) {// contains result set

while ($list = $ptmt->fetch ()) {

echo $list [' name ']. " -----". $list [' Telno ']." ----". $list [' email ']." <br/> ";

}

}

}catch (Pdoexcepton $e) {

echo " database operation failed ";

Exit

}

Instance code:xsphp/demo.php

PHP Operating Database PDO

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.