PHP Learning Notes (ii) PHP connection with MySQL and send SQL query with PHP

Source: Internet
Author: User
Tags getmessage php language

<?php//Establish Connection object try  {  $pdo  = new pdo (' mysql:host=localhost;dbname=test ', ' TestUser ', ' Pass ');  $pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception);  //setattribute   pdo object Methods, PDO:: Indicates that these variables are part of the PDO we use, not the PHP language itself built-in variables. You want to set the PDO property (Pdo::attr_errmode) of the control error mode to the pattern that throws the exception (Pdo::errmode_exception).   $pdo->exec (' set names  ' utf8 ');  //the character code of the configuration database connection.  } catch  (pdoexception  $e)  {      $output  =  ' Database connection Failed! '. $e->getmessage ();//Get a detailed error message from the MySQL server  include  ' output.html.php ';      exit ();     }       $output  =  ' database connection was successful! ';include  ' output.html.php ';//Build Form try{     $sql  =  ' create table  joke  (           id int not null  auto_increment primary key,    joketext text,    jokedate date not null     ) Default character set utf8 engine=innodb '; $pdo->exec ($sql);  // Pass SQL query to Object}catch (pdoexception  $e) {$output  = ' table build failed:  '. $e->getmessage (); include  ' Output.html.php '; exit ();} $output  =  ' form success ';include  ' output.html.php ';//update content try{$sql  =  ' update joke  Set jokedate= "2012-04-01"  WHERE joketext LIKE  "%chicken%" ';    $ad  =  $pdo->exec ($sql);  //Gets the value returned, for delete,update and insert (which they want to modify to store the outgoing data), EXEC returns the number of rows in the affected table. }catch  (pdoexception  $e) {     $output  =  ' content update error:  '. $e GetMessage ();    include  ' output.html.php ';     exit ();}   $output  =  "Content update is successful,   $ad   line  " is updated;  //values have variables in double quotes.     include  ' output.html.php ';? >


This article is from the "Cocoa sauce data collection Room" blog, reproduced please contact the author!

PHP Learning Notes (ii) PHP connection with MySQL and send SQL query with PHP

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.