Jump to content

[SOLVED] Omitting Missing DB id's


pcbytes

Recommended Posts

Hah heres the real issue.  ;D

 

This works great for spitting out images to my screen from the database...

Lets say I deleted id 20 from the database.

When the for loop gets to the 20 it just prints a blank image box on the screen.

 

How do I go about getting it to check to see if the id is still there? And if it doesn't exist not to print it. This is what I tried.

 

<?
mysql_connect("localhost","jason","p4ssw0rd");
mysql_select_db("test");

$result = mysql_query("select id from upload");
$id = mysql_result($result,0,"id");
$rows = mysql_num_rows($result);

for ($i=$id; $i < $rows; $i++){
if($id){
	print "<img src=\"download.php?id=$i\"><br>";
}
}

Link to comment
https://forums.phpfreaks.com/topic/43443-solved-omitting-missing-db-ids/
Share on other sites

<?php
mysql_connect("localhost","jason","p4ssw0rd");
mysql_select_db("test");

$result = mysql_query("select id from upload");
$id = mysql_result($result,0,"id");
$rows = mysql_num_rows($result);

for ($i=$id; $i < $rows; $i++){
if($id){
	print "<img src=\"download.php?id=$i\"><br>";
}
}else{

print "<img src=\"no_pic.jpg"><br>";
}
?>

I've actually tried that, when $i counts to 20. It still prints the blank image box even though id 20 doesnt exist in the database.

 

also tried this to see if it would verify the id in the database with no luck;

 

<?
mysql_connect("localhost","jason","p4ssw0rd");
mysql_select_db("test");

$result = mysql_query("select id from upload");
$id = mysql_result($result,0,"id");
$rows = mysql_num_rows($result);

for ($i=$id; $i < $rows; $i++){
if ($var1 = mysql_query("select id from upload where id =$i")){
	print "<img src=\"download.php?id=$var1\"><br>";
}
}
?>

I almost have this narrowed down. Changed it to a while loop. Now it displays the correct amount of image boxes. but the image boxes are blank because it is not grabbing the row id in the img src= section.

 

<?
mysql_connect("localhost","jason","password");
mysql_select_db("test");

$result = mysql_query("select id from upload");

while($row = mysql_fetch_row($result)){
$var1 = $row['id'];
print ("<img src=\"download.php?id=$var1\"><br>");

}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.