Sample code for using MySQL database in PHP is posted here in this blog.
<html>
<body>
<?php
//////////// connection////////////
$con = mysql_connect("localhost","root",""); //server,user,password
if(!$con)
die("Cannot connect with myqsl: ".mysql_error());
// select a database
mysql_select_db("badhan",$con); // databaseName:badhan
//////////// insert ////////////
$q = "INSERT INTO person (PersonName, BloodGroup) VALUES ('" . $_POST["pname"] . "', '" . $_POST["bloodgroup"] ."')"; // name and bloodgroup value are posted from another PHP file
// execute the query
$result = mysql_query($q,$con);
// show result if successfully added data
if(!$result)
die ("Error in insertion : " . mysql_error());
echo "1 row successfully added!!";
///////// select ////////////
$q = "SELECT * from person";
$result = mysql_query($q, $con);
if(!$result)
die ("Error : " . mysql_error());
// explore result of select query
while($row = mysql_fetch_array($result))
echo $row["PersonName"] . " , " . $row["BloodGroup"] . "<br/>";
// close the connection
mysql_close($con);
?>
</body>
</html>
0 Comments:
Post a Comment