Jump to content

Only display if database row is not empty


erme

Recommended Posts

Hi,

 

I have a table with an ID of 1-20 with some fields of data in (title, copy, type). I am displaying the data via php as follows:

 

$result = mysql_query("SELECT * FROM database WHERE id = 'theid1'");
$row = mysql_fetch_array($result);

echo $row['title'];
echo $row['copy'];
echo $row['type'];

 

This works fine for the first row in the database (theid1) - but how can i adapt it so that it displays the other id's (theid2, theid3 etc) should there be data added to them. Also, if data was deleted it wouldnt display on the php page.

 

Many thanks.

Edited by erme
Link to comment
Share on other sites

Is your id really called 'theid1'? Because that's a horrible way to do ids. Usually, for something that's not 100% critical (like financial or medical records), an auto-incremented integer is used.

 

Also, you should never actually delete entries from a database. Instead, set a flag to mark if they've been deleted. That will save you from having any gaps.

 

Since you know you only want the first 20 records, you could do something like:

 

$result = mysql_query("SELECT * FROM database LIMIT 20 ORDER BY id ASC");

while($row = mysql_fetch_assoc($result)) {
   echo $row['title'];
   echo $row['copy'];
   echo $row['type'];
}

 

That said, you shouldn't be using the old mysql_* functions. They're soft deprecated. Instead, use either MySQLi, or, better yet, PDO.

Link to comment
Share on other sites

Is your id really called 'theid1'? Because that's a horrible way to do ids. Usually, for something that's not 100% critical (like financial or medical records), an auto-incremented integer is used.

 

Also, you should never actually delete entries from a database. Instead, set a flag to mark if they've been deleted. That will save you from having any gaps.

 

Since you know you only want the first 20 records, you could do something like:

 

$result = mysql_query("SELECT * FROM database LIMIT 20 ORDER BY id ASC");

while($row = mysql_fetch_assoc($result)) {
echo $row['title'];
echo $row['copy'];
echo $row['type'];
}

 

That said, you shouldn't be using the old mysql_* functions. They're soft deprecated. Instead, use either MySQLi, or, better yet, PDO.

 

^ What he said, although I think he simply has 20 rows in his table but wants to display them all. If that is the case, simply remove the LIMIT clause.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.