Page 1 of 2

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

Posted: Tue Sep 24, 2002 6:30 pm
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.

Posted: Tue Sep 24, 2002 6:35 pm
by Coco
remove all the @'s and see what the error is?

Posted: Tue Sep 24, 2002 6:43 pm
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.

Okay, Here's

Posted: Tue Sep 24, 2002 9:09 pm
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?

Posted: Tue Sep 24, 2002 9:44 pm
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

Posted: Wed Sep 25, 2002 1:18 am
by Takuma
If you set the "id" as integer you don't need to use single quotes.

Posted: Wed Sep 25, 2002 5:46 am
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

You guys...

Posted: Wed Sep 25, 2002 9:04 am
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.

well, here's a stupid question

Posted: Wed Sep 25, 2002 2:03 pm
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?

Posted: Wed Sep 25, 2002 5:33 pm
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.

Posted: Wed Sep 25, 2002 5:38 pm
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.

Posted: Wed Sep 25, 2002 6:23 pm
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

Re: well, here's a stupid question

Posted: Thu Sep 26, 2002 5:03 am
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

Posted: Thu Sep 26, 2002 5:07 am
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

Posted: Thu Sep 26, 2002 6:11 am
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.