...

View Full Version : passing var for mysql query



rndilger
03-10-2007, 08:00 PM
Hello everyone, I have a quick PHP/MySQL syntax question.


<?php
if(isset($_GET['id'])){

$id = $_GET['id'];

echo "ID: ".$id;

$result = mysql_query("SELECT * FROM classifieds WHERE adid=$id");
while ($row = mysql_fetch_assoc($result)) {
echo "<img src='images/classifieds/$row[pictureid].jpg'>";
}

mysql_close($connect);

} else {

echo "<br><br><span class='bold'>We apologize, but you have reached this page in error.<br><br>Please use the navigation menu on the left to choose another main page.</span>";

}
?>

I've confirmed that I am receiving the id variable (i.e., I'm able to echo the correctly passed id). However, the MySQL query is not working. I'm guessing I've missed something small, but any assistance would be appreciated.

Thanks,

Ryan

Fou-Lu
03-10-2007, 08:24 PM
Depending on your mysql version, you probably just need to wrap your id in quotes:


$result = mysql_query("SELECT `pictureID` FROM classifieds WHERE adid='" . $id . "'");

Since you only need your pictureID, don't bother with selecting all of your fields as well, it will save you some processing time.
Try that, see if the solution is as simple as that. If not, post back.

rndilger
03-10-2007, 08:27 PM
Nope, not quite that simple I'm afraid. Also, I need to select all variables from the database because they will be used when I further develop this code.

Thanks,

Ryan

Nightfire
03-10-2007, 08:53 PM
echo '<img src="images/classifieds/'.$row['pictureid'].'.jpg">';

The variable there is not a constant, so remember to add in the quotes around pictureid.

Even if you're needing all the fields, do not use *, call the fields singally by their name

rndilger
03-10-2007, 09:01 PM
Well, I've tried your suggestion and still had no luck. Also, because I'm still learning the nuances of php/mysql, could you please explain why it's more appropriate to call specific fields?

Thanks,

Ryan

rndilger
03-10-2007, 09:13 PM
Ok, I've figured out the syntax problem. Rather, it was a mistake in the particular database entry I was using. Thanks for the assistance, and I'd still like to hear your logic as to why individual fields should be called.

Thanks,

Ryan

Fou-Lu
03-10-2007, 10:23 PM
echo '<img src="images/classifieds/'.$row['pictureid'].'.jpg">';

The variable there is not a constant, so remember to add in the quotes around pictureid.

Even if you're needing all the fields, do not use *, call the fields singally by their name

Good call m8, I didn't notice that one o.O

Individual field calls reduce the load on the memory that needs to be allocated . Not a big deal in small time applications, but if you only make use of a couple of field entries on a table containing 1000000 records, it is less intensive to store just the fields required instead of all fields for all records. Granted that when the database table loads, it loads all data into memory, but will discard any that is not in use.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum