PHP PDO notation connection mysql;
Writing one:
$db = "Mysql:host=localhost;dbname=sql"; //connection data, address localhost; database name SQL;
$username = "root"; Database login account;
$password = "root"; Database login password;
try{
$pdo =new PDO ($db, $username, $password); Connection database Assignment $pdo;
}catch (Pdoexception $e) {
echo "PDO connection Failed". $e->getmessage ();
}
Two:
$db =new PDO ("Mysql:host=localhost;dbname=sql", "root", "root"); Connection data, address localhost; database name SQL; database login account password; connection Assignment $db
PDO additions and deletions to check and change
Case one:
<?php
$DBH = new PDO (' Mysql:host=localhost;dbname=access_control ', ' root ', ');
$DBH->setattribute (Pdo::attr_errmode, pdo::errmode_exception);
$DBH->exec (' Set names UTF8 ');
/ * Add * /
$sql = "INSERT into ' user ' SET ' login ' =:login and ' password ' =:p assword";
$sql = "INSERT into ' user ' (' login ', ' password ') VALUES (: Login,:p assword)"; $stmt = $dbh->prepare ($sql); $stmt->execute (': Login ' = ' kevin2 ', ':p assword ' = ') ');
echo $DBH->lastinsertid ();
/ * Modify * /
$sql = "UPDATE ' user ' SET ' password ' =:p assword WHERE ' user_id ' =:userid";
$stmt = $dbh->prepare ($sql);
$stmt->execute (Array (': userId ' = ' 7 ', ':p assword ' = ' 4607e782c4d86fd5364d7e4508bb10d9 '));
echo $stmt->rowcount ();
/ * Delete * /
$sql = "DELETE from ' user ' WHERE ' login ' like ' kevin_ '"; kevin%
$stmt = $dbh->prepare ($sql);
$stmt->execute ();
echo $stmt->rowcount ();
/ * Query * /
$login = ' kevin% ';
$sql = "SELECT * from ' user ' WHERE ' login ' Like:login";
$stmt = $dbh->prepare ($sql);
$stmt->execute (Array (': Login ' = $login));
while ($row = $stmt->fetch (PDO::FETCH_ASSOC)) {
Print_r ($row);
}
Print_r ($stmt->fetchall (PDO::FETCH_ASSOC));
?>
Case TWO:
<?php
header(
"content-type:text/html;charset=utf-8"
);
$dsn
=
"mysql:dbname=test;host=localhost"
;
$db_user
=
‘root‘
;
$db_pass
=
‘admin123‘
;
try
{
$pdo
=
new
PDO(
$dsn
,
$db_user
,
$db_pass
);
}
catch
(PDOException
$e
){
echo
‘数据库连接失败‘
.
$e
->getMessage();
}
//新增
$sql
=
"insert into test (id,user) values (1,‘phpthinking‘)"
;
$res
=
$pdo
->
exec
(
$sql
);
echo
‘影响行数:‘
.
$res
;
//修改
$sql
=
"update test set user=‘phpthinking‘ where id=1"
;
$res
=
$pdo
->
exec
(
$sql
);
echo
‘影响行数:‘
.
$res
;
//查询
$sql
=
"select * from test"
;
$res
=
$pdo
->query(
$sql
);
foreach
(
$res
as
$row
){
echo
$row
[
‘user‘
].
‘<br/>‘
;
}
//删除
$sql
=
"delete from test where id=1"
;
$res
=
$pdo
->
exec
(
$sql
);
echo
‘影响行数:‘
.
$res
;
?>
PDO Statement Description
<?php
$pdo =new PDO ("mysql:dbname=test;host=127.0.0.1;port=3306", "root", "PHP");
$pdo =new PDO ("Mysql:dbname= database; host=127.0.0.1;port=3306", "root", "PHP", Array (pdo::attr_persistent=>true));
$pdo->setattribute (pdo::attr_persistent,true);//Set the database connection as a persistent connection
$pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception);//Set throw error
$pdo->setattribute (pdo::attr_oracle_nulls,true);//Set NULL to convert to SQL when the string is empty
$pdo->setattribute (Pdo::attr_case,pdo::case_upper);//Case Conversion of table field characters or use of column information as-is
$pdo->query ("Set NAMES UTF8");//Set Database encoding
$PDO->query (SQL statement);//Returns Pdostatement object, typically used for select
$PDO->exec (SQL statement);//Returns the number of rows affected, typically for insert|update|delete
$SM = $pdo->query ();
$SM->rowcount ()//return record Count
$pdo =null;//Release Resources
while ($data = $sm->fetch ()) {Print_r ($data);} Only one piece of data is returned
$SM->setfetchmode (PDO::FETCH_ASSOC);//returns only the associated index
$data = $sm->fetchall ();//Return all data
$SM = $pdo->prepare (SQL statement);
$SM->execute ();
$data = $sm->fetchcolumn ();//commonly used for count statistics
Distributing a column to a variable
$SM->bindcolumn (numbers, variables);
$sm->bindcolumn (field name, variable);
while ($data = $sm->fetch (pdo::fetch_bound)) {}
Substitution variables
$SM = $pdo->prepare (": Placeholder variable");
$sm->bindparam (": Placeholder variable", value, PDO::P aram_int);
$sm->bindparam (": Placeholder variable", value, PDO::P aram_str,12);
$SM->execute ();
Replace question mark placeholder
$SM = $pdo->prepare ("?");
$sm->bindvalue (1, Value, PDO::P aram_int);//1th question mark
$sm->bindvalue (2, Value, PDO::P aram_str,12);//2nd question mark
$SM->execute ();
Method
Pdo::query ()//processes an SQL statement and returns a Pdostatement object
Pdo::lastinsertid ()//Gets the primary key value of the last piece of data inserted into the table
PDO::p repare ()//SQL statement to prepare for execution
Pdo::exec ()//processes an SQL statement and returns the number of rows affected
pdo::begintransaction//start a transaction and indicate the rollback start point
pdo::commit//commit a transaction and execute the SQL statement
pdo::__construct//constructor function
pdo::errorcode//Getting the error code
pdo::errorinfo//getting error messages
pdo::getattribute//get the properties of a database connection object
pdo::getavailabledrivers//get a valid PDO drive name
Pdo::intransaction
pdo::quote//Add quotation marks to a string in an SQL statement
pdo::rollback//rolling back a transaction
pdo::setattribute//setting a property for a database connection object
pdostatement::bindcolumn//Distributing columns to variables
pdostatement::bindparam//substitution variables
pdostatement::bindvalue//Replace question mark placeholders
pdostatement::closecursor//Close Cursor
pdostatement::columncount//Number of fields
Pdostatement::d ebugdumpparams
pdostatement::errorcode//Getting the error code
pdostatement::errorinfo//getting error messages
pdostatement::execute//Executing statements
pdostatement::fetch//will only return one piece of data
pdostatement::fetchall//Return all data
pdostatement::fetchcolumn//is typically used for count statistics
Pdostatement::fetchobject
Pdostatement::getattribute
Pdostatement::getcolumnmeta
Pdostatement::nextrowset
pdostatement::rowcount//Record Count
Pdostatement::setattribute
Pdostatement::setfetchmode
Constant
pdo::attr_autocommit//whether the auto-submit feature is turned on True|false
pdo::attr_prefetch//set the size of the data that the application obtains in advance [Kbytes]
pdo::attr_timeout//wait time [seconds] before setting time-out
pdo::attr_server_info//contains server information specific to the database
pdo::attr_server_version//contains information about the database server version number
pdo::attr_client_version//contains information about the database client version number
pdo::attr_connection_status//wait time [seconds] before setting time-out
pdo::case_lower//Mandatory column name is lowercase
pdo::case_upper//Force column name to uppercase
pdo::case_natural//column names in the original way
pdo::fetch_assoc//Associative Array Form
pdo::fetch_num//Numeric index Array form
pdo::fetch_both//both array forms have
pdo::fetch_obj//in the form of objects
?>
Comments:
When an exception is triggered, it usually occurs when an error exception handling module similar to other languages is added to the PHP5. Exceptions generated in PHP code can be thrown by a throw statement and captured by a catch statement. The code that requires exception handling must be placed inside a try code block to catch possible exceptions. Each try must have at least one catch corresponding to it.
Multiple catches can be used to catch exceptions from different classes, and when a try code block no longer throws an exception or cannot find a catch that matches the thrown exception, the PHP code resumes execution after jumping to the last catch. Of course, PHP allows the exception to be thrown in the catch code block again, and when an exception is thrown, the code behind it (the code block where the exception was thrown) will not continue, and PHP will try to find the first catch that matches it, if an exception is not caught, And without using Set_exception_handler () for the appropriate processing, then PHP will produce a serious error, and output uncaught exception ... (The exception is not caught) prompt information.
<?php
With:
Http://www.jb51.net/article/61316.htm
Http://www.jb51.net/article/59692.htm
Http://www.jb51.net/article/61317.htm
?>
PHP PDO mysql notation