Skip to content

Instantly share code, notes, and snippets.

@de-raaf-media
Created December 3, 2012 12:33
Show Gist options
  • Save de-raaf-media/4194743 to your computer and use it in GitHub Desktop.
Save de-raaf-media/4194743 to your computer and use it in GitHub Desktop.
simple database operations
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con) or die(mysql_error());
// insert query
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin',35)") or die(mysql_error());
// update query
mysql_query("UPDATE Persons SET Age=36
WHERE FirstName='Peter' AND LastName='Griffin'") or die(mysql_error());
// delete query
mysql_query("DELETE FROM Persons WHERE LastName='Griffin'") or die(mysql_error());
// select query
$result = mysql_query("SELECT * FROM Persons") or die(mysql_error());
// loop through result
while($row = mysql_fetch_array($result)) {
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
mysql_close($con);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment