Unseeeen Posted June 25, 2006 Share Posted June 25, 2006 Well, I haven't done anything with php or sql in a while and I just had a simple question...I'm making a very simple news script and right now all I want to do is insert & display results to the database.Here's my code so far. [code]<?$frontpagelimit = "5";$dbconnect = mysql_connect ("localhost", "user", "pass") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("db");$query = "SELECT * FROM news ORDER BY id DESC LIMIT $frontpagelimit";mysql_query($query);$row = mysql_fetch_object($query);echo $row->newsbody; ?>[/code]What I'm trying to do at this stage is get the newsbody that has already been inserted. I get a [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource[/quote]on line 7 error. From what I remember...this should be right. What's my problem? And how, once I've grabbed everything, display more than one news insert at a time? I'm thinking with maybe an array, but how would I do that?Thanks. Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/ Share on other sites More sharing options...
.josh Posted June 25, 2006 Share Posted June 25, 2006 as to your first question, you are trying to create an object with the query string, not the result. you need to assign the query result to a variable and then create the object from the result variable. example:[code]<?$frontpagelimit = "5";$dbconnect = mysql_connect ("localhost", "user", "pass") or die ('I cannot connect to the database because: ' . mysql_error());mysql_select_db ("db");$query = "SELECT * FROM news ORDER BY id DESC LIMIT $frontpagelimit";$result = mysql_query($query);$row = mysql_fetch_object($result);echo $row->newsbody;?>[/code]as to your 2nd question, you would just wrap a while statement around it, like so:[code]while ($row = mysql_fetch_object($result)) { echo $row->newsbody;}[/code] Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49299 Share on other sites More sharing options...
Unseeeen Posted June 25, 2006 Author Share Posted June 25, 2006 Hey thanks man, I appreciate it. Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49305 Share on other sites More sharing options...
paul2463 Posted June 25, 2006 Share Posted June 25, 2006 Hisomething that I found out as well that may be of use in this problem is that whenever I have used a variable in a PHP/SQL statement that variable has to be wrapped in single quotes[code]$query = "SELECT * FROM news ORDER BY id DESC LIMIT '$frontpagelimit'";[/code] Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49326 Share on other sites More sharing options...
Unseeeen Posted June 25, 2006 Author Share Posted June 25, 2006 I have another question...My code, will only display one result from the database, but I'm trying to get it to display 5. Here's my code...once again, I dont see what's wrong with it.[code]<?include 'functions.php';$query = "SELECT * FROM news ORDER BY id DESC LIMIT $frontpagelimit";$result = mysql_query($query);$row = mysql_fetch_object($result);?><center><table class="news"><tr><td><b>Title</b>:<i><? echo $row->title; ?></i></td></tr><tr><td><? echo $row->newsbody;?></td></tr><tr><td><b>Posted by:</b> <i><? echo $row->username; ?></i> <b>on</b> <i><? echo $row->date; ?></i> <b>at</b> <i><? echo $row->time; ?></i></td></tr></table>[/code] Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49470 Share on other sites More sharing options...
dptr1988 Posted June 25, 2006 Share Posted June 25, 2006 Try this:[code]while ($row = mysql_fetch_assoc($result)){ // do your operations with $row}[/code] Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49471 Share on other sites More sharing options...
Unseeeen Posted June 25, 2006 Author Share Posted June 25, 2006 Thanks, but I don't think I quite understand. I tried:[code]$query = "SELECT * FROM news ORDER BY id DESC LIMIT $frontpagelimit";$result = mysql_query($query);$row = mysql_fetch_object($result);while ($row = mysql_fetch_assoc($result)){?><center><table class="news"><tr><td><b>Title</b>:<i><? echo $row->title; ?></i></td></tr><tr><td><? echo $row->newsbody;?></td></tr><tr><td><b>Posted by:</b> <i><? echo $row->username; ?></i> <b>on</b> <i>Jan 1st, 2006</i> <b>at</b> <i><? echo $row->time; ?></i></td></tr></table><?}?>[/code]But nothing shows up? What's wrong? Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49477 Share on other sites More sharing options...
dptr1988 Posted June 25, 2006 Share Posted June 25, 2006 Did you check the HTML code that you got (View Source in your browser)?Does you PHP installation allow the short tags '<?' and '?>' ?Is the page completly empty? or just the part that should be the query result? Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49481 Share on other sites More sharing options...
Unseeeen Posted June 25, 2006 Author Share Posted June 25, 2006 Yes and yes. The part where the query result should be showing up is empty. Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49482 Share on other sites More sharing options...
scheols Posted June 25, 2006 Share Posted June 25, 2006 RobX can you show me how it looks i always wanted to use thsi featuer u got msn? Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49483 Share on other sites More sharing options...
Unseeeen Posted June 25, 2006 Author Share Posted June 25, 2006 Yeah I can show you, but only after I finish it up. Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49485 Share on other sites More sharing options...
Unseeeen Posted June 26, 2006 Author Share Posted June 26, 2006 Can anyone see what's possibly wrong? I've been stuck for a while now. Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49510 Share on other sites More sharing options...
trq Posted June 26, 2006 Share Posted June 26, 2006 Your calling mysql_fetch_assoc in your while, but your code expects an object. Try...[code]<?php$query = "SELECT * FROM news ORDER BY id DESC LIMIT $frontpagelimit";if ($result = mysql_query($query)) { while ($row = mysql_fetch_object($result)) {?> <center><table class="news"> <tr><td><b>Title</b>:<i><? echo $row->title; ?></i></td></tr> <tr><td><? echo $row->newsbody;?></td></tr> <tr><td><b>Posted by:</b> <i><? echo $row->username; ?></i> <b>on</b> <i>Jan 1st, 2006</i> <b>at</b> <i><? echo $row->time; ?></i></td></tr> </table><?php }}?>[/code]I also added a little error checking around your result.PS: Get out of the habit right NOW of using short <? php tags. Use only <?php or your heading for trouble. Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49512 Share on other sites More sharing options...
Unseeeen Posted June 26, 2006 Author Share Posted June 26, 2006 Bingo!Thanks a lot. Also, how common is it for php short tags to NOT be useable? Just seems like all the hosts I've been on have had short tags enabled. Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49521 Share on other sites More sharing options...
.josh Posted June 26, 2006 Share Posted June 26, 2006 i answered your 2nd question in my very first post. dunno why you went and asked it again... Link to comment https://forums.phpfreaks.com/topic/12854-grabbing-results-from-sql-db/#findComment-49533 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.