PHP Database SELECT and INSERT Example – php-pdo

LAMP is probably the most popular web stack in the world. It powers Wikipedia, and it powered Facebook for years.
To access your MariaDB database from PHP server side scripts, use PHP-PDO. Remember to use library functions to put user data into SQL strings (prepare, bind); and sanitize away any Javascript before showing data to users (htmlentities).

This is just a quick script. For more thorough explanations, see Read MySQL database with PHP – php-pdo.

<?php
// connect
$pdo=new PDO("mysql:host=localhost;charset=UTF8;dbname=hats", 'hats', 'di3fgh2..&dheAB_loK');
// insert
$name = $_GET["name"];
$price = $_GET["price"];
if ("" != $name) {
	echo "<p>Inserting new record.</p>";
	$pdoStatement=$pdo->prepare("INSERT INTO hats (name) VALUES (:name)");
	$pdoStatement->bindParam(':name', $name);
	$pdoStatement->execute();
}
// list
$pdoStatement=$pdo->prepare('SELECT * FROM hats;');
$pdoStatement->execute();
$hits=$pdoStatement->fetchAll();
foreach($hits as $row) {
     echo "<p>".$row['id']." ".htmlentities($row['name'])." price ".$row['price']." rupees</p>\n";
}
?>
<form method=get>
	<input type=text name=name value="pipo">
	<input type=text name=price value=12>
	<input type=submit>
</form>
Posted in Uncategorized | Tagged | Comments Off on PHP Database SELECT and INSERT Example – php-pdo

Comments are closed.