X10nD
X10nD

Reputation: 22030

PDO mysql select query not executing

Why is this PDO not executing?

$statement = $db->prepare("select * from quest where id = :'"+$testno+"'");
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC); 

echo "Q:".$row["questions"];

The "Q" is not echoed.

Upvotes: 0

Views: 238

Answers (3)

Stanislav Shabalin
Stanislav Shabalin

Reputation: 19278

You should put named placeholder in prepared sql and bind it to desired value later:

$statement = $db->prepare("select * from quest where id = :id");
$statement->bindParam(':id', $testno, \PDO::PARAM_INT);
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC); 

echo "Q:".$row["questions"];

Assuming id is integer, if not - replace \PDO::PARAM_INT with \PDO::PARAM_STR.

Upvotes: 4

Shahzad Barkati
Shahzad Barkati

Reputation: 2526

@X10nD, Simply use like below code... :)

$query = "SELECT * FROM quest WHERE id = ?";
$statement = $db->prepare($query);
$statement->bindParam(1,$testno);
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC); 

echo "Q:".$row['questions'];

Upvotes: 0

RichardBernards
RichardBernards

Reputation: 3097

$statement = $db->prepare("select * from quest where id = :testno");
$statement->execute(array(':testno'=>$testno));
$row = $statement->fetch(PDO::FETCH_ASSOC); 

echo "Q:".$row["questions"];

Upvotes: 1

Related Questions