Posts tagged Mysql

Delete data from a mysql database

/*
Change the first line to whatever
you use to connect to the database.

Change tablename to the name of your
database table.

This example would delete a row from
a table based on the id of the row.
You can change this to whatever you
want.

*/

// Your database connection code
db_connect();

$query = “DELETE FROM tablename WHERE id = (‘$id’)”;

$result = mysql_query($query);

echo “The data has been deleted.”;

?>

Leave a comment »

Add data to a mysql database

<?php

/*
Change the first line to whatever
you use to connect to the database.

We’re using two values, title and
text. Replace these with whatever
you want to add to the database.

Finally, change tablename to the
name of your table.

*/

// Your database connection code
db_connect();

$query = “INSERT INTO tablename(title, text) VALUES(‘$title’,'$text’)”;

$result = mysql_query($query);

echo “The data has been added to the database.”;

?>

Leave a comment »

Connect to a MySQL database

<?php

/*
Edit the database settings to your own.
To use, just include the function and
call it using <?php db_connect(); ?>

You can then do your database queries
*/

// Edit your database settings
$db_host = “localhost”;
$db_user = “username”;
$db_pass = “password”;
$db_name = “databasename”;

function db_connect() {
global $db_host;
global $db_user;
global $db_pass;
global $db_name;
$connection = mysql_connect($db_host,$db_user,$db_pass);
if (!(mysql_select_db($db_name,$connection))) {
echo “Could not connect to the database”;
}
return $connection;
}

?>

Leave a comment »