| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read | ![]() |
|
||||||
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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(); |
![]() |
| Thread Tools | |
| Display Modes | |
|
|