开发者

Prepared Statements with OOP - PHP MySql

开发者 https://www.devze.com 2023-03-11 14:12 出处:网络
Ok, so I have been working a Mysql class. I am making a form that will get information about the user logged in. I have a function with in the class that is supposed to do this but I am having a hard

Ok, so I have been working a Mysql class. I am making a form that will get information about the user logged in. I have a function with in the class that is supposed to do this but I am having a hard time accomplishing this. I am using Prepared Statements. What I want to do is return an array of the data from the database.

Here is the code I have so far:

<?php

require_once('constants.php');

class Mysql {

    private $conn;

    function __construct(){
        $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or 
            die("There was an issue connecting to the database");
    }

    function u开发者_如何学Goser_info($username){
        //query
        $query = "SELECT * FROM users WHERE username = ? LIMIT 1";

        //prepare query and execute
        if($stmt = $this->conn->prepare($query)){
            $stmt->bind_param('s', $username);
            $stmt->execute();

            //WHAT DO I DO HERE

            $stmt->close();
            return $data;
        }
    }
}
?>


After the execute you would do something like this:

/* bind result variables */
$stmt->bind_result($user_id, $user_lastname); // Add all the variables you need

/* fetch value */
$stmt->fetch();

echo 'The user ID is: ' . $user_id;


After you call mysqli_stmt->execute() (which is similar to mysql_query() in that it actually fires off the query), you will either want to call mysqli_stmt->fetch(), which fetches the next.

In place of your comment which indicates some confusion, put this code:

while($row = $sth->fetch())
{
    print_r($row); // Do whatever you want with each row here
}

At each iteration of that loop, you will get one row from the result. If you are querying by a UNIQUE key (which looks like it), you only have to call it once.


You must bind variables to the result fields of your result. fetch only returns a bool when using mysqli.

http://us3.php.net/manual/en/mysqli-stmt.fetch.php

// Bind the result to however many variables are in *
$stmt->bind_result($your, $returned, $fields);

/* fetch values */
while ($stmt->fetch()) {
    // do stuff with $your $returned $fields
}


If you want the results from your PreparedStatement-Query, you'll need to use the bind_result-method. After that, you can use the fetch-method to populate the binded variables.


There is a simple way you can do this just push everything in an array.

//Execute statement
$stmt->execute();
$dataArr = array();
//Fetch result to array
$result = $stmt->get_result();
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
    array_push($dataArr, $row);
}
print_r($dataArr);

$dataArray will hold all things.If you use this method then you don't need to bind result.

0

精彩评论

暂无评论...
验证码 换一张
取 消