I am trying to Select data from my db using PDO. However whenever I run the query PDO "rowCount()" returns 0 affected rows. When I run the same query in Navicat, I get the appropriate rows returned.
My Code:
$select = $dbh->prepare("SELECT columnName FROM tblName WHERE columName2 = :val1 AND `value` = :val2");
$select->execute(
array(
":val1" => "data1",
":val2" => "data2"
)
);
I configured PDO to throw errors and my code runs in a try/catch block. The try part is executed till the end, no exceptions thrown.
What I want to achieve: Get appropriate rows and insert into new table.
Any ideas on where my problem could be?
EDIT:
try {
$dbh->beginTransaction();//drop/create table
if($dbh->exec("DROP TABLE IF EXISTS tblName") === false)
{
$dbh->rollback();
exit($dbh->errInfo());
}
if($dbh->exec("CREATE TABLE tblName (id bigint);") === false)
{
$dbh->rollback();
exit($dbh->errInfo());
}
$dbh->commit();//save table
$dbh->beginTransaction();//insert transaction
$put = $dbh->prepare("INSERT INTO tblName (id) VALUES (:id)");
$bind = array(
":id" => null
);
$a = "string";
// $b = "[lang_es]string[/lang_es][lang_en]string[/lang_en]";
$select = $dbh->prepare("SELECT columnName FROM tblName WHERE columnName = :val1 ");
// AND `value` = :val2");
$select->bindValue(":val1", $a);
// $select->bindValue(":val2", $b);
$select->execute();
print_r($select);
while($row = $select->fetch(PDO::FETCH_ASSOC))
{
$bind[":id"] = $row["columName"];
$put->execute($bind);//inserts row
$put->closeCursor();//optional
}
$dbh->commit();//save changes to db
var_dump($dbh->errorInfo());
}
Because execute take just list of arguments to placeholders.
$select = $dbh->prepare("SELECT columnName FROM tblName WHERE columName2 = ? AND `value` = ?");
$select->execute(
array(
'data1',
'data2',
)
);