5 feb
Hi,
Yet another simple howto, this one is more an example page, most people including me, are searching the web for examples and howto documents, in these examples I only say what it will do and you have to figure the rest out to integrate into your programming/website.
So this document is about, how to fetch data from a mysql database with php, how to insert data into a database on a mysql server with php, and how to replace data in a mysql database with php. Do not complain to me if it’s to simple, that is the purpose of this document
Here is a sample of how to fetch simple data from a mysql server:
We have a database “test”, with a table named “testing”. In the table testing we have the “id”, “text” and “image_location”.
Code:
if (!mysql_connect(’127.0.0.1′,’test’,'test123′)) {
echo “Sorry database is offline or username password incorrect”;
};
$db = “test”;
$result = mysql($db,”select * from testing”);
do {
$row = mysql_fetch_array($result);
if ($row[id] > “”) {
echo “$row[id]“;
echo “$row[text]“;
echo “$row[image_location]“;
};
} while ($rij);
mysql_close();
Now we are going to insert fresh data in the same database, so we are doing an insert. For example you want to put data into text and image_location, the ID is an automatic mysql increment.
You can call the php with this url http://example.com/test.php?text=hello&image_location=top
$text contains the value “hello”
$image_location contains the value “top”
This is handy so if you see in the script this will be added into the database.
if (!mysql_connect(’127.0.0.1′,’test’,'test123′)) {
echo “Sorry database is offline or username password incorrect”;
};
mysql(test,”insert into testing(text, image_location) values (’$text’, ‘$image_location’)”);
mysql_close();
Last but not least we will update the database also, so for example if we have an ID in above example we could update the text and the image_location that belongs to that ID, so we have ID 6, and ID 6 contains the text “hello” en the image_location “top” and we would like to change “top” to bottom we do the following.
if (!mysql_connect(’127.0.0.1′,’test’,'test123′)) {
echo “Sorry database is offline or username password incorrect”;
};
mysql_select_db(test);
mysql_query(”update testing set image_location=’top’ where id=’6′”);
mysql_close();
So again, this is very simple and should only be used for reference or learning to understand php and mysql a little bit, for more advanced database querys and stuff please again visit Google.
You really can’t find your problem you can always find my email on my homepage, but though it can be lost in the massive spam
. And yet again the one who searches will find a solution, a good reference for a php questions is www.php.net.
Leave a reply
You must be logged in to post a comment.