-3

I have a very simple query that works in both phpMyAdmin and in the PHP page I'm using it on, to update a table based on a simple HTML form:

UPDATE customers SET customer_name='$name', customer_email='$email', customer_tel='$tel' WHERE customer_id = $id LIMIT 1

This updates the table perfectly - I can see that in phpMyAdmin, and I have an if statement checking the number of affected rows that reports no problems, but I still get:

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

Suggesting that the query is incorrect. What gives?

Paul Lo
  • 6,032
  • 6
  • 31
  • 36
ElendilTheTall
  • 1,344
  • 15
  • 23
  • could you point out the code where u are using mysqli_fetch_array()? – Bhadra Dec 16 '13 at 15:59
  • Please add the whole code as the likelihood is that it has nothing to do with this query but a function you have used. – The Humble Rat Dec 16 '13 at 15:59
  • What does `mysqli_query` tell you? You might want to look at a prepared statement, too, as someone with the name of `O'Connor` will break your query. – andrewsi Dec 16 '13 at 15:59
  • 1
    Your query works, but you have fundamental lack of knowledge. UPDATE returns no data. How can you fetch a row from something that returns no data? – N.B. Dec 16 '13 at 16:01
  • @N.B. We're all here to learn, aren't we? He posted enough code to easily see what was wrong. – Josh Harrison Dec 16 '13 at 16:03
  • @JoshHarrison - we are here to learn, and all of us have "silly" questions along the road. But some errors are easily resolvable using your own resources. Resorting to SO for any error you encounter won't make you learn at all. Using your own brain and resources to resolve an issue that isn't working but should will make you learn. That's all there is to it really. – N.B. Dec 16 '13 at 16:08
  • please post your code that does the updation and causes the stated error. – dev1234 Dec 16 '13 at 16:09

3 Answers3

4

An UPDATE query doesn't return records.

Do a SELECT query after your UPDATE and iterate over those results instead.

Josh Harrison
  • 5,927
  • 1
  • 30
  • 44
2

Update queries cause mysqli_query to return true or false based on success or failure of the update. You shouldn't be calling mysqli_fetch_array on it.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • In future, make sure to `var_dump` anything that is reported as unexpected. This should have shown you `bool(true)`, which would have helped you solve the problem ;) – Niet the Dark Absol Dec 16 '13 at 16:07
0

You must pass the result set to mysqli_fetch_array();

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

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}


if ($result = $mysqli->query("UPDATE customers SET customer_name='$name', customer_email='$email', customer_tel='$tel' WHERE customer_id = $id")) {
    // if success  do something here
}

in your case, You shouldn't be calling mysqli_fetch_array()

check this answer : mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in

Community
  • 1
  • 1
dev1234
  • 5,376
  • 15
  • 56
  • 115