2

I am using this statement to fetch the elements in a column in my db

$result = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);

but I get this error when trying to run it.

Call to undefined method mysqli_stmt::fetchAll();

Is there something I need to add to my php file to include that method or something?

UPDATE 1:

class LoginAPI
{
private $db;

function __construct() 
{
    $this->db = new mysqli('xxx', 'xxx', 'xxx', 'xxx');
    $this->db->autocommit(FALSE);
}

function __destruct() 
{
    $this->db->close();
}

function login() 
{
    if(isset($_POST["lang"]))
    {
        $stmt = $this->db->prepare("SELECT code FROM locations");
        $stmt->execute();
        $stmt->bind_result($code);

        //while($stmt->fetch())
        //{
        //  result[] = "code"=>$code;
        //  break;
        //}
        $codeArr = array();
        $result = $stmt->fetch_all();//|PDO::FETCH_GROUP;




        sendResponse(200, json_encode($result));
        return true;
    }
    sendResponse(400, 'Invalid Request');
    return false;
}
}
linuxer
  • 523
  • 2
  • 4
  • 22
  • 1
    you are using `PDO` or `mysqli`? In any case, you are mixing the two here. – itachi Nov 08 '12 at 20:21
  • 2
    Looks like your mixing PDO and mysqli. If you post how you connect it will show you're making a mysqli connection object, not PDO statement. A mysqli result set object have fetch_all() and not a function fetchAll(). fetchAll() is a function provided by PDOStatement class. See this: http://php.net/manual/en/mysqli-result.fetch-all.php – Ray Nov 08 '12 at 20:19
  • Please check http://us1.php.net/pdostatement.fetchall#refsect1-pdostatement.fetchall-examples – CodeTalk Nov 08 '12 at 20:27
  • Here is how I am connecting to the DB $this->db = new mysqli(...); – linuxer Nov 08 '12 at 20:52

1 Answers1

8

You're using mysqli, and you've got a statement not a result. If you have a mysqli_result you could be using the method fetch_all().

However, as this is a mysqli_stmt, you first need to execute and then use fetch_all() on the result set. See below example

 $stmt = $this->db->prepare("SELECT code FROM locations");
 $stmt->execute();


//grab a result set
$resultSet = $stmt->get_result();

//pull all results as an associative array
$result = $resultSet->fetch_all();

Note: with fetch_all you don't need to bind the result as your original code.

Ray
  • 40,256
  • 21
  • 101
  • 138
  • I changed it to fetch_all() and now I get there error for that method. I am still quite new to all of this, so sorry if I am being stupid – linuxer Nov 08 '12 at 20:53
  • @linuxer Also are you using php 5.3 or an earlier version – Ray Nov 08 '12 at 21:01
  • Call to undefined method mysqli_stmt::fetch_all() and my PHP version is 5.3.15 – linuxer Nov 08 '12 at 21:08
  • @linuxer Ok, now that I look at this I see a lot more going on. Can you put how you create a connection and create your sql statement? It looks like you're using a prepared statement. If so do you call execute? Just paste in your example the whole block of mysqli commands you use – Ray Nov 08 '12 at 21:10