The query below worked when it contained just one variable ($MyURL). But when I added $MySection, I started getting this error message:
PDOStatement::bindParam() expects parameter 3 to be long, string given in...
I know how to add multiple values in a query that fetches an array, but this kind of query is confusing for me. What am I doing wrong?
$sql= "SELECT COUNT(URL) AS num FROM pox_topics
WHERE URL = :MyURL AND Site = 'PX' AND Section = ':MySection' AND Live != 0";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':MyURL',$MyURL,':MySection',$MySection,PDO::PARAM_STR);
$stmt->execute();
$Total = $stmt->fetch();
EDIT:
I've tried the following query with PDO::PARAM_STR following $MyURL, $MySection and following both of them...
$sql= "SELECT COUNT(URL) AS num FROM pox_topics
WHERE URL = :MyURL AND Site = 'PX' AND Section = :MySection AND Live != 0";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':MyURL',$MyURL,PDO::PARAM_STR);
$stmt->bindParam(':MySection',$MySection);
$stmt->execute();
$Total = $stmt->fetch();
But I'm getting this error message: "Invalid parameter number: number of bound variables does not match number of tokens"
You have quotes around your 2nd bind ':MySection'
in AND Section = ':MySection'
, those need to be removed.
Then you're using both of your binds in the same bindParam
. They need to be on separate statements.
$sql= "SELECT COUNT(URL) AS num FROM pox_topics
WHERE URL = :MyURL AND Site = 'PX' AND Section = :MySection AND Live != 0";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':MyURL',$MyURL);
$stmt->bindParam(':MySection',$MySection,PDO::PARAM_STR);
$stmt->execute();
$Total = $stmt->fetch();
Sidenote: Unsure which one you want to use PDO::PARAM_STR
for. Adjust respectively.
Also make sure those variables have values. Error reporting will throw you something about it, should they fail.
Example from the manual http://php.net/manual/en/pdostatement.bindparam.php
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
Also use PDO's error handling: