borden0108 Posted September 11, 2011 Share Posted September 11, 2011 hi all I am trying to process a result from a query but cant use a echo in a while statement Here is the code I am trying to get the result from each row in the table and then display them I get the error Parse error: syntax error, unexpected T_ECHO in C:\wamp\www\blackrain\Resources\read.php on line 13 <?php $link = mysql_connect('localhost', 'root', ''); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db("php"); $rs = "SELECT `id`, `title`, `author`, `date`, `imageUrl`, `text` FROM `items` "; $rs_return = mysql_query($rs) or trigger_error($rs . ' has encountered an error: <br />'. mysql_error()); while($obj = mysql_fetch_array($rs_return, MYSQL_ASSOC)) {" <h1>"+ echo $obj['title']; +"</h1> <div id="+"wrapper"+"> <div id="text"> <h4>Posted:"+ echo $obj['author'];+"By:"+ echo $obj['date'] +"</h4> <p class="p3"> <span> <img class="+"alignright"+"src="+ echo $obj['imageUrl'];+"> </img>"+ echo $obj['text'];+"</span></p>" } ?> Link to comment https://forums.phpfreaks.com/topic/246903-while-statement/ Share on other sites More sharing options...
marcelobm Posted September 11, 2011 Share Posted September 11, 2011 Ok, First in PHP you concatenate a string with a dot(.) not a plus(+), Second if you are concatenating a string with variables you don't use the echo in the middle. This is an example how you could do it: $link = mysql_connect ( 'localhost', 'root', '' ); if (! $link) { die ( 'Could not connect: ' . mysql_error () ); } mysql_select_db ( "php" ); $rs = "SELECT `id`, `title`, `author`, `date`, `imageUrl`, `text` FROM `items` "; $rs_return = mysql_query ( $rs ) or trigger_error ( $rs . ' has encountered an error: <br />' . mysql_error () ); while ( $obj = mysql_fetch_array ( $rs_return, MYSQL_ASSOC ) ) { $row = "<h1>"; $row .= $obj ['title']; $row .= "</h1><div id=\"wrapper\"><div id=\"text\"><h4>Posted:"; $row .= $obj ['author']."By:".$obj['date']."</h4><p class=\"p3\"><span><img class=\"alignright\" src=\"".$obj ['imageUrl']."\"></img>"; $row .= $obj ['text']; $row .= "</span></p>"; } also you have to remember to escape " Link to comment https://forums.phpfreaks.com/topic/246903-while-statement/#findComment-1267969 Share on other sites More sharing options...
borden0108 Posted September 11, 2011 Author Share Posted September 11, 2011 how do i print this code to the document? Link to comment https://forums.phpfreaks.com/topic/246903-while-statement/#findComment-1267982 Share on other sites More sharing options...
marcelobm Posted September 11, 2011 Share Posted September 11, 2011 You can do echo $row; at the end of each cycle while ( $obj = mysql_fetch_array ( $rs_return, MYSQL_ASSOC ) ) { $row = "<h1>"; $row .= $obj ['title']; $row .= "</h1><div id=\"wrapper\"><div id=\"text\"><h4>Posted:"; $row .= $obj ['author']."By:".$obj['date']."</h4><p class=\"p3\"><span><img class=\"alignright\" src=\"".$obj ['imageUrl']."\"></img>"; $row .= $obj ['text']; $row .= "</span></p>"; echo $row; } Link to comment https://forums.phpfreaks.com/topic/246903-while-statement/#findComment-1267985 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.