0

I'm working on a project where I have a database class with some functions that may be used throughout the project. One of the functions basically executes a query and returns anything that may be a result of that query.

I want to put that return value into an array. This is because I have an error function called within the query function and want to return that as well. This is my query function:

//Execute query
function executeQuery($payload, $database){

    //Create database connection
    $con = connectDB($database);

    //execute query
    $result = mysqli_query($con, $payload);

    //Check for error
    $error = sqlErrorHandling($con);

    //Create return array
    $returnArray = array($error, $result);

    //Return result
    return $returnArray;    

}

I know that the sqlErrorHandling() function works and my connectDB() function works, but this function gives me the error: Cannot use object of type mysqli_result as array

I'm assuming that due to my putting $result in an array. Is there any way to do this?

This is the code that's giving me the error:

$result = executeQuery($databaseQuery, null);

        //Create local variables based off array
        list ($error, $result) = $result;

NOTE: I'm NOT trying to get the value of the rows in with this bit of code. It may very well be used to do so, but this is supposed to be a generic function to execute a query and then allow multiple things to be done with that query. For instance using it in mysqli_num_rows and such like that.

9
  • Where exactly does that error occur, i.e. what line? Commented Aug 9, 2013 at 19:38
  • Where I try to create a variable from the array (using list): $result = executeQuery($databaseQuery, null); //Create local variables based off array list ($error, $result) = $result; Commented Aug 9, 2013 at 19:40
  • My bad I meant to do a line break Commented Aug 9, 2013 at 19:41
  • w3schools.com/php/func_mysqli_fetch_array.asp Commented Aug 9, 2013 at 19:41
  • @Mr.Monshaw That won't work because the result of the query will be used for multiple things such as mysqli_num_rows Commented Aug 9, 2013 at 19:42

1 Answer 1

2

Your approach is all wrong.

First, this function should throw an exception in case of error, not return it
Next, you should connect once, not every time you run a query! And pass connection resource into function.

This is how it should be

function executeQuery($con, $query){

    //execute query
    $result = mysqli_query($con, $query);
    if (!$result) {
        throw new Exception($con->error);
    }

    //Return result
    return $result;
}
$result = executeQuery($con, $databaseQuery);

this way you will have your result along with proper error reporting.

Sign up to request clarification or add additional context in comments.

6 Comments

I see many reasons for a function. Prevents recursive code. I'm working on a rest api and I see no reason in having to type in mysqli_query a bunch of times when I can save a lot of time by just calling one line of code instead of multiple lines.
And that may work but I want to handle my errors since it is an api. Not just throw an exception.
Exceptions are the best way to handle errors, mind you. Though I doubt you going to really handle them.
This is an api that is going to be open source. If the person who uses the api want's to do something with their error than I want to let them do that. That's why I wanted to return the error as well. Not only that but the $error was supposed to be a the error number so I could pass that back to ajax and show a specialized message based of the error number.
Oh, exception is the only way then. This is the standard way "to do something with the error". The only acceptable way
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.