Ghostff
Ghostff

Reputation: 1458

php bind_result return empty result

i have tried most of the similar solutions i found online but non of them seem to worrk this is my PHP

$db_host = "localhost";
$db_name = "nm";
$db_user = "root";
$db_pass = "";
$mysqli_news = new mysqli($db_host, $db_user, $db_pass, $db_name);

function topbanner()
{
    global $mysqli_news; 
    $stmt = $mysqli_news->prepare("SELECT date FROM Ftemp_news_top_pic ORDER BY id DESC LIMIT 3");
    $stmt->execute();
    $stmt->bind_result($id, $image, $link, $source, $date);
    while ($stmt->fetch()){
        return $date;
    }
    $stmt->close();


}

echo topbanner();`

And this a pic of my db enter image description here

Upvotes: 1

Views: 1683

Answers (2)

ArtHare
ArtHare

Reputation: 1836

Really embarrassing possible answer: you thought you typed bind_result, but you actually typed bind_param again. Result: All your "results" end up empty no matter what you do.

My code:

$stmt = $mysqli->prepare("select 5 where 5=?") ;
$stmt->bind_param("i", $value);
$stmt->execute();
$stmt->bind_param($five); // oops!  This should have been bind_result
if($stmt->fetch())
{
  // why is $five completely empty?  This is infuriating...
}

It turns out that bind_param can fail silently in certain situations, and you end up with a bunch of empty variables.

Upvotes: -1

ghostffcode
ghostffcode

Reputation: 93

The mistake came from $stmt->bind_result(); Your MySQL query selects 1 column from the database and you are binding 5 columns which is not possible. Here is something that should work correctly:

$db_host = "localhost";
$db_name = "nm";
$db_user = "root";
$db_pass = "";
$mysqli_news = new mysqli($db_host, $db_user, $db_pass, $db_name);

function topbanner()
{
    global $mysqli_news; 
    $stmt = $mysqli_news->prepare("SELECT date FROM `Ftemp_news_top_pic` ORDER BY `id` DESC LIMIT 3");
    $stmt->execute();
    $stmt->bind_result($date);
    while ($stmt->fetch()){
        echo $date; // echo/return $date column
    }
    $stmt->close();
}

echo topbanner();

Or for the topbanner() if you want to get all data in the Database, use:

function topbanner()
{
    global $mysqli_news; 
    $stmt = $mysqli_news->prepare("SELECT * FROM `Ftemp_news_top_pic` ORDER BY `id` DESC LIMIT 3");
    $stmt->execute();
    $stmt->bind_result($id, $title, $image, $link, $source, $date);
    while ($stmt->fetch()){
        echo $date; //echo whatever column you want
    }
    $stmt->close();
}

Also, know that depending on how you want to make use of the function, when you use "return", you end the function after the first occurrence of the while loop, if you want to get all the available values, you can append the results to the while loop to a variable and return the variable at the end of the function, like below:

function topbanner()
{
    global $mysqli_news; 
    $stmt = $mysqli_news->prepare("SELECT * FROM `Ftemp_news_top_pic` ORDER BY `id` DESC LIMIT 3");
    $stmt->execute();
    $stmt->bind_result($id, $title, $image, $link, $source, $date);
    while ($stmt->fetch()){
        $result .= $date; //Append results to a variable
    }
    return $result;
    $stmt->close();
}

So, with the above code, you can go ahead and echo the function as:

echo $topbanner();

Upvotes: 3

Related Questions