dongxiaoxiao1983 2016-03-22 00:51
浏览 142
已采纳

具有COUNT(URL)查询的多个变量

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"

  • 写回答

1条回答 默认 最新

  • dongyinzheng6572 2016-03-22 01:06
    关注

    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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!