renfley Posted August 23, 2011 Share Posted August 23, 2011 hey just curious if i could pull this off some how <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $result = mysql_query("SELECT title FROM table1"); foreach($result) { ?> <div><?php echo title; ?> </div> <?php mysql_close($con); ?> Basicly im trying to query the database and foreach result display it within divs? ive been trying to figure it out for a while and im stuck any help would be awsome thanks Quote Link to comment https://forums.phpfreaks.com/topic/245534-display-help-with-php-mysql/ Share on other sites More sharing options...
dodgeitorelse Posted August 23, 2011 Share Posted August 23, 2011 Are you trying to get all table names that are in the database or is there actually a database called database with a table called table1 with a column named title? Quote Link to comment https://forums.phpfreaks.com/topic/245534-display-help-with-php-mysql/#findComment-1261093 Share on other sites More sharing options...
QuickOldCar Posted August 23, 2011 Share Posted August 23, 2011 http://php.net/manual/en/function.mysql-fetch-row.php <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $result = mysql_query("SELECT title FROM table1"); while($row = mysql_fetch_array($result)){ $title = $row['title']; echo "<div>".$title."</div>"; } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/245534-display-help-with-php-mysql/#findComment-1261099 Share on other sites More sharing options...
renfley Posted August 23, 2011 Author Share Posted August 23, 2011 iwas thinking more something like $this is the qurey above foreach($result) { ?> <div> <p>this is the title of the row</p><?php echo title; ?> </div> this way it will deisplay every row and give me more control over the content? Quote Link to comment https://forums.phpfreaks.com/topic/245534-display-help-with-php-mysql/#findComment-1261101 Share on other sites More sharing options...
QuickOldCar Posted August 23, 2011 Share Posted August 23, 2011 you never close your foreach loop, title should be a variable...$title, need to associate the row value to a variable. What kind of control are you looking for, while can do the same as foreach. Quote Link to comment https://forums.phpfreaks.com/topic/245534-display-help-with-php-mysql/#findComment-1261102 Share on other sites More sharing options...
renfley Posted August 23, 2011 Author Share Posted August 23, 2011 im trying to avoid this echo <div></div> echo <div></div> echo <div></div> echo <div></div> echo <div></div> echo <div></div> echo <div></div> echo <div></div> echo <div></div> echo <div></div> echo <div></div> echo <div></div> echo <div></div> echo <div></div> lol Quote Link to comment https://forums.phpfreaks.com/topic/245534-display-help-with-php-mysql/#findComment-1261108 Share on other sites More sharing options...
QuickOldCar Posted August 23, 2011 Share Posted August 23, 2011 if($title != ""){ echo "<div>$title</div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/245534-display-help-with-php-mysql/#findComment-1261115 Share on other sites More sharing options...
Psycho Posted August 23, 2011 Share Posted August 23, 2011 @renfley, You were trying to use a foreach loop on the reference ID for a database result set. You need to create a loop (typically a while() loop) to extract each record one at a time and then display/process the data however you want. QuickOldCar gave you a solution in Reply #2. I *think* your reply, in Reply #5, was to prevent having to have multiple echo statemetns - one for each record rather than having empty values displayed. Unless, of course, you have empty values in your database. @QuickOldCa; Why define a variable $title just to use it one time? $title = $row['title']; echo "<div>".$title."</div>"; This would be easier $title = $row['title']; echo "<div>{$row['title']}</div>"; Anyway, here is what I would do (based off of QuickOldCar's script) //Connect to Database $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); //Create and run query $query = "SELECT title FROM table1"; $result = mysql_query($query) or die("Query:<br>{$query}<br>Error:<br>" . mysql_error()); //Output results while($row = mysql_fetch_assoc($result)) { echo "<div>{$row['title']}</div>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/245534-display-help-with-php-mysql/#findComment-1261119 Share on other sites More sharing options...
QuickOldCar Posted August 23, 2011 Share Posted August 23, 2011 @QuickOldCa; Why define a variable $title just to use it one time? Showing where it came from, maybe if wanted to modify it in some way first. Quote Link to comment https://forums.phpfreaks.com/topic/245534-display-help-with-php-mysql/#findComment-1261122 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.