Go Back   Web Hosting UK Forums | Linux Windows Dedicated Server and cPanel VPS Hosting Forum > Support > FAQ's / Tutorials.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-11-2006, 05:54 PM
Member
 
Join Date: Sep 2006
Posts: 94
Default Common PHP/MySQL Procedures

This is how I learned to do these things; works very well and all methods are very flexible.

CONNECTING TO DATABASE
----------------------------

PHP Code:
mysql_connect("localhost", "db_username", "db_pwd"); //Login as a database user. set this up in CPanel
mysql_select_db("db_name"); //select the database associated with the user



INSERTING INTO A DATABASE TABLE
-------------------------------------

PHP Code:
mysql_query("INSERT INTO table_name VALUES(NULL, '$first_field', '$second_field', '$etc')");

Only use that NULL if you have an auto_increment (like an 'id' field) or something.


MODIFYING A DATABASE TABLE
--------------------------------

PHP Code:
mysql_query("UPDATE table_name SET field_name='$variable',different_field='$anotheron e' WHERE id='$thisID' LIMIT 1");

You can modify the "WHERE" clause to fit any criteria you need, and the LIMIT to whatever you want.


RETRIEVING FROM A DATABASE TABLE
---------------------------------------
The more complex part of database interaction... we're also going to set up the variables to USE the values we retrieve in a list, etc.


PHP Code:
$results = mysql_query("SELECT * FROM table_name WHERE id='$variable' LIMIT 5"); //This gets the data we want

//Now we need to get and display the data, very simply.
//The $row variable is an array that holds each row's data through the while loop.
while ($row = mysql_fetch_array($results))
{
$showThis = $row['field_name'];
echo($showThis."<br>");
}
//Now, each time we go through a row in the database,
//(5 in this case, because of our LIMIT clause in the query),
//it shows the value of a table field named "field_name" then goes
//to another line for the next table row.


That was the most complex part of this actually simple process.


DELETING A ROW FROM A TABLE
---------------------------------

PHP Code:
mysql_query("DELETE FROM table_name WHERE id='$thisID' LIMIT 1");



CLOSING DATABASE CONNETION
---------------------------------

PHP Code:
mysql_close();
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:22 PM.
Copyright 2002-2007 WebHosting.uk.com. All rights reserved.
Web Hosting UK Forum