mysqli_query() returns “Table doesn't exist” f

2019-07-16 03:12发布

问题:

Here's my code:

$dsn = "dbinstancename.c5twsfnt9pph.us-east-1.rds.amazonaws.com";
$port = "3306";
$db = "mysql";
$username = "username";
$password = "password";
$sourcetable = "test";

//initialize the connection
$link = mysqli_connect($dsn, $username, $password, $db, $port);

if ($link->connect_error){

        echo "Connection Failed";

        }

if(mysqli_connect_error()){
        echo 'MySQL Error: ' . mysqli_connect_error();
}

if(mysqli_select_db($link, $db)){
        echo 'connected successfully';
}

//set initial source id to import from
$sourceid = "1";

// get the first source url and name
$query = "SELECT * FROM $sourcetable WHERE id = $sourceid;";
//echo $query;

$result = mysqli_query($link, $query);

if($result === FALSE) {
        echo "query failed";
        echo mysqli_error($link);

}else{
        while ($row = mysqli_fetch_assoc($result)) {
                echo $row['name'];
                echo $row['url'];
        }
}

//echo "finished";

?>

The issue is that the table test does exist and contains data. The test table is in the database mysql.

If I change the value of database I am given an error that it cannot connect (even for other known databases beside mysql).

When this program runs the output is "connected successfullyquery failedTable 'mysql.test' doesn't exist"

Any help would be hugely appreciated. This an AWS RDS instance running mariadb, and the PHP is running from an EC2 micro instance.

EDIT: Here is the mysql string to prove table exists:

MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| column_stats              |
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| gtid_slave_pos            |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| index_stats               |
| innodb_index_stats        |
| innodb_table_stats        |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| roles_mapping             |
| servers                   |
| slow_log                  |
| table_stats               |
| tables_priv               |
| test                      |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
31 rows in set (0.00 sec)

MariaDB [mysql]> select * from test;
+----+--------+------------+
| id | name   | url        |
+----+--------+------------+
|  1 | banana | banana.com |
+----+--------+------------+
1 row in set (0.00 sec)