0

This simple little function has been in use and working for quite some time but now seems to be broken after updating to PHP 7.X. Not sure why but it is giving an error of Undefined offset 0 on the row containing $row[0]. I understand the error but not sure why it is there all of a sudden when it had been working. DBConnect() is a custom function used throughout the site to fetch single or multiple rows, count rows, update, insert and delete data and is working everywhere else except here.

What do I do to repair it without, hopefully, modifying DBConnect()?

function ListTableNames($DBname) {

    $rowQuery = "SHOW TABLES FROM $DBname";
    $rowTables = DBConnect($rowQuery,"Multiple", $DBname,"assoc");
    $tableList = [];

    foreach ($rowTables as $row) :
        $tableList[] = $row[0];
    endforeach;

    return $tableList;
}

A portion of the DBConnect() function as used above:

case "Multiple":
    if ($result = $mysqli->query($Query)) :
        $numrowsCat = $result->num_rows;
        if ($numrowsCat >= 1) :
            $result = $mysqli->query($Query);
            if ($selType === "assoc") :
                while($row = $result->fetch_assoc()) :
                    $results_array[] = $row;
                endwhile;
            else :                  
                while($row = $result->fetch_array()) :
                    $results_array[] = $row;
                endwhile;
            endif;
            return $results_array;
        endif;
        $MySQLError = ($mysqli->connect_errno) ? mysqli_error($mysqli) : "";
        $mysqli->close();
        if ($MySQLError) return $MySQLError;
    endif;
break;

A var_dump($rowTables) shows:

array(29) {
  [0]=>
  array(1) {
    ["Tables_in_dbname"]=>
    string(9) "table1"
  }
  [1]=>
  array(1) {
    ["Tables_in_dbname"]=>
    string(9) "table2"
  }
  [2]=>
  array(1) {
    ["Tables_in_dbname"]=>
    string(17) "table3"
  }
  [3]=>
  array(1) {
    ["Tables_in_dbname"]=>
    string(8) "table4"
  }
 }
DonP
  • 725
  • 1
  • 8
  • 27
  • 1
    What does a `var_dump($rowTables);` show? (before the foreach loop) – Paul T. Feb 29 '20 at 22:33
  • Good question: I posted a sample output in the posting. – DonP Feb 29 '20 at 22:49
  • 1
    I noted the `assoc` ... rather than `$row[0]`, should that be: `$row['Tables_in_dbname']` instead? – Paul T. Feb 29 '20 at 23:10
  • Since the function is being used on several sites and the database name is a variable, I had tried that already with `$row["Tables_in_$DBname"]` and got the same error but I just tried it again and it worked! I'm not sure the difference as the code appears to be identical to that in my question other than `"Tables_in_$DBname"` instead of `0`. – DonP Feb 29 '20 at 23:26
  • It's generally a bad idea to write functions like `DBConnect()`. They are much more difficult to read and maintain. – Dharman Mar 01 '20 at 00:18
  • Also it looks like your code is vulnerable to SQL injection. You should be using prepared statements. – Dharman Mar 01 '20 at 00:18
  • Please read [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Mar 01 '20 at 00:18
  • What good is `$selType` if you are using `fetch_array` anyway? This functions itself can accepts such parameter. – Dharman Mar 01 '20 at 00:19
  • Why do you `$mysqli->close();`? – Dharman Mar 01 '20 at 00:20
  • I wrote DBConnect(), which is shared between all my sites, to keep from having to write the same code over and over. Very easy to read and maintain as it’s in one single place, unlike doing the connection each and every time it’s needed. As for prepared statements, I know it needs to be reworked for that and will be soon but I’ll need help doing so. The value of $selType determines whether to use fetch_array or fetch_assoc. As for $mysqli->close();, should the connection not be closed once it’s no longer needed? – DonP Mar 01 '20 at 19:21

0 Answers0