Simply Cutting Edge
Welcome Guest [Log In] [Register]
Welcome to zetaNetwork. We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as viewing zetaNetwork exclusive tutorials and articles, and access to our code snippets section. Registration is simple, fast, and completely free.


Click Here To Register


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Query Content
Topic Started: Apr 17 2011, 02:08 AM (25,106 Views)
Flavius
Member Avatar
Awesome Cool Dude

Query Content
Difficulty: Easy
Time: 15 - 20mins



PHP and SQL work hand in hand to store and display information and content for websites. Data is stored in the database and is queried by PHP. This data can be obtained, inserted, updated and deleted. Simple display of this data is done using HTML, while inserting, updating and deleting requires forms. Forms are still HTML, but are sent. A form without PHP will not send any information. This sending of data requires the reload of a page unless done through Ajax or Jquery. The following article will teach you the basics of Querying.

Connecting to the Database:
Code:
 
$mysql_host = "host.mysql.link";
$mysql_database = "name.of.database";
$mysql_user = "user.connected.to.database";
$mysql_password = "password.of.user.above";

$connection = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die("Could not connect to MySQL");
mysql_select_db($mysql_database, $connection) or die ("Database cannot be found.");
A connection to the database is always simple to make. Everything is set up through the control panel and phpmyadmin, after which you can connect to it easily. All the functions are built in. The die you see in the code will terminate the attempt if there's a fault and output an error. In this case, if it cannot connect to the database, it will say so. Should you simply want a generic error, you can use another built in function mysql_error() which would be placed within die().

I like to put the connection in a file and call it config.php but it's up to you what you call it. It's also good to put mysql_close($connection); in an ending file just to close the connection to protect yourself.

Grabbing Data from Database:
Code:
 
$query = "SELECT column.name
FROM table.name
WHERE row.value.of.column.name = 'this.value'
AND/OR row.value.of.column.name = 'this.value'
LIMIT number.rows.to.grab";
$result = mysql_query($query);
This query will grab information from the database. As you can see, it grabs defined information based on certain parameters and limits the amount of information that is grabbed. It is faster to grab only the information you and how much you need as opposed to all information. This greatly decreases the loading time of a query. If you wish to grab all columns from a table, instead of column.name you would use *. Parameters are not mandatory to a query and neither is the limit. The limit however is highly supported, especially when the database is large.

Inserting into the Database:
Code:
 
$query = "INSERT INTO table.name(column.name)
VALUES ('row.value')
LIMIT number.rows.to.update";
$result = mysql_query($query);
Inserting is, as you can see, the shortest of the queries. There's nothing to really comment on this one, it's self explanatory. There's one thing to remember though! The values will have quotation marks, the column names will not. This is highly important or the query will not work.

Updating the Database:
Code:
 
$query = "UPDATE table.name
SET column.name = 'row.value'
WHERE row.value.of.column.name = 'this.value'
AND/OR row.value.of.column.name = 'this.value'
LIMIT number.rows.to.update";
$result = mysql_query($query);
Here, the code is much like a grab query, but in this case, you update data already in the database. For this reason, this code must include parameters. As well as this, if updating only 1 line as defined by a unique ID, it is best to limit the the update to 1. When the ID is found it will no longer search and the row will be updated. If there are more than one IDs searched, the limit would be the number of IDs. All column.names and row.value are separated by commas.

Delete from Database:
Code:
 
$query = "DELETE
FROM table.name
WHERE row.value.of.column.name = 'this.value'
AND/OR row.value.of.column.name = 'this.value'
LIMIT number.rows.to.grab";
$result = mysql_query($query);
This query is much simpler. It will delete the row that matches you parameters. It's as simple as that. As you can see, there's also no column.name defined. There is no need as the whole row is deleted. If you want to simply remove the content of a certain column, then you would update the database, as opposed to delete, setting the content to NULL for that column.



Flavius | zetaNetwork Instructor
If you have any questions feel free to PM me or leave the questions bellow.
Offline Profile Quote ^
 
Pro
Member Avatar
Underground Coding

Great tut on data queries except they don't know how to access the queried data :\
Pro | zetaNetwork Instructor & Admin
I Coded Most Of The Smexy Stuff You See :P
PM Me Any Questions
Need live support? Click here.
Offline Profile Quote ^
 
Flavius
Member Avatar
Awesome Cool Dude

Pro
Apr 17 2011, 07:22 AM
Great tut on data queries except they don't know how to access the queried data :\
Just for you I've added it in :wub: :P
Offline Profile Quote ^
 
Pro
Member Avatar
Underground Coding

Great job with this tut. The only thing I would like to mention though, is that you forgot to explain die, which is basically when your connection or query fails.
Pro | zetaNetwork Instructor & Admin
I Coded Most Of The Smexy Stuff You See :P
PM Me Any Questions
Need live support? Click here.
Offline Profile Quote ^
 
Flavius
Member Avatar
Awesome Cool Dude

Pro
Apr 17 2011, 05:46 PM
Great job with this tut. The only thing I would like to mention though, is that you forgot to explain die, which is basically when your connection or query fails.
Done once more. It's always good if anyone thinks there's more that needs to be added, I'll just add it in.
It's strange, but I come back to this thread all the time XD
Edited by Flavius, Jun 2 2011, 06:40 AM.
Offline Profile Quote ^
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Tutorials · Next Topic »
Add Reply