2

I am learning MySQL and PHP, I have seen that with this code you can insert values in the database:

$sql = "INSERT QUERY..."
$insert = mysqli_query($connection, $sql);

if($insert){
 echo "Insert Worked";
}else{
 echo "Insert error". mysqli_error($connection);
}

So like you see, this should work, but I don't get how. If you are never calling the function mysqli_query, it is just stored in the $insert variable. It's called in the if condition?

Another thing that called my attention is that you get the error of that insert in the connection query, is not that a bad thing? I mean, if you have a lot of errors in different queries it's not too useful, is this the way for looking for errors?.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Risker
  • 358
  • 3
  • 15
  • 4
    I don't understand why you think you are never calling `mysqli_query()`, you are calling it when you create the `$insert` variable. The `if` condition is basically making sure that `$insert` is not `false`, which `mysqli_query()` returns if there is a problem. – GrumpyCrouton Jul 02 '20 at 17:16
  • `mysqli_query($connection, $sql);` is clearly calling mysqli_query. I really don't understand what you're saying / asking? – ADyson Jul 02 '20 at 17:25
  • 1
    `$insert` variable stores the result of execution of `mysqli_query()` function, if function fails it's `FALSE`, the `if` statement checks if it failed or not. – Flash Thunder Jul 02 '20 at 17:31
  • Yes now I get it, I didn't knew that all the things behind the = of the variable was evaluated after being stored. – Risker Jul 02 '20 at 18:04
  • To be clear, you're not storing the function itself in $insert, you're storing the _result_ of the function - i.e. the value that the function returns after its been executed. – ADyson Jul 02 '20 at 19:59

3 Answers3

1

In most languages, including PHP, anything that follows = (assignment operator) is evaluated before it is stored in a given variable. Whatever follows the assignment operator = until the ; is parsed as an expression and that expression may include function calls. Those function calls are evaluated right away and the result of those function calls is used in the rest of the evaluation of the expression.

In your case, mysqli_query() function is called and the rows are inserted then the value returned by mysqli_query() is stored in the variable $insert. It's not called when evaluating the if statement.

About your error handling question, like @O.Jones said, each connection runs 1 query at a time so, if you have multiple queries, you would need to run a query and then check for error and do this in a series.

Also, like @Dharman mentioned, you should use mysqli_error() with care as it is not desirable to use it in production unless you know and have a well-thought use case. Read more about what @Dharman has to say here: Should we ever check for mysqli_connect() errors manually?

Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62
  • You should not be manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Jul 02 '20 at 19:30
  • @Dharman I am by no means suggesting the use of `mysqli_error()` function rather my answer is more focused on how to deal with the group of functions that behave like `mysqli_error()` i.e. return the status of last execution. If you have anything to add, please do. Your links are not relevant in the context of the question. OP didn't ask whether he/she should use `mysqli_error()` or not, he/she asked, **What if there are multiple queries and they have to check status of each? Wouldn't such functions be useless?** – Muhammad Talha Akbar Jul 03 '20 at 09:44
  • @Dharman Have you read the questions posed by the questioner? The questioner posed these questions as a beginner in PHP. I am not advocating whether he/she should check error for each query or not, I have only suggested if they wanted to, what his/her options are. Like I said, if you have anything helpful to add, you can edit my answer. – Muhammad Talha Akbar Jul 03 '20 at 09:55
  • The only thing I have to add is that there should be no `if` statement. If you enable error reporting properly there is absolutely no reason to check the return variable. – Dharman Jul 03 '20 at 09:58
  • @Dharman I know you are right but, are you even applying the context of the question? The OP is definitely a beginner and that `mysqli_error()` is just there for debugging purposes. I can add your links to discourage the use of `mysqli_error` if it's okay with you. – Muhammad Talha Akbar Jul 03 '20 at 10:06
0

You are calling mysqli_query, and storing the result in $insert. Since you're using an insert statement, $insert will contain have TRUE or FALSE, depending on whether the insert statement was successful or not.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Each connection is capable of handling one query at a time; that's the way MySQL and other client-server-style database systems work. The mysqli API designers decided to make errors accessible via the connection object, not the query object. It's a reasonable decision; ordinarily an application program like yours must handle errors before running new queries, so it makes less sense to hold errors on the query object.

An error is signified by the mysqli_query() returned value coming back falsy. If it comes back truthy, you can do more things with it. If you're not sure about "falsy" and "truthy" please look tehm up.

But, these are, at their root, decisions made by API designers doing their best to create useful APIs.

(And, of course, you do use mysqli_query() in your code sample..)

O. Jones
  • 103,626
  • 17
  • 118
  • 172