PHP Connection Database with Mysqli and PDO two ways
The following is a sample of PDO
I. Creating a database table (students)
Ii. Connection Database (db.php)
$DSN: Database connection server, database name
$DB _user: Login database name
$DB _pass: Login Database Password
1<?PHP2 $dsn= "Mysql:dbname=phpsql;host=localhost;charset=utf8"; 3 $db _user= ' Root '; 4 $db _pass= ' 123456 '; 5 Try{ 6 $pdo=NewPDO ($dsn,$db _user,$db _pass); 7 $pdo-exec(' Set names UTF8 ');8}Catch(pdoexception$e){ 9 Echo' Database connection failed '.$e-GetMessage (); Ten } One?>
Three, display data list (list.php)
1<?PHP2 require_once' Db.php ';3 $sql= ' SELECT * FROM students ';4 $query=$pdo->query ($sql);5 Echo"<table border= ' 1 ' >";6 Echo"<tr><td>id</td><td>number</td><td>name</td><td> Operations </td> <td> Delete </td></tr> ";7 foreach($query as $row){ 8 Echo"<tr>";9 Echo"<td>".$row[' Id ']. " </td> ";Ten Echo"<td>".$row[' Number ']. " </td> "; One Echo"<td>".$row[' name ']. " </td> "; A Echo"<td><a href= ' update.php?id=".$row[' Id ']. "' > Modify </a></td> "; - Echo"<td><a href= ' delete.php?id=".$row[' Id ']. "' > Delete </a></td> "; - Echo"</tr>"; the } - Echo"</table>"; -?>
Iv. adding a piece of data (index.php,add.php)
1 <HTML>2 <Head>3 <title>Add data</title>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />5 </Head>6 <Body>7 <formAction= "add.php"Method= "POST">8Student number<inputname= "Number"value="" /><BR/>9Student Name<inputname= "Name"value="" /><BR/>Ten <inputtype= "Submit"value= "Add" /> One </form> A </Body> - </HTML>
1<?PHP2 Header(' content-type:text/html; Charset=utf-8 ');3 require_once' Db.php ';4 $data=$_post;5 $number=$data[' Number '];6 $name=$data[' Name '];7 $sql= ' INSERT into students (Number,name) VALUES ("'.$number.‘","‘.$name.‘")‘;8 $result=$pdo-exec($sql); 9 if($result){ Ten Echo"Added successfully! "; One}Else{ A Echo"Add failed! "; - } -?>
V. Modification of a data (update.php)
1<?PHP2 require_once' Db.php ';3 Header("content-type:text/html; Charset=utf-8 "); 4?>5<?PHP6 if($_get){7 $id=$_get[' ID '];8 $sql= ' SELECT * from students where Id = '.$id. ' Limit 1 ';9 $query=$pdo->query ($sql);Ten $data=$query->fetch (PDO::fetch_obj) One?> A - -<title> Add Data </title> the<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/> - -<body> -<form action= "update.php" method= "POST" > +<input type= "hidden" name= "id" value= "<?php Echo$data->id;?> "/> -Student ID <input name= "number" value= "<?php Echo$data->number;?> "/><br/> +Student name <input name= "name" value= "<?php Echo$data->name;?> "/><br/> A<input type= "Submit" value= "Modify"/> at</form> -</body> - -<?php}?> -<?PHP - if($_post){ in $update _data=$_post; - $sql= ' Update students set number = '.$update _data[' Number ']. ' ", name =" '.$update _data[' name ']. ' " where Id = '.$update _data[' ID ']; to $rst=$pdo-exec($sql); + Echo $rst? ' Modified successfully! ': ' Modification failed! ‘; - } the?>
Vi. Deleting a data (delete.php)
1<?PHP2 require_once' Db.php ';3 Header(' content-type:text/html; Charset=utf-8 ');4 $id=$_get[' ID '];5 $sql= ' Delete from students where Id = '.$id;6 $result=$pdo->query ($sql);7 Echo $result? ' Delete succeeded! ': ' Delete failed! ‘;8?>
Note : The database, PHP files, HTML files, development tools coding must be consistent!
Php+mysql base additions, deletions and modifications