PHP using PDO implement CRUD


<?php

///// php pdo for query operation

header( "Content-type: text/html; charset=utf-8" );
$dbms = 'mysql';
$user = 'root';
$pwd = '12345678';
$dbName = 'ceshi';
$host = 'localhost';
$charset = 'utf8';
$dsn = "$dbms:host=$host;dbname=$dbName;charset=$charset";
try {
$pdo = new PDO( $dsn, $user, $pwd );
} catch ( Exception $e ) {
echo $e->getMessage();
}

// query
$ sql = "dunling_chat the WHERE from the SELECT * the above mentioned id =?";
// sql ready template
$ stmt = $ PDO-> PREPARE ($ sql);
$ the above mentioned id = '1';
// bind parameters
$ stmt- > bindValue (. 1, $ ID);
// execute prepared statement
$ stmt-> execute ();
// recommended to obtain query results in this manner
the while ($ Row = $ stmt-> FETCH ()) {
echo $ Row . [ 'ID'] "<br />";
echo $ Row [ 'nicheng'] "<br />";.
echo $ Row [ 'Content'] "<br />";.
echo $ Row [ ' . Time '] "<br />";
}
// Free result
$ stmt = null;
// close the connection
$ pdo = null;

?>

 

 

 

<?php

/////php操作pdo实现更新
header( "Content-type: text/html; charset=utf-8" );
$dbms = 'mysql';
$user = 'root';
$pwd = '12345678';
$dbName = 'ceshi';
$host = 'localhost';
$charset = 'utf8';
$dsn = "$dbms:host=$host;dbname=$dbName;charset=$charset";
try {
$pdo = new PDO( $dsn, $user, $pwd );
} catch ( Exception $e ) {
echo $e->getMessage();
}

// Update the
$ sql = "Update dunling_chat the SET nicheng the WHERE the above mentioned id = =??";
// sql ready template
$ stmt = $ PDO-> PREPARE ($ sql);
$ name = 'One';
$ Age = 1;
/ / bound parameters
$ stmt-> bindValue (. 1, $ name);
$ stmt-> bindValue (2, $ Age);
// execute prepared statement
$ stmt-> execute ();
$ $ stmt- affect_row => the rowCount ();
IF ($ affect_row) {
echo 'successfully updated' '<br>';.
} the else {
echo 'update failed' '<br>';.
}
// Free result
$ stmt = null;
// Close connecting
$ pdo = null;

?>

 

 

<?php

////php操作pdo实现插入
$dbms = 'mysql';
$user = 'root';
$pwd = '12345678';
$dbName = 'ceshi';
$host = 'localhost';
$charset = 'utf8';
$dsn = "$dbms:host=$host;dbname=$dbName;charset=$charset";
try {
$pdo = new PDO( $dsn, $user, $pwd );
} catch ( Exception $e ) {
echo $e->getMessage();
}

// insert
$ sql = "INSERT INTO dunling_chat (nicheng, Content) values (,??)";
// sql ready template
$ stmt = $ PDO-> PREPARE ($ sql);
$ nicheng = 'TWO';
$ Content = 000;
// bind parameter
$ stmt-> bindValue (. 1, $ nicheng);
$ stmt-> bindValue (2, $ Content);
// execute prepared statement
$ stmt-> execute ();
$ = $ INSERT_ID PDO-> lastInsertId ();
IF ($ INSERT_ID) {
echo 'new success' '<br>';.
} the else {
echo 'new failure' '<br>';.
}
// release the results
$ stmt null =;
// close the connection
$ pdo = null;

?>

 

 

? <PHP

//// pdo PHP operation achieved delete
header ( "Content-of the type: text / HTML; charset = UTF-8");
$ the DBMS = 'MySQL';
$ the User = 'root';
$ pwd = '12345678 ';
$ dbName =' ceshi ';
$ Host =' localhost ';
$ charset =' UTF8 ';
$ DSN = "$ DBMS: Host = $ Host; dbname = $ dbName; charset = $ charset";
the try {
$ PDO new new PDO = ($ DSN, the User $, $ pwd);
}
the catch (Exception $ E)
{
echo $ E-> getMessage ();
}

// delete
$ SQL = "dunling_chat the delete from the WHERE the above mentioned id =?";
// sql ready template
$ stmt = $ PDO-> pREPARE ($ sql);
$ the above mentioned id = 11;
// bind parameters
$ stmt-> bindValue (1, $ the above mentioned id);
// execute the prepared statement
$ stmt-> execute ( );
$affect_row = $stmt->rowCount();
IF ($ affect_row)
{
echo 'successfully deleted' '<br>';.
}
the else
{
echo 'delete failed' '<br>';.
}
// Free result
$ stmt = null;
// close the connection
$ PDO = null;

?>

 

Guess you like

Origin www.cnblogs.com/summerGraden/p/11458875.html