1

I was wondering how to rewrite/fix the following code.

Basically, I have a database named DB and a table in it called poems. I want to get all the data in the poems table that has a certain ID (i.e. who wrote the poem, the tags it has, its content, when it was created, etc.) and securely store it in an array to later be printed out onto a web page.

The hosting service I have doesn't have support for the mysqlnd driver needed for mysqli_getresult(), so I currently get the error "Call to undefined method mysqli_stmt::get_result()"

$mysqli is a pre-written connection to the database, using new mysqli()

function getpoemdata($mysqli) {
                echo "Function started.";
                if(isset($_GET['poem_id'])) { // Check if poem_id is set in the URL
                    $poemid=$_GET['poem_id']; // Get poem_id set in URL
                    if (!is_int($poemid)) { $poemid = 7; } // Check if poem_id is set an integer
                    if ($poemid <= 0) { $poemid = 7; } //Check if poem_id is positive- If negative, it is changed to positive
                    $stmt = $mysqli->prepare("SELECT * FROM `poems` WHERE `poem_id` = ?");
                    $stmt->bind_param('i', $poemid);
                    $stmt->execute();
                    $result = $stmt->get_result();
                    $returned_data = $returned_data->fetch_array();
                    while($stmt->fetch()){
                        echo $returned_data . '<br />';
                    }
                    $stmt->free_result();
                }
                else { //If user simply types "poemview.php" and not "poemview.php?poem_id="
                echo "error";
            }
    }

Thank you a ton in advance for any help- I'm pretty new to PHP and trying to learn it as I go along- it's just a little overwhelming everytime I try to do something new and need to learn a dozen more functions.

1 Answer 1

3

get_result() was introduced in PHP 5.3.0 and, as mentioned, requires mysqlnd. However, I don't think you need it. You can use fetch() in combination with bind_result to retrieve the data. There may be other ways as well.

Example from the docs:

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";

if ($stmt = $mysqli->prepare($query)) {

    /* execute statement */
    $stmt->execute();

    /* bind result variables */
    $stmt->bind_result($name, $code);

    /* fetch values */
    while ($stmt->fetch()) {
        printf ("%s (%s)\n", $name, $code);
    }

    /* close statement */
    $stmt->close();
}

/* close connection */
$mysqli->close();
?>

An example further down in the docs shows how to bind to an array.

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

Comments

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.