For the Life of Me I can't Make this work

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

lloydsmods
Forum Newbie
Posts: 22
Joined: Wed Aug 14, 2002 2:03 pm
Location: NC

For the Life of Me I can't Make this work

Post by lloydsmods »

I'm querying a table of products and listing them by title and price. The title is an HTML link to a page called "title.php" and a variable (x) is set equal to the product id ($id) and passed in the URL (title.php?x=$id).

Here's the code that works:

Code: Select all

$sql="SELECT * FROM products ORDER BY title ASC";
$result = @mysql_query($sql);
$num = @mysql_num_rows($result);
$i=0;

echo "<table><tr bgcolor="black"><td width="400"><b><font color="white">Title</font></b></td><td width="25"><b><font color="white">Price</font></b></td><td><b><font color="white">Add to Cart</font></b></td></tr>";

while($num > $i)
	&#123;
		$id = @mysql_result($result,$i,"id");	
		$title = @mysql_result($result,$i,"title");
		$cost = @mysql_result($result,$i,"cost");
		$price = $cost * 1.2;
		$price = number_format($price, 2, '.', '');

		if($i % 2)
		&#123;
		echo "<tr bgcolor="#757575"><td width="400" height="20"><a href="title.php?x=$pid">$title</a></td><td width="25" height="20">$price</td><td height="20">";
The title.php page is supposed to display info about the product.

When I click on the title it takes me to "title.php?x=1" (its the only test data I have in the table so far) but this code doesn't work in title.php:



Code: Select all

$x = $_GET&#1111;'x']; 

$sql="SELECT * FROM products WHERE id = $x";
$result = @mysql_query($sql);

$title = @mysql_result($result,$prod,"title");
$cost = @mysql_result($result,$prod,"cost");
$price = $cost * 1.2;
$price = number_format($price, 2, '.', '');
$developer = @mysql_result($result,$prod,"developer");
$description = @mysql_result($result,$prod,"description");
$rating = @mysql_result($result,$prod,"rating");

echo "<table border="1">";
echo "<tr><td colspan="3">";
echo "<h3>Title: $title</h3>";
echo "</td>";
echo "<td><img src="$x.jpg"></td>";
echo "<td><table><tr><td>$price</td></tr><tr><td><img src="theme/addtocart.gif"></td></tr></table></td>";
echo "<td><img src="$rating.gif"></td>";
echo "</tr><tr>";
echo "<td colspan="3">$description</td>";
echo "</tr></table>";
"title.php?x=1" appears in the address bar, as expected, but the info from the table does not display.

Any help would be appreciated. I'm stumped.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

remove all the @'s and see what the error is?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

my guess is that the problem lies with the $prod variable. the second argument of mysql_result should be a number indicating the row offset to look at within the result set. try setting this to 0 (zero). it might help if you told us what $prod was.
lloydsmods
Forum Newbie
Posts: 22
Joined: Wed Aug 14, 2002 2:03 pm
Location: NC

Okay, Here's

Post by lloydsmods »

You're right about the $prod variable. I was toying with anything and everything to try to make this work and left that in there.

I set that variable to zero and I get this:

Supplied argument is not a valid MySQL result resource...

I understand that the variable refers to a row number, but if you're only selecting a single row from the table is it still necessary?

Do I just query the table the same way I would if I were selecting multiple rows?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

i am about to beat myself silly for missing this try replacing this line line:

$sql="SELECT * FROM products WHERE id = $x";

with:

$sql="SELECT * FROM products WHERE id = '$x'";

note the single quotes around $x
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

If you set the "id" as integer you don't need to use single quotes.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

.mysql_error() is your friend and using mysql_fetch_assoc() is better than repeated calls to mysql_result(). Try something like this:

Code: Select all

$x = $_GET&#1111;'x']; 

$sql="SELECT title, cost, developer, description, rating FROM products WHERE id = $x"; 
@$result = mysql_query($sql) or die(mysql_error().'&lt;p&gt;'.$sql.'&lt;/p&gt;'); 

$row = mysql_fetch_assoc($result);
$title = $row&#1111;'title'];
$cost = $row&#1111;'cost'];
$developer = $row&#1111;'developer'];
$description = $row&#1111;'description'];
$rating = $row&#1111;'rating'];

$price = number_format(($cost * 1.2), 2, '.', '');
Mac
lloydsmods
Forum Newbie
Posts: 22
Joined: Wed Aug 14, 2002 2:03 pm
Location: NC

You guys...

Post by lloydsmods »

...are the bomb. I love these forums because you get quick answers, even to stupid questions. I know, there are no stupid questions. Just stupid people who ask questions, right?

Cant wait to get home from work so I can try these suggestions but I'm having a wisdom tooth removed today so I'll have to wait for my pain-killer-induced stupor to wear off. I'll let you know how it goes.

Thanks to all for the help.
User avatar
lanlord
Forum Newbie
Posts: 13
Joined: Wed Sep 18, 2002 10:02 am

well, here's a stupid question

Post by lanlord »

This is unrelated to the problem, but why are you doing:

$x = $_GET['x'];

In title.php I would have just expected $x to contain 1. Am I missing something?
lloydsmods
Forum Newbie
Posts: 22
Joined: Wed Aug 14, 2002 2:03 pm
Location: NC

Post by lloydsmods »

I survived the dentist so I got a chance to try this out tonite.

Using twigletmac's code I get this error:
You have an error in your SQL syntax near '' at line 1
SELECT title,cost,developer,description,rating FROM products WHERE id =
Grrr. Argh.
Last edited by lloydsmods on Wed Sep 25, 2002 5:42 pm, edited 2 times in total.
lloydsmods
Forum Newbie
Posts: 22
Joined: Wed Aug 14, 2002 2:03 pm
Location: NC

Post by lloydsmods »

I took mydimension's suggestion and changed WHERE id = $x and changed it to '$x' and I'm back where I started.

It displays the table, no title, no images.

Hmmmm.....

If you right click the place-holders for the images and select properties they are identified as ".jpg" instead of "1.jpg" so the script is not assigning the passed variable (x) to $x.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what's your php-version?
put print($x.'<br/>'.$_GET['x']); right after <?php before assigning $x=$_GET['x']; to check the need of $_GET
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: well, here's a stupid question

Post by twigletmac »

lanlord wrote:This is unrelated to the problem, but why are you doing:

$x = $_GET['x'];

In title.php I would have just expected $x to contain 1. Am I missing something?
Have a read of this:
viewtopic.php?t=511
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you are using PHP version 4.0.6 or below you should be using $HTTP_GET_VARS['x'] instead of $_GET['x'].

Mac
lloydsmods
Forum Newbie
Posts: 22
Joined: Wed Aug 14, 2002 2:03 pm
Location: NC

Post by lloydsmods »

I've inquired about the version of PHP that my ISP uses...is there a way for me to find out without asking?

But I went ahead and tried $HTTP_GET_VARS['x'] and that gives me the same result.
Post Reply