SQL查询在PHP中返回null,但在MySQL中是正确的结果
I'm building a web application for inserting data into a MySQL database. I'm using PHP's PDO API to query the database to obtain an auto-incrementing primary key. When I run the query in MySQL Console, it produces the correct output. But when I try to run the query in PHP, it returns null.
Query:
mysql> SELECT Auto_Increment FROM information_schema.tables WHERE table_name='charlist';
Result in MySQL console:
+----------------+
| Auto_Increment |
+----------------+
| 7 |
+----------------+
Relevant PHP code:
// Configuration.
$username = "root";
$password = "root";
$hostname = "localhost";
$dbname = "asoiaf";
$tablename = "charlist";
// Opens a connection to the database.
try {
$conn = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getmessage();
}
// Gets all the information from POST.
$autoidquery = "SELECT Auto_Increment FROM information_schema.tables WHERE table_name='$tablename'";
$id = $conn->query("SELECT Auto_Increment FROM information_schema.tables WHERE table_name='$tablename'");
// This should output the auto-incremented primary key, but nothing is output.
echo $id."<br>Hello world";
Nothing is output on the page, although it should output the auto-incremented id and then "Hello world". I can't see any typos that I've made. Why would the query work in the console, but not in PHP?
我正在构建一个用于将数据插入MySQL数据库的Web应用程序。 我正在使用PHP的PDO API来查询数据库以获取自动递增的主键。 当我在MySQL控制台中运行查询时,它会生成正确的输出。 但是当我尝试在PHP中运行查询时,它返回null。 p>
查询: p>
mysql&gt; SELECT Auto_Increment FROM information_schema.tables WHERE table_name ='charlist';
code> pre>
MySQL控制台中的结果: p>
+ ---------------- + \ N | Auto_Increment |
+ ---------------- +
| 7 |
+ ---------------- +
code> pre>
相关的PHP代码: p>
// Configuration。
$ username =“root”;
$ password =“root”;
$ hostname =“localhost”;
$ dbname =“asoiaf”;
$ tablename =“charlist”;
//打开与数据库的连接。
尝试{
$ conn = new PDO(“mysql:host = $ hostname; dbname = $ dbname”,$ username,$ password) ;
$ conn-&gt; setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
} catch(PDOException $ e){
echo $ e-&gt; getmessage();
}
//从POST获取所有信息。
$ autoidquery =“SELECT Auto_Increment FROM information_schema.tables WHERE table_name ='$ tablename'”;
$ id = $ conn-&gt; query(“SELECT Auto_Increment FROM information_schema.tables WHERE table_name ='$ tablename'“);
//这应输出自动递增的主键,但不输出任何内容。
echo $ id。”&lt; br&gt; Hello world“;
code>
页面上没有输出任何内容,但应输出 自动递增的id然后是“Hello world”。 我看不到任何打字错误。 为什么查询在控制台中工作,而在PHP中不起作用? p>
div>
You need to fetch the result
$qry = $conn->query("SELECT Auto_Increment FROM information_schema.tables WHERE table_name='$tablename'");
$result = $qry->fetch();
$id = $result['Auto_Increment'];
echo $id."<br>Hello world";
PDO Query returns a PDO Statement, to access the desired result you need to access the result as an array.
echo $id['Auto_Increment'];
Or you could fetch the result from the statement
echo $id->fetchColumn();
Or use PDO Prepare + fetchColumn to retrieve the result as an integer.
Many ways lead to rome ;)
use : print_r($id);
to see the content of query result . the output of the query result is an array. you shou