Jump to content

Archived

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

Jewel

Help! with While statement Please

Recommended Posts


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\" >}";

?>

]

Share this post


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

?>

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

×

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.