First, link the database and query the database data:
The
code is as follows:
<?php
$dbms = ' MySQL '; Database type, for developers, use a different database, just change this, do not have to remember so many functions
$host = ' localhost '; Database host name
$dbName = ' db_database19 '; Database used by
$user = ' root '; Database connection user name
$pass = ' root '; The corresponding password
$dsn = "$dbms: host= $host;d bname= $dbName";
try {
$pdo = new PDO ($DSN, $user, $pass); Initializing a PDO object is the creation of a database connection object $pdo
$query = "SELECT * from Tb_pdo_mysql"; Define SQL statement
$result = $pdo->prepare ($query); Prepare query Statements
$result->execute (); Executes the query statement and returns the result set
while ($res = $result->fetch (PDO::FETCH_ASSOC)) {//while loops out the query result set and sets the result set as the associated index
?>
<tr>
<td align= "center" valign= "Middle" ><?php echo $res [' id '];? ></td>
<TD align= "center" valign= "Middle" ><?php echo $res [' Pdo_type '];? ></td>
<td align= "center" valign= "Middle" ><?php echo $res [' database_name '];? ></td>
<td align= "center" valign= "Middle" ><?php echo $res [' dates '];? ></td>
<td align= "center" valign= "middle" ><a href= "Javascript:del (<?php echo $res [' ID ']?>]" > Delete </a ></td>
</tr>
<?php
}
} catch (Pdoexception $e) {
die ("error!:"). $e->getmessage (). "<br/>");
}
?>
Invoke a JavaScript method at the hyperlink deletion, passing the record Id,js method as:
The
code is as follows:
<script>
function del (_id) {
if (Confirm ("Confirm deletion"))
{
window.location.href= "index.php?conn_id=" +_ID; This page refreshes
}
}
</script>
To delete a database record code:
The
code is as follows:
<?php
if (@$_get[' conn_id ']!= "") {
$dbms = ' MySQL '; Database type, for developers, use a different database, just change this, do not have to remember so many functions
$host = ' localhost '; Database host name
$dbName = ' db_database19 '; Database used by
$user = ' root '; Database connection user name
$pass = ' root '; The corresponding password
$dsn = "$dbms: host= $host;d bname= $dbName";
try {
$pdo = new PDO ($DSN, $user, $pass); Initializing a PDO object is the creation of a database connection object $pdo
$pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception);
$query = "Delete from tb_pdo_mysql where Id=:id";
$result = $pdo->prepare ($query); Pre-prepared statement
$result->bindparam (': Id ', $_get[' conn_id '); Bind the updated data
$result->execute ();
} catch (Pdoexception $e) {
Echo ' PDO Exception caught. '
Echo ' Error with the database:<br/> ';
Echo ' SQL Query: '. $query;
echo ' <pre> ';
echo "Error:". $e->getmessage (). "<br/>";
echo "Code:". $e->getcode (). "<br/>";
echo "File:". $e->getfile (). "<br/>";
echo "line:". $e->getline (). "<br/>";
echo "Trace:". $e->gettraceasstring (). "<br/>";
echo ' </pre> ';
}
}
?>
This code is placed at the very beginning of the body section of the HTML page and, at worst, before the query logging code.