Jump to content


Photo

Help! with While statement Please


  • Please log in to reply
3 replies to this topic

#1 Jewel

Jewel
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 September 2006 - 11:53 PM


I have a short and simple script which is supposed to query my database for film titles and display them.  At the moment it will only display the first title.  I have checked and double checked but I can't see anything wrong with the code, can anyone tell me what I'm doing wrong please?

The database only has the one field (title) and contains 11 titles at the moment.  The code is as follows:

<?
//set up table and database names
$db_name = "dynamic";
$table_name = "Kubrick";

//connect to server and select database
$connection = @mysql_connect("localhost","jimmy","12345") or die(mysql_error());

$db = @mysql_select_db($db_name,$connection) or die(mysql_error());

//build and issue query
$sql ="SELECT * FROM $table_name ORDER by title";

//store the result of the query
$result = @mysql_query($sql,$connection)or die(mysql_error());

//get results for display
while ($row = mysql_fetch_array($result)) {
$title = $row['title'];
}
?>
Kubrick [
<? echo "($title) {< KubrickMovie \"$title\" >}";

?>

]


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 September 2006 - 11:57 PM

Put you echo statement inside the while loop. SO chnage this:
//get results for display
while ($row = mysql_fetch_array($result)) {
   $title = $row['title'];
}
?>
Kubrick [
<? echo "($title) {< KubrickMovie \"$title\" >}";

?>

]
To this:
//get results for display
while ($row = mysql_fetch_array($result)) {
   $title = $row['title'];
   echo 'Kubrick [ ' . $title . "]<br />\n";
}


#3 pgsjoe

pgsjoe
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 02 September 2006 - 12:00 AM

damn! finally something I'd be able to answer and someone else beats me to it.

#4 Jewel

Jewel
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 02 September 2006 - 12:11 AM


Thank you for that, It works fine now! I thought that I'd tried that earlier, but obviously I did something not quite right. Thanks again.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users