Skip to main content
added 293 characters in body
Source Link
Your Common Sense
  • 9.1k
  • 1
  • 22
  • 51

First of all, that's a very good idea to create such a function. It says you are a programmer in your heart. Sadly, but most PHP users never come to the idea of such an automation, writing thousands of repeated lines of code over and over again.

What could be criticized about your code is already pretty much covered in the other answer. However, the solution offered there is still far from being optimal.

First of all, the error reporting is absolutely flawed in both cases. Returning an array with error information instead of the actual query result is absolutely unacceptable. It will lead to numerous errors and confusions in your code. Errors must be thrown, not returned. For mysqli it's especially simple because it can throw exceptions by itself. Check out my article on PHP error reporting principles

Next, returning the mysqli result may cause an error if you are running a DML query. So the only proper return value would be a mysqli statement.

Using "s" for all data types is a very smart move, it will serve you 999 times out of 1000. However, adding a possibility to set the types explicitly is a good idea anyway.

Taking all the above into consideration, I wrote such a function myself, a Mysqli helper function:

function prepared_query($mysqli, $sql, $params = [], $types = "")
{
    $types = $types ?: str_repeat("s", count($params));
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    return $stmt;
}

As you can see, it is not only much simpler but also much more flexible. Note the examples section in the article linked above. As you can see, I tested this function with many query types and return values.

On a side note, the proper mysqli connection is a bit more complex than just a single line of code.

First of all, that's a very good idea to create such a function. It says you are a programmer in your heart. Sadly, but most PHP users never come to the idea of such an automation, writing thousands of repeated lines of code over and over again.

What could be criticized about your code is already pretty much covered in the other answer. However, the solution offered there is still far from being optimal.

First of all, the error reporting is absolutely flawed in both cases. Returning an array with error information instead of the actual query result is absolutely unacceptable. It will lead to numerous errors and confusions in your code. Errors must be thrown, not returned. For mysqli it's especially simple because it can throw exceptions by itself.

Next, returning the mysqli result may cause an error if you are running a DML query. So the only proper return value would be a mysqli statement.

Using "s" for all data types is a very smart move, it will serve you 999 times out of 1000. However, adding a possibility to set the types explicitly is a good idea anyway.

Taking all the above into consideration, I wrote such a function myself, a Mysqli helper function:

function prepared_query($mysqli, $sql, $params = [], $types = "")
{
    $types = $types ?: str_repeat("s", count($params));
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    return $stmt;
}

As you can see, it is not only much simpler but also much more flexible. Note the examples section. As you can see, I tested this function with many query types and return values.

First of all, that's a very good idea to create such a function. It says you are a programmer in your heart. Sadly, but most PHP users never come to the idea of such an automation, writing thousands of repeated lines of code over and over again.

What could be criticized about your code is already pretty much covered in the other answer. However, the solution offered there is still far from being optimal.

First of all, the error reporting is absolutely flawed in both cases. Returning an array with error information instead of the actual query result is absolutely unacceptable. It will lead to numerous errors and confusions in your code. Errors must be thrown, not returned. For mysqli it's especially simple because it can throw exceptions by itself. Check out my article on PHP error reporting principles

Next, returning the mysqli result may cause an error if you are running a DML query. So the only proper return value would be a mysqli statement.

Using "s" for all data types is a very smart move, it will serve you 999 times out of 1000. However, adding a possibility to set the types explicitly is a good idea anyway.

Taking all the above into consideration, I wrote such a function myself, a Mysqli helper function:

function prepared_query($mysqli, $sql, $params, $types = "")
{
    $types = $types ?: str_repeat("s", count($params));
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    return $stmt;
}

As you can see, it is not only much simpler but also much more flexible. Note the examples section in the article linked above. As you can see, I tested this function with many query types and return values.

On a side note, the proper mysqli connection is a bit more complex than just a single line of code.

Source Link
Your Common Sense
  • 9.1k
  • 1
  • 22
  • 51

First of all, that's a very good idea to create such a function. It says you are a programmer in your heart. Sadly, but most PHP users never come to the idea of such an automation, writing thousands of repeated lines of code over and over again.

What could be criticized about your code is already pretty much covered in the other answer. However, the solution offered there is still far from being optimal.

First of all, the error reporting is absolutely flawed in both cases. Returning an array with error information instead of the actual query result is absolutely unacceptable. It will lead to numerous errors and confusions in your code. Errors must be thrown, not returned. For mysqli it's especially simple because it can throw exceptions by itself.

Next, returning the mysqli result may cause an error if you are running a DML query. So the only proper return value would be a mysqli statement.

Using "s" for all data types is a very smart move, it will serve you 999 times out of 1000. However, adding a possibility to set the types explicitly is a good idea anyway.

Taking all the above into consideration, I wrote such a function myself, a Mysqli helper function:

function prepared_query($mysqli, $sql, $params = [], $types = "")
{
    $types = $types ?: str_repeat("s", count($params));
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    return $stmt;
}

As you can see, it is not only much simpler but also much more flexible. Note the examples section. As you can see, I tested this function with many query types and return values.