Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/42976-solved-another-newb-question/
Share on other sites

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.

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

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.

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 :(

try this :P

 

<?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];
}

}

 

?>

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;
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.