这是我的代码:
$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";
?>
问题是表测试确实存在并包含数据。测试表在数据库mysql中。
如果我更改了数据库的值,我会收到一个无法连接的错误(即使是mysql旁边的其他已知数据库)。
当这个程序运行时输出是“连接成功,查询失败,表'mysql.test'不存在”
非常感谢任何帮助。这是运行mariadb的AWS RDS实例,PHP是从EC2微实例运行的。
编辑:这是证明表存在的mysql字符串:
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)