-2

Okay, I took out all the functions to eliminate out of scope variables and am now getting an error on the last line. " Call to undefined method mysqli_result::fetch_all()"

    $conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM users";
$result = $conn->query($sql);

$rows = $result->fetch_all(MYSQLI_ASSOC);
thinkofacard
  • 491
  • 1
  • 6
  • 19

2 Answers2

0

Any idea why I'm getting this error when the $result = $conn->query($sql); tries to run??

You should read this part of documentation.

You can't use $conn variable inside function like that. All variables used inside function must be either passed as arguments or declared in function body.

tldr: Just move $conn = test_connect(); into body of function test.

Igor Denisenko
  • 254
  • 2
  • 7
0

Problem is in below code:

function test() {$sql = "SELECT * FROM users"; $result = $conn->query($sql); return $result->fetch_all(MYSQLI_ASSOC); }

You should define $conn object as global in test function or pass this in function.

Try below code:

function test() { global $conn; $sql = "SELECT * FROM users"; $result = $conn->query($sql); return $result->fetch_all(MYSQLI_ASSOC); }

OR

function test($conn) {$sql = "SELECT * FROM users"; $result = $conn->query($sql); return $result->fetch_all(MYSQLI_ASSOC); }

amarjeet kumar
  • 345
  • 1
  • 10