Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show MySQL results with PHP

Status
Not open for further replies.

jon159785

IS-IT--Management
Joined
Jul 8, 2003
Messages
15
Location
US
I have a database with the file names of pictures that I want to display. The pictures aren't necessesarily in id order and they are categorized by 'collection'

I want to be able to run a query or a couple of queries to determine the current picture collection based on the id that is passed along as well as the previous picture in the collection and the next one.

It seems I could do this by running a query on all of the data in the database and then filling it in an array and moving through that to find what I need, but that seems like a lot of work considering I could make 3 queries that would accomplish the same things much quicker witout filling an array with everything in the database. But when I made 3 queries and used the 'fetch_array' it only worked for the first one.

Is there some better way of doing this?

Thanks,
-Jon
 
Can you post the source code that you used for the three queries?
 
correct code makes a world of difference. On the 2nd two SQL statements I had left out some quotes and thus I was getting a 'false' for them and that's why the array couldn't be fetched.

Here is the corrected code:

if (!empty($id)) {

//This query finds the information for the picture with the requested id
$query = "SELECT pic_id, pic_thumbnail, pic_picture, pic_description, pic_collection, pic_modified, pic_count FROM `pictures` WHERE pic_id = $id";
$result = @mysql_query($query); //Run the query
$center = mysql_fetch_array($result);
//This query finds the picture with the next higher id in the same collection as the first.
$query2 = "SELECT pic_id FROM `pictures` WHERE pic_collection = '$center[4]' AND pic_id > $id ORDER BY pic_id ASC LIMIT 1";
$result2 = @mysql_query($query2); //Run the query
$right = mysql_fetch_array($result2);
//This query finds the picture with the next lower id in the same collection as the first.
$query3 = &quot;SELECT pic_id FROM `pictures` WHERE pic_collection = '$center[4]' AND pic_id < $id ORDER BY pic_id DESC LIMIT 1&quot;;
$result3 = @mysql_query($query3); //Run the query
$left = mysql_fetch_array($result3);

//To check the pictures of the previous, current and next
echo $left[0].$center[0] . $right[0];


}

I guess I just needed a fresh look at it today. Thanks.
 
You could have caught these errors immediately if you had implemented sufficient error-catching in your code. Also, you are using the &quot;@&quot; error-suppression operator, which is further hampering your debugging efforts.

At a minimum, replacing:

$result = @mysql_query($query);

with

$result = mysql_query($query) or die(mysql_error());

would have told you what is wrong with your code.

Don't use the &quot;@&quot; operator. It's a band-aid.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top