Hi there everyone, long time, no type!

I'm trying to help someone regarding an issue with a script. The problem I'm having is that the script will just blindly continue executing regardless of the fact that it's doing queries on tables and rows that don't exist. I'm trying to find out where the problems arise so I'd like to print these errors to screen but I've never worked with statements created like this(I think they're called prepared statements). Could someone tell me how I would modify this function to report the sql error to screen and kill the script?

    // Insert into Database
    function dbquery($sql, $returnresult = true)
    {
        $conn = new mysqli($GLOBALS['mysql_host'], $GLOBALS['mysql_user'], $GLOBALS['mysql_pass'], $GLOBALS['mysql_db']);
        if ($conn->connect_errno) {
            error_log('MySQL could not connect: ' . $conn->connect_error);
            return $conn->connect_error;
        }

    $return = array();

    $result = mysqli_query($conn, $sql);
    if ($returnresult) {
        if (mysqli_num_rows($result) != 0) {
            while ($r = $result->fetch_assoc()) {
                array_push($return, $r);
            }
        } else {
            $return = array();
        }

    } else {
        $return = array();
    }

    return $return;
}

Any help would be greatly appreciated. Thanks for your time!

    There's nothing here that creates these offending queries, but the mysqli_query call would be handled the usual way whether they were prepared or not (if the query fails, the function returns false, and using the MySQLi error functions on the connection will say why).

    Some of your confusion might be because you've got a mixture of the object-oriented and procedural interfaces (a $conn->connect_error that could have been written mysqli_connection_error() versus a mysqli_query($conn,$sql) that could have been written $conn->query($sql)). Pick one and be consistent about it.

      See this reply - https://board.phpbuilder.com/d/10402137-my-sqli-num-rows-problem/5 for how to add error handling for the mysqli extension.

      BTW - you are NOT using prepared queries, as the mysqli query() method executes a normal query. To use prepared queries, you would use the msyqli prepare() method, followed by the mysqli_statement bind_param() and execute() methods.

      Also, you shouldn't make a database connection in your function, since php will destroy that connection when the function call ends, but the actual connection won't be immediately closed, so each call to that function within one instance of your script will consume and tie up multiple connections. Your main code should make one database connection and supply that as a call time parameter to any function that needs the connection.

        Thank you both for your help, gents. I was taking a WAG on the type of statement as I've never seen it done like that before and to be honest, the whole script is done in a manner I don't understand. I'm finding it impossible to make even the smallest of modifications to it because I can't figure out how things are happening in it.

        Live and learn, I suppose. If it doesn't break me I'll probably learn something from it 🙂

        Thanks for your help!

          got a mixture of the object-oriented and procedural interfaces

          //redundancy_or_saying_the_same_thing_twice.php
          
          /* each statement is repeated, the first time with the "procedural" method and the second time in the 'object-oriented' style ... */
          
          $conn  = mysqli_connect($host, $user, $pass, $db_name);
          $conn2 = new Mysqli($host, $user, $pass, $db_name);
          
          $sql = "select * from mytable;";  // we'll just say THIS once ;)
          
          $result  = mysqli_query($conn, $sql); // really, why am I being redundant?  I'm just wasting
          $result2 = $conn2->query($sql);       // resources having 2 DB connections open.  Did I cause
                                                      // the invention of the Singleton pattern? :D
          
          if (!$result)  echo "No result from mysqli_query, server said: " . mysqli_error($conn);
          if (!$result2) echo "No result from \$conn->query, server said: " . $conn->error;
          
          $row_count  = mysqli_num_rows($result); // NOTE: written like a function
          $row_count2 = $result->num_rows;         // *not* a function! This bites me - 2x/month on avg(?)
          
          while ($row  = mysqli_fetch_assoc($result)) { do_something(); }
          while ($row2 = $result->fetch_assoc() { do_something(); }       // use *same* result object ;-)
                                                                                                             // could've used $result2 !??!

          Can you dig it and get what I'm saying?
          I expect you can get it and have confidence that you will understand!
          I think I shall soon have to start saying things only once, and desist from repeating myself!
          Your friendly and nice fellow-programmer and collegial wage-earner ... DaleCoSP 😃

            Write a Reply...