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.
this is what i executed
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,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";
According to PDO::exec manual it returns number of rows affected by the query. And there is no rows affected by a
CREATEquery. Soexecfor this query will return 0 even if it was successful. And it will be evaluatedfalse. This explains whydieis called even when there is no error.It is better to either rely on exception or do a
=== falseon return code of exec to decide if it failed. From the manual:Example of doing the same thing with exceptions: