Help with Accessing Data from MySQL DB with PHP Function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
urb
Forum Newbie
Posts: 21
Joined: Fri Nov 08, 2002 1:08 am
Location: Los Angeles, California

Help with Accessing Data from MySQL DB with PHP Function

Post by urb »

If anyone could help me out with this function I wrote on a thumbnail display code for my site. The function is supposed to show specific thumbnails from a table in my database. For instance if you click on business category it would display all the thumbnails from the table with the category business in them.

My code is below.



function template_display() {
$results4 = mysql_query("SELECT * FROM templates ORDER BY tempid DESC");
while($row4 = mysql_fetch_array($results4)) {
$name = $row4['name'];
$price = $row4['price'];
$uniq_price = $row4['uniq_price'];
$designer = $row4['designer'];

print "<table width='400' border='0' cellspacing='0' cellpadding='0' align='center'>
<tr>
<td width='150' valign='top'>
<div align='center'><img src='/files/templates/thumbs/$name.gif' border='1'><a href='preview.php?name=$name'><img src='/images/button_preview.gif' width='59' height='14' border='0'></a>
<a href='cart.php?txt_itemcode=$tempid&txt_mode=addition_new'><img src='images/button_addtocart.gif' width='77' height='14' border='0'></a>
</div>
</td>
<td width='10'>&nbsp;</td>
<td valign='top'>
<table width='100%' border='0' cellspacing='1' cellpadding='1'>
<tr>
<td><b>Name: </b>$name</td>
</tr>
<tr>
<td><b>Price: $</b>$price</td>
</tr>
<tr>
<td><b>Unique: $</b>$uniq_price</td>
</tr>
<tr>
<td><b>Designer: </b>$designer</td>
</tr>
</table>
</td>
</tr>
</table><br><br>";

}
}



When I use this code it will display the thumbnails the way I want, but it will display all the thumbnails in the database. No matter what category I click on it displays all the thumbs from the database.

Can someone please help me order my thumbs by $catid. I have tried different things but they all seem to not work.

Any help is greatly appreciated.

Thanks

Matt Urban
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

add a WHERE clause in your select statement

mysql_query("SELECT column1,column2,column3 FROM sometable WHERE thecatcolumn = '".$catid."' ORDER BY columntosortby");
urb
Forum Newbie
Posts: 21
Joined: Fri Nov 08, 2002 1:08 am
Location: Los Angeles, California

Post by urb »

results4 = mysql_query("SELECT * FROM templates WHERE category = '".$catid."' ORDER BY tempid DESC");
Using the line of code above with the WHERE clause added, I get no errors produced from it but still nothing will display now when i click on a link such as cataglog.php?catid=5 and there should be 3 thumbnails shown under that category.

Please help?


Thanks for the time
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you passed $catid into the function? Or do you access it as a superglobal? If you haven't there's a couple of things you could do. If you are using PHP version 4.1 or greater you can reference catid as $_GET['catid'] - but I would do some validation on it before putting it into the SQL statement (e.g. if you expect it to be a number check it with is_numeric()). Or if you've already done that before you call the function just declare the variable global in the function:

Code: Select all

function template_display() { 
    global $catid;

    // rest of function
}
You might also want to consider moving your SQL statements out of the call to mysql_query() and into a variable so that you can echo them to make sure variables are being passed as expected. E.g.:

Code: Select all

$sql = "SELECT * FROM templates WHERE category = '".$catid."' ORDER BY tempid DESC";
$results4 = mysql_query($sql);

then if things aren't going as expected you can just do:

Code: Select all

echo $sql;
to check that the SQL statement looks as it should.

Mac
urb
Forum Newbie
Posts: 21
Joined: Fri Nov 08, 2002 1:08 am
Location: Los Angeles, California

Post by urb »

Creating $catid as a global variable worked. Thank you so much. Also can you link me to a helpful site about global variables or if you have time care to tell me some more info about when and when not to use them.


Thanks agian

urb
Post Reply