hi Yesterday it was 213 again. Although it was objectively affected by the fact that my roommate didn’t go to bed until after 3 o’clock, the fundamental reason was that I didn’t want to study last night. Start it today. I plan to finish learning PDO and Ajax within 3 or 4 days. I hope everyone will come and scold me if they have nothing to do, so that I won't be lazy again. 1. PDO 2. Use of PDO objects (2) 2.2 Error message errorCode() error number; errorInfo(
hi
It was 213 again yesterday. Although it was objectively affected by the fact that my roommate didn’t go to bed until after 3 o’clock, the fundamental reason was that I didn’t want to study last night. Start it today. I plan to finish learning PDO and Ajax within 3 or 4 days. I hope everyone will come and scold me if they have nothing to do, so that I won't be lazy again.
1. PDO
2. Use of PDO objects (2)
2.2 Error message
errorCode()——error number;
errorInfo()——error message;
Give me a chestnut
/*
* PDO error message
*/
$pdo=new PDO('MySQL:host=localhost;dbname=imooc','root','');
$pdo->exec('use imooc_pdo');
$resultd=$pdo->exec('delete from user where id=13');
var_dump($resultd);
$insert='insert user(username,passWord,email) values("Knga","'.md5('king').'","shit@shit.com")';
$result1= $pdo->exec($insert);
var_dump($result1);
if ($result1==false) {
echo "An error occurred";
echo $pdo->errorCode();
PRint_r($pdo->errorInfo());
}
Look at the error message
Array ( [0] => 23000 [1] => 1062 [2] =>
0 is the error type, 1062 is the code, and 2 is the error message; (This is because the username is set to a unique key, but the ID number is still growing).
2.3 query() to implement query
Execute a statement and return a PDOstatement object.
--Give me a chestnut
/* * PDOquery
*/
$pdo->exec('use imooc_pdo');
$insert='select * from user';
$result1=$pdo->query($insert);
var_dump($result1); //View statement object
foreach ($result1 as $row){ //View the output results (according to the return situation) print_r($row);
}
if ($result1== false) {
echo "An error occurred";
echo $pdo->errorCode();
print_r($pdo->errorInfo());
}
If there is a problem with the sql statement, the statement object is false, and the subsequent output is also an error message;
If the sql statement is correct but the content of the query does not exist, then the statement object is OK and the output is empty.
Of course it will look better this way:foreach ($result1 as $row){ //View the output results (according to the return situation)
// print_r($row);echo "
";
echo 'number: '.$row['id'];echo "
";
echo 'Username:'.$row['username'];echo "
";
echo 'Password:'.$row['password'];echo "
";
echo 'Email:'.$row['email'];echo "
";
echo "
";
}
Of course, it is no problem to add, delete or modify the query.
2.4 prepare() and execute() methods to implement query
The recommended query method can realize conditional query.
prepare()——Prepare the SQL statement to be executed and return the PDOstatement object;
execute()——Execute a prepared statement,returns true or false;
So the above is a pair.
--give an example
/** PDOprepare&execute method
*/
$pdo=new PDO('mysql:host=localhost;dbname=imooc','root','');
$pdo->exec('use imooc_pdo');
$insert='select * from user where username="king"';
$result=$pdo->prepare($insert);var_dump($result) ;
$result1=$result->execute();//Execution is for prepared statementsvar_dump($result1);
print_r($result->fetchAll());//The result can only be output for the statement object
if ($result1==false) {
echo "An error occurred";
echo $pdo->errorCode();
print_r($pdo->errorInfo());
}
--Select the output format
To associate array output or all or index arrays, there are two different methods: parameters and methods.header('content-type:text/html;charset=utf-8');
try{
$pdo=new PDO('mysql:host=localhost; dbname=imooc','root','root');
$sql='select * from user';
$stmt=$pdo->prepare($sql);
$res= $stmt->execute();
// if($res){
// while($row=$stmt->fetch(PDO::FETCH_ASSOC)){//only Associative array output is required
// print_r($row);
// echo '
';
// }
// }
// $rows=$stmt->fetchAll(PDO::FETCH_ASSOC);
// print_r($rows);
echo '
';
$stmt-> ;setFetchMode(PDO::FETCH_ASSOC); //Same implementation effect, you can also use this method, set the default mode
//var_dump($stmt);
$rows=$stmt-> fetchAll();
print_r($rows);
}catch(PDOException $e){
echo $e->getMessage();
}
Generally we want to index an array.
2.5 Set database connection properties
setAttribute()——Set database connection attributes;
getAttribute()——Get the database connection attributes;
--give an example
$pdo=new PDO('mysql:host=localhost;dbname=imooc','root','');
echo "Autocommit".$pdo->getAttribute(PDO: :ATTR_AUTOCOMMIT);echo "
";
//Remember that pdo is an object, so you get the attributes, you know. Then there are many set attribute values inside it, which is the premise for us to get the attributes.
echo "Default error handling mode:".$pdo->getAttribute(PDO::ATTR_ERRMODE);echo "
";
$pdo->setAttribute(PDO ::ATTR_AUTOCOMMIT, 0);
echo "Autocommit".$pdo->getAttribute(PDO::ATTR_AUTOCOMMIT);echo "
";
Then try to get a large wave of attribute information:
$attrArr=array(
'AUTOCOMMIT','ERRMODE','CASE','PERSISTENT','SERVER_INFO','SERVER_VERSION'
);
foreach ($attrArr as $attr) {
echo "PDO::ATTR_$attr: ";
echo $pdo->getAttribute(constant("PDO::ATTR_$attr"))."
";
}
Some are not available, there will be error messages, it doesn’t matter.
3. Use of PDOstatement object
3.1 quote() method to prevent SQL injection
--SQL injection
First of all, give an example to illustrate this simple SQL injection (actually I don’t understand it very well - Baidu http://baike.baidu.com/link?url=jiMtgmTeePlWAqdAntWbk-wB8XKP8xS3ZOViJE9IVSToLP_iT2anuUaPdMEM0b-VDknjolQ8BdxN8ycNLohup_)
The so-called SQL injection is to insert SQL commands into Web form submissions or enter domain names or query strings for page requests, ultimately tricking the server into executing malicious SQL commands.
So you need to have a form, and then you need to query the database for data, etc., and then through malicious use of loopholes in the rules, you can get a large amount of data that is not what the page expects. The chestnuts are as follows:
The example is login - login requires user name, password, etc., and needs to be compared with the information in the database;
The first is the login page



