Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i complete this script to output the titles column

Status
Not open for further replies.

kerspink1

Technical User
Mar 21, 2008
5
GB
i have been trying to complete this script to output the titls column

only gives me Resource id #3
at moment

<?php

$host = 'localhost';
$user = 'xxxxxx';
$pass = 'Xxxxxxxx';
mysql_connect($host, $user, $pass);


mysql_select_db($user);
$result = mysql_query("SELECT title FROM books");

if(!$result) die("Query Failed.");

while($row = mysql_fetch_row($result)) {



/*
$row[0] now contains the first column of the current row,
index 1 is the second, etc.
*/
}







?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" <html xmlns=" xml:lang="en" lang="en"> <head>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
<title>first mysql php action</title>
</head>
<body>
<?php echo "$result"
?>
</body>
</html>
 
Code:
while($row = mysql_fetch_row($result)) {
[red]
echo $row[0]. "<br>";
[/red]
   

}

That will basically just echo out the entire title column since your query will only ever return a single column.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thanks vacunita

i thought the echo should go into the body of the html to read out??
 
Yes but you would need to then just store the output somewhere. So basically you'll want to do this then:

Code:
<?php

$host = 'localhost';
$user = 'xxxxxx';
$pass = 'Xxxxxxxx';
mysql_connect($host, $user, $pass);


mysql_select_db($user);
$result = mysql_query("SELECT title FROM books");

if(!$result) die("Query Failed.");
[red]$ouput="";[/red]
while($row = mysql_fetch_row($result)) {
[red]$output.=$row[0] . "<br>";[/red]
    
}




    


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL] <html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en"> <head>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
<title>first mysql php action</title>
</head>
<body>
<?php [red] echo $output;[/red]
?>
</body>
</html>

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top