Viewed   247 times

I am new to mysqli and started trying to learn basic things. With respect to this i example (http://php.net/manual/en/mysqli-result.fetch-array.php) i was trying fetch_array. Here is my code.

$sqlGetChartData    =   "SELECT date, ratepersqft, location 
                          FROM ratepersqft
                         WHERE project_id = 1";
$runGetChartData    =   $mysqli->query($sqlGetChartData);

while($rowGetChartData = $runGetChartData->fetch_array(MYSQLI_BOTH))
    $arrGetChartData[]  =   $rowGetChartData;

    print "<pre>";
    print_r($arrGetChartData);
    exit();

Here i am getting this error Call to a member function fetch_array() on a non-object on line next to while condition line. I tried googling it and did not get result for my problem. Hope my question is clear. Thanks in Advance.

 Answers

2

This answer has been written very long time ago and become irrelevant.

Since then I learned the proper solution for this problem and wrote it in this answer. Please navigate there.

Thursday, November 10, 2022
 
5

It is quite simple

$db = new mysqli("host", "user", "password", "db");
$result = $db->query("SELECT * FROM $tableName");
while ($row = $result->fetch_row()) {
    //etc.
}

I find the php.net documentation on mysqli useful for looking up functions as well as codesnippets. This also answers your question php.net/mysqli_fetch_row

Thursday, October 20, 2022
 
mario_f
 
4

MySQL has a different timeout than PHP. You could increase it in php.ini on the line mysql.connect_timeout = 14400. Also increase the default_socket_timeout = 14400

Note that if your PHP setting allow you to do an ini_set, you can also do as follows:

ini_set('mysql.connect_timeout', 14400);
ini_set('default_socket_timeout', 14400);
Thursday, December 22, 2022
 
plutor
 
1

You should put $stmt into you if clause. There is a possiblity that if (false) and still get to your $stmt->close();

Tuesday, October 11, 2022
 
jakobii
 
3

In your code you're mixing both procedural and object-oriented code. Choose either one or the other. Here's how you would solve the problem the procedural way.

$result = mysqli_query($link, $sql, MYSQLI_USE_RESULT)

I'm getting the Call to a member function query() on a non-object when I try to call my function.

That's because the $mysqli object is not declared anywhere (or is it)? Before you can use $mysqli you should first create an instance of mysqli and assign it to your object.

$mysqli = new mysqli("localhost", "my_user", "my_password", "database");

Only then you may call the methods of the mysqli class like $mysqli->query();

The error you made depends probably on two misconceptions:

1) you pasted half of your code from the procedural-style part of the mysqli manual and half from the oop part

2) you assume $mysqli is a global object instantiated with $mysqli_connect();. It is not. You should invoke the constructor with the new keyword if you'd like to use it as an object.

Monday, September 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :