erme Posted February 5, 2013 Share Posted February 5, 2013 (edited) 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 February 5, 2013 by erme Quote Link to comment https://forums.phpfreaks.com/topic/274064-only-display-if-database-row-is-not-empty/ Share on other sites More sharing options...
Jessica Posted February 5, 2013 Share Posted February 5, 2013 Every single example on the page shows how to do that: http://php.net/mysql_fetch_array Quote Link to comment https://forums.phpfreaks.com/topic/274064-only-display-if-database-row-is-not-empty/#findComment-1410282 Share on other sites More sharing options...
KevinM1 Posted February 5, 2013 Share Posted February 5, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274064-only-display-if-database-row-is-not-empty/#findComment-1410283 Share on other sites More sharing options...
Andy123 Posted February 5, 2013 Share Posted February 5, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274064-only-display-if-database-row-is-not-empty/#findComment-1410284 Share on other sites More sharing options...
erme Posted February 6, 2013 Author Share Posted February 6, 2013 Thank you. I added a 'publish' box that the user can select. Quote Link to comment https://forums.phpfreaks.com/topic/274064-only-display-if-database-row-is-not-empty/#findComment-1410418 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.