user1978550
user1978550

Reputation:

mysqli query not returning any result or error php code

I have a data entry php code. I am allowing the user to select a table to be displayed on a separate page for easy access. I can get the code to insert the selected table into the "maintable" table, but I can't get any information out of that table. The insert table file won't even get the current table name from it even though on that same page I am inserting into that database. So I figure if I can fix this problem, I can fix that one, too. The maintable table has just one column with one record in one of my other databases. I am doing this so that the user doesn't have access to this directly since they can see all of the other tables in a different database.

The data-entry-header.php file has my connect statements, $connect being the one I am using for this database connection. What I get as a result is my main-form.php file with no table name. So it isn't throwing any errors, it's just not getting the table name. I know my connect statement and table/column names are correct because I have used the same one for the insert statement.

include 'data-entry-header.php';
    $keys = array();

    /* Query for Main Table Value */
    $string = 'SELECT TableName FROM maintable';
    $resultMain = mysqli_query($connect, $string) or die(mysqli_error($link));
    while ($rowMain = mysqli_fetch_row($resultMain)){
        $table = $rowMain[0];
    }

    /* Display Message or Table */
    if ($table = NULL || $table = "" ){
        echo 'No Main Table has been set. Go to the <a href="set-main-table.php">Set Main Table</a> page to select a Main Table.';
    }
    else {
        include 'main-form.php';
    }

    /* Get footer Contents */
    include "../footer.php";

Upvotes: 1

Views: 2326

Answers (3)

Your Common Sense
Your Common Sense

Reputation: 157865

There are many other faults with your code.

  • Instead of die(), you have to use more convenient trigger_error(); which won't reveal sensitive info to anyone.
  • if you are expecting only one result, there is no point in a loop
  • PHP is a loosely typed language. No need for excessive conditions. just if($table) is enough for most cases
  • using meaningful (and consistent) variable names also helps a lot

    $sql = 'SELECT TableName FROM maintable';
    $res = mysqli_query($connect, $sql) or trigger_error(mysqli_error($connect));
    //                                            $connect, not $link ^
    $row = mysqli_fetch_row($res);
    
    /* Display Message or Table */
    if (!$row){
        echo 'No Main Table has been set. Go to the <a href="set-main-table.php">Set Main Table</a> page to select a Main Table.';
    } else {
        $table = $row[0];
        include 'main-form.php';
    }
    

Upvotes: 0

cantsay
cantsay

Reputation: 2046

Instead of or die, just add this below

echo mysqli_error($link);

Also, in your query, wouldn't it be columnName FROM table?

Upvotes: -1

andrewsi
andrewsi

Reputation: 10732

if ($table = NULL || $table = "" ){

You're confusing assignment = with comparison ==

You should probably try:

if ($table == NULL || $table == "" ){

Upvotes: 2

Related Questions