Steppio Posted March 16, 2007 Share Posted March 16, 2007 Hi there, before i start with this no doubt laughable issue to real php programmers i will explain, not only am i a total newb to this language (though i must say what i've seen is mighty impressive) i am also an idiot. If you could help me with this problem i would be very grateful. The problem is that i have a main page that i want to display the last 5 entrys from my news database in descending order of thier ID, the database layout being ID, Newsuser, Newsdate, Newshead and News. I've tried the following code to try and connect to the database and display the ID's in descending order but i always get the same error message, printed below: function get_news_id() { $conn = db_connect(); $result = $conn->query("SELECT newshead FROM news ORDER BY id desc"); if (!$result) throw new Exception('Could not execute query'); echo $result; } Catchable fatal error: Object of class mysqli_result could not be converted to string It's been doing my head in for a while now, if you could help me out in anyway i'd be mega happy and grateful and generally chuffed. Thanks for reading. Ste Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/ Share on other sites More sharing options...
per1os Posted March 16, 2007 Share Posted March 16, 2007 i am also an idiot. Glad to see some honesty go along with the "I am a newbie at php" statement =) function get_news_id() { $conn = db_connect(); $result = $conn->query("SELECT newshead FROM news ORDER BY id desc LIMIT 5"); if (!$result) throw new Exception('Could not execute query'); $i=0; while($row = mysqli_fetch_array($result)) { $return[$i++] = $row['newshead']; } echo $return; } This will return an array of newsheads. Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-208793 Share on other sites More sharing options...
Steppio Posted March 16, 2007 Author Share Posted March 16, 2007 Thank you very much Frost110, i'll try it as soon as i get home, most appreciated! The whole of the problem is that i have a function that calls the decoration of the <Table> i have set up to display the news (get_news($ID)), and i would like to take the last 5 id's of the ID field in the news table, turn each of the 5 into a variable and pass each variable to a seperate call to the function, as to display the last 5 news entrys individually in the way i'd like the tables to look. I don't know if that makes any sense, anyway thank you very much for your help, it is most appreciated! Ste Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-208825 Share on other sites More sharing options...
Steppio Posted March 18, 2007 Author Share Posted March 18, 2007 I keep having the same problem with this, instead of returning the results for the mysql query (i.e. in this case an array of the last five newsheads based on ID number) it just returns the word 'Array'. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-209996 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 try something like this: <?php $newsheads = get_news_id(); $i = 0; while($i < count($newsheads)){ //output newsheads echo $newsheads[$i]."<br />"; $i++; } function get_news_id() { $conn = db_connect(); $result = $conn->query("SELECT newshead FROM news ORDER BY id desc LIMIT 5"); if (!$result){ throw new Exception('Could not execute query'); } $i=0; while($row = mysqli_fetch_array($result)) { $return[$i] = $row['newshead']; $i++; } echo $return; } ?> ...i didn't test it, but it looks right let me know how it works out. Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-210003 Share on other sites More sharing options...
Steppio Posted March 18, 2007 Author Share Posted March 18, 2007 Again the result is just the word 'Array'. Most annoying. Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-210014 Share on other sites More sharing options...
interpim Posted March 18, 2007 Share Posted March 18, 2007 would this work? while(mysqli_fetch_assoc($result)) Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-210016 Share on other sites More sharing options...
Steppio Posted March 18, 2007 Author Share Posted March 18, 2007 Again the result is just 'Array', im sure its something very simple that i've forgotten to do but i cant find it, the database connection is ok as i use it in another function to input the news articles and that works fine, and the table is definatly called news with fields id, newsdate, newsuser, newshead and news. It's all a mystery to me Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-210020 Share on other sites More sharing options...
interpim Posted March 18, 2007 Share Posted March 18, 2007 try this <?php $newsheads = get_news_id(); $i = 0; while($i < count($newsheads)){ //output newsheads echo $newsheads[$i]."<br />"; $i++; } function get_news_id() { $conn = db_connect(); $result = $conn->query("SELECT newshead FROM news ORDER BY id desc LIMIT 5"); if (!$result){ throw new Exception('Could not execute query'); } $i=0; while($row = mysqli_fetch_array($result)) { $return[$i] = $row['newshead']; $i++; echo $return[$i]; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-210026 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 ok i think i got it, try : <?php $newsheads = get_news_id(); $i = 0; while($i < count($newsheads)){ //output newsheads echo $newsheads[$i]."<br />"; $i++; } function get_news_id() { $conn = db_connect(); $result = $conn->query("SELECT newshead FROM news ORDER BY id desc LIMIT 5"); if (!$result){ throw new Exception('Could not execute query'); } $i=0; while($row = mysqli_fetch_array($result)) { $newsheads[$i] = $row['newshead']; $i++; } return $newsheads; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-210028 Share on other sites More sharing options...
Steppio Posted March 18, 2007 Author Share Posted March 18, 2007 BINGO! Thanks very much for your help cmgmyr, most kind of you, that issue has been a source of stress for me for quite some time, i'm most appreciative! Thanks again to everybody who helped me! Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-210037 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 not a problem...i should have seen that earlier myself. The function was echoing the result instead of returning it to the top where it was called. Glad I could help! Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-210040 Share on other sites More sharing options...
Steppio Posted March 19, 2007 Author Share Posted March 19, 2007 Sorry to bring this back up again but how would i take all 5 outputs of this code and then hold them in 5 seperate variables? i tried to use explode with them but its getting a tad complicated for me now :/ Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-210488 Share on other sites More sharing options...
cmgmyr Posted March 19, 2007 Share Posted March 19, 2007 well you do have 5 variables... $newsheads[0] $newsheads[1] $newsheads[2] $newsheads[3] $newsheads[4] Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-210491 Share on other sites More sharing options...
Steppio Posted March 19, 2007 Author Share Posted March 19, 2007 you know sometimes i excel myself in stupidity, occams razor prevails again! Thanks again cmgmyr, your a sanity saviour. Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-210496 Share on other sites More sharing options...
cmgmyr Posted March 19, 2007 Share Posted March 19, 2007 no problem Quote Link to comment https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/#findComment-210497 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.