0

I am trying to learn how to use prepared statements in my PHP, to get data out from MySQL database. At the moment I am not getting anything printed out.

I need to have a while loop there is going through my rows in the database. Is that correct?

Am I on the correct way here, or what am I missing? I made some comments in the code, to describe what I am doing.

    <?php

    $stmt = $mysqli->prepare("SELECT * FROM fantasies WHERE headline = ? AND description = ? AND place = ? ORDER BY reg_date DESC LIMIT 3");

                // Execute prepared statement 
                if ($stmt->execute()) {
                    $success = true;
                }

                // Make variables ready
                $head = null;
                $desc = null;
                $plac = null;

                // Bind result to variables
                $stmt->bind_result($head, $desc, $plac);

                while ($stmt->fetch()) {

                    // What should go here?
                    echo "Headline: ".$head."Description: ".$desc."Place: ".$place;
                }
                // Close statement
                $stmt->close();

                // Close connection
                $mysqli->close();   

if($success) {
    echo "Selected Succesfull";
} else {
    echo "Failed: " .  $stmt->error;
  }
}
?>
4
  • use num_rows to check query return result or not Commented Apr 12, 2017 at 9:36
  • you execute twice Commented Apr 12, 2017 at 9:44
  • Thanks for the comments. I have updated my question Commented Apr 12, 2017 at 9:53
  • you didn't use bind_param(); that's why your code failed @KrMa php.net/manual/en/mysqli-stmt.bind-param.php Commented Apr 12, 2017 at 9:57

1 Answer 1

1

That code when executed it should give you an error :

Invalid parameter number: no parameters were bound

You need to bind your parameters as you are using placeholders

$stmt->bind_param("sss", $headline, $description, $place); //missing from your code

where "sss" is the dataType in this case a string and $headline, $description, $place are your variables that you replacing with placeholders

Your code should be

<?php

$stmt = $mysqli->prepare("SELECT * FROM fantasies WHERE headline = ? AND description = ? AND place = ? ORDER BY reg_date DESC LIMIT 3");

//bind parameters
$stmt->bind_param("sss", $headline, $description, $place);

if ($stmt->execute()) {
    $stmt->bind_result($head, $desc, $plac);

    while ($stmt->fetch()) {

        echo "Headline: " . $head . "desc: " . $desc . "Place: " . $plac;
    }
    $stmt->close();
} else {

    echo "error" . $mysqli->error;
}

$mysqli->close();

?>
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the answer. I can see what you mean. I just tried with your code. I am still not getting anything printet out. Could it be my SELECT statement?
do u have these variables $headline, $description, $place?
Yes, I copied your code and tried, but it is still not giving me any output. The columns names in my database is: head, description, place. Actually I am not even getting the "error" message. So it looks like nothing is called.
When I use this query: jsfiddle.net/r447bgr6 I get printed my output correct.
the reason u not getting the results is because the where condition is not met, make sure it's met

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.