Can someone tell me what is wrong with this? <?php include("databaseinfo.php"); $query = mysql_query("SELECT intro FROM layoutdesc WHERE cat='$idd'"); $description = $row['intro']; echo $description; ?> Code (markup): I basically wrote an introduction to each page of my site and i want to access a database that I have made that has 3 values, NR which is just an increment to how many rows I have, category which is the page title and intro which is the introduction. layoutdesc= table name Fields= nr, intro, cat For some reason no text displays and I have no idea why.
Modify the code as follows: <?php include("databaseinfo.php"); $query = mysql_query("SELECT intro FROM layoutdesc WHERE cat='$idd'"); while($row = mysql_fetch_array($query, MYSQL_BOTH)) { $description = $row['intro']; echo $description; } ?> Code (markup):
Could you possibly explain why we need the while loop just so I can learn for the future? Also is there an easier way to access the field from a table or is this the only way to get it done?
While loop is needed if you are expecting more than one row. The while-loop will keep iterating through the records and display the data. If there is only one row (or zero row), you can skip the while loop and replace it with the following code: <?php include("databaseinfo.php"); $query = mysql_query("SELECT intro FROM layoutdesc WHERE cat='$idd'"); $row = mysql_fetch_array($query, MYSQL_BOTH); $description = $row['intro']; echo $description; ?> Code (markup): I think there is no easier way to get the data than this.
I got it, so basically if the database ONLY had one row then the command would work but if I have 50 rows for example the while look will cycle through the database and pull out all the match's for the category correct? Or do you mean that if we are expecting more then one match? I have about 27 rows in the database and only 1 will match per query so should I use the second method or the first?