M
M
MRcracker2020-04-14 09:55:06
PHP
MRcracker, 2020-04-14 09:55:06

How to get data from mysql?

There is a task to deduce from a database of users with a certain name. I am getting the error Couldn't fetch mysqli in. Tell me what I did wrong?

$name = "SELECT * FROM users WHERE name == 'alex'";
$result3 = mysqli_query($conn, $name);

$b = array();

if(mysqli_num_rows($result3) > 0) {
  while($row = mysqli_fetch_assoc($result2)) {
    $b[] = $row;
  }
} else {
  echo "0 results";
}

print_r($b);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FanatPHP, 2020-04-14
@MRcracker

The Couldn't fetch mysqli error means that you have already closed the connection to the database for some reason
Well, to answer the question from the title and rewrite all this cave horror to normal PHP

$b = $conn->query("SELECT * FROM users WHERE name = 'alex'")->fetch_all(MYSQLI_ASSOC);
if($b) {
    print_r($b);
} else {
  echo "0 results";
}

D
Dr. Bacon, 2020-04-14
@bacon

== vs =

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question