Recent Articles

Net Neutrality Amendment Shot Down
Telecommunications giants scored a victory over Net Neutrality advocates in the U.S. legislature yesterday as the proposed "Markey Amendment," a provision to
prevent Internet providers from creating access chokepoints was voted down in the House of Representatives.

Is Your Website Content Delivering you Results?
The Importance of Keeping Content Up to Date: Imagine you're the owner of a successful car dealership but when your customers look at the cars listed on your website
they notice that the cars are a month or so old or, even worse, they call you only to find out that that dream car they were looking at sold a week ago?

Blog Comments Cannot Be Ignored
Price again, this time with details of a study (pdf) conducted by Buzzmetrics which offers some interesting data on blog comments.

What Am I Going to Do with 403 links?
I'm a big social bookmarker, perhaps too big. As I was looking at my account yesterday, I realize I have 403
links bookmarked at BlinkList.

Decoding Web Hosting Reviews: ASP to FrontPage
Web hosts come under many names: ASP Hosting, Cold Fusion Hosting, Java Hosting, PHP Hosting; and as such, so must web hosting reviews.

Understanding Wikis and Best Practice
Finding out about wikis isn't terribly difficult - there are plenty of descriptions around to help you understand
what they are, starting with this one on Wikipedia (itself a wiki).

Understanding Social Bookmarketing and Delicious
Delicious is a social bookmarking system, that is notable, not only for its unusual web address http://del.icio.us,
but for its unusual approach to content building that is becoming increasingly popular.

Websites Reflect True Face of an Organization
A website shows the true face of the organization as
never before. A website is increasingly the place where customers get that vital first impression. Spend a few minutes on...

Understanding Wikis and Best Practice
Finding out about wikis isn't terribly difficult - there are plenty of descriptions around to help you understand what they are, starting with this one on Wikipedia (itself a
wiki).

Decoding Web Hosting Reviews: ASP to FrontPage
Web hosts come under many names: ASP Hosting, Cold Fusion Hosting, Java Hosting, PHP Hosting; and as such, so must web hosting reviews.

05.10.06


Database Queries

By Thomas White

This is a basic tutorial to teach newbies how to query a MySQL database using PHP, and how to display the results.

Here is the query code itself: Code:

$query = "SELECT * FROM Tablename WHERE Fieldname='".$variable."' ORDER BY Fieldname LIMIT 5"; $result = @mysql_query ($query);

This query must be manipulated according to the structure of the database in question. "Tablename" must be edited to reflect the actual name of an existing table, whereas "Fieldname" must also be changed to reflect a field in the table.

This query is also dynamic, because it searches for Fieldname matches to the variable "$variable". Therefore you could supply this variable using a form like this: HTML Code:

<form action=form.php method=post> <input type=text name=[b]variable[/b]> <input type=submit value=Submit name=submit> </form>

Pay attention to the other 2 specifications in the query's code. "ORDER BY Fieldname", as you may guess, tells the script in what order the results should be displayed. It can be changed to any field that exists in the table.

"LIMIT 5" puts a ceiling on the amount of results that the query can fetch, and can obviously be changed to whatever is required. Using some advanced PHP you can use the LIMIT function to paginate mass amounts of data, but I wont cover that here.

Send targeted visitors directly to your site or to the advertisement of your choice. Get Started Now!

Now that we've executed our query, we'll want to make sure it worked, and that it found some results. We do this with a function called mysql_num_rows: Code:

$num = mysql_num_rows ($result);

This code creates a variable to store the amount of rows (or results) that the query affected.

One thing to note is that if your query finds no results and you still try to display results, you will hit some nasty errors. Therefore I like to install a simple check to make sure we did get a result using an if statement: Code:

if ($num > 0) {

Explained in plain english: If num is above 0, (and therefore at least 1 result was found) then do the following. Bear in mind that we have to close the bracket later to obey PHP syntax.

So here comes the more tricky part of fetching the results. We're going to use a loop function called while() to do something for each result we find. Code:

while ($row = mysql_fetch_array($result)) { echo $row[Fieldname]; echo $row[AnotherFieldname]; }

This is a bit more complex. It turns each field name into a unique array that is stored in $row[Fieldname], so whatever is in that particular row in the database under that Fieldname will be stored there. You don't have to have the echo statements in there, they'll just prove that the code has worked so far.

And finally we have to close the open if: Code:

} else { // The query didnt find any results, so: echo "No results found"; }

I hope this tutorial was useful to you! Good luck with PHP and MySQL.


About the Author:
Thomas White of ShortyWhite.com.

About NetDummy
A collection of news and articles covering the latest developments for webmasters and site owners seeking to enhance their online properties. Building better websites for business

NetDummy is brought to you by:

WebProNews.com Jayde.com
MarketingNewz.com SalesNewz.com
CareerNewz.com ManagerNewz.com
eCommNewz.com WebsiteNotes.com
AdvertisingDay.com ManagerNewz.com
SoHoDay.com CRMNewz.com

-- NetDummy is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2006 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article

NetDummy Home Page About Article Archive News Downloads WebProWorld Forums iEntry Advertise Contact Jayde NetDummy News Archives About Us Feedback Building better websites for business WebProWorld Forum