创建表格后,它将显示此数组([0] => 00000 [1] => [2] =>) [英] After creating table it shows this Array ( [0] => 00000 [1] => [2] => )
问题描述
在PDO中使用exec()创建表后,它显示如下
After creating table using exec() in PDO,it shows like this
Array ( [0] => 00000 [1] => [2] => )
这是错误还是我成功创建了表格? 当我在数据库中查找表时,就会创建表.
Is this an error or did I successfully create my table? When I looked up in the database the table is created.
这就是我执行的
try{
$tbl = new PDO("mysql:host=localhost;dbname=myDB",'root','');
$tbl->exec("CREATE TABLE test(fld1 CHAR(40),fld2 CHAR(40))")
or die(print_r($tbl->errorInfo(),true));
}
catch(Exception $e){
echo $e.getMessage();
}
@Vikas,如果我错了,请纠正我.这可以正常工作
@Vikas,correct me if i am wrong.and this is working
$queryTBl="CREATE TABLE test(fld1 CHAR(40),fld2 CHAR(40))";
$evaluateTBL=tbl->exec($queryTBL);
if ($evaluateTBL===false)
print "Test table could not be created";
else
print "Successfully Created";
推荐答案
根据 PDO :: exec manual ,它返回受查询影响的行数.而且没有任何行受CREATE
查询影响.因此,此查询的exec
即使成功也将返回0.它将被评估为false
.这就解释了为什么即使没有错误也要调用die
的原因.
According to PDO::exec manual it returns number of rows affected by the query. And there is no rows affected by a CREATE
query. So exec
for this query will return 0 even if it was successful. And it will be evaluated false
. This explains why die
is called even when there is no error.
最好依靠异常或对exec的返回码进行=== false
以确定它是否失败.从手册中:
It is better to either rely on exception or do a === false
on return code of exec to decide if it failed. From the manual:
此函数可以返回布尔FALSE,但也可以返回非布尔值,其值为FALSE.请阅读有关布尔值的部分以了解更多信息.使用===运算符测试此函数的返回值.
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
执行相同操作的示例,但有例外:
Example of doing the same thing with exceptions:
try {
$connection = new PDO("mysql:host=localhost;dbname=myDB",'root','');
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection->exec('CREATE TABLE test(fld1 CHAR(40),fld2 CHAR(40))');
} catch (PDOException $e) {
<handle exception>
}
这篇关于创建表格后,它将显示此数组([0] => 00000 [1] => [2] =>)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!