Home » Php » mysql – php query failing on new tables

mysql – php query failing on new tables

Posted by: admin February 25, 2020 Leave a comment

Questions:

** Resolved – ISP server change led to new and old databases being accessed **

I have been working on a club membership application using PHP and MySQL. All has been going well with the majority of the master file programs up and running.

I then added a new master file and got a SQL query error. Copying the error into SQL the query ran fine which didn’t make sense as I had simply taken an existing working program and changed the table and columns for the new table.

I have now created a simple program as shown below and run it using each of the following 3 tables in turn…

  1. memberlist – the table of members which already works fine with
    the member list program
  2. test – a new table consisting of two rows containing a single ‘RecID’ column
  3. memberlist2 – a copy of memberlist created via PHPMyAdmin

Running the below code against each table I get the following…

  1. Using memberlist runs fine and I get a list of the RecIDs for the table
  2. Using test gives the following error ‘Update query failed: SELECT RecID FROM test’. Running the $sqlget code in PHPMySQL SQL brings up the data correctly
  3. Using memberlist2 gives the following error ‘Update query failed: SELECT RecID FROM memberlist2’. Running the $sqlget code in PHPMySQL SQL brings up the data correctly
  4. I finally substituted ‘test’ and ‘memberlist2’ for the memberlist table in the working member listing program, and again got SQL Query errors for each which disappeared when I reverted to the original ‘memberlist’ table.

Note that all error messages show nothing for mysqli_error.
Although I have been working with SQL for some 15 years this has me baffled at the moment.

Any ideas welcome!

<?php
    include('connect_mysql.php');
    $sqlget = "SELECT RecID FROM test";  // 'memberlist' or 'memberlist2' substitued for other tests
    $sqldata = mysqli_query($dbcon, $sqlget) or die("Update query failed: " . mysqli_error().$sqlget);
    echo "<table><tr><td>Rec ID</td></tr>";
    while($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) { 
        echo "<tr><td>$row[RecID]</td></tr>";
    }
    echo "</table>"
?>

SELECT DATABASE() gives…

blueskya_blueskyclub    

SHOW TABLES gives…

Tables_in_blueskya_blueskyclub
Account_Parties
Account_Transactions
Accounts
Books
Fees
OptionSets
memberlist
memberlist2
model_list
noise_tests
noisetest_list
password_reset_temp
plans
test

The full error line is

Update query failed with error 'Table 'blueskya_blueskyclub.test' doesn't exist' Query: SELECT RecID FROM test

OK – More strange things. I have been experimenting with creating tables within PHP and I thought at first that this had failed but have now found that if I do a SHOW TABLE within PHPMyAdmin I get the following

Tables_in_blueskya_blueskyclub
Account_Parties
Account_Transactions
Accounts
Books
Fees
OptionSets
memberlist
memberlist2
model_list
noise_tests
noisetest_list
password_reset_temp
plans
test
test2

But if I do the same from within PHP I get…

Table Names
Account_To_From
Account_Transactions
Accounts
Books
Fees
OptionSets
memberlist
model_list
noise_tests
noisetest_list
password_reset_temp
plans
test3
test4
zz

Note that Account_Parties, tests and test2 are not visible from PHP code, and test3, test4 and zz are tables I created via PHP code and are not visible to PHPMyAdmin.

Is the database accessed a different way from within PHP code?

How to&Answers: