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

具有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"

图片转代码服务由CSDN问答提供 功能建议

下面的查询只包含一个变量($ MyURL)。 但是当我添加$ MySection时,我开始收到此错误消息:

PDOStatement :: bindParam()期望参数3为long,字符串在中给出。 。

我知道如何在获取数组的查询中添加多个值,但这种查询对我来说很困惑。 我做错了什么?

  $ 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(); 
   
 
 

编辑:

我在$ MyURL,$ MySection之后使用PDO :: PARAM_STR尝试了以下查询并跟随它们......

  $ sql =“SELECT COUNT(URL)AS num FROM pox_topics 
WHERE URL =:MyURL AND Site ='PX'AND Section =:MySection AND Live!= 0”; 
 $ stmt =  $ pdo->制备($ SQL); 
 $的stmt-> bindParam( ':MyURL',$ MyURL,PDO :: PARAM_STR); 
 $的stmt-> bindParam( ':MySection',$ MySection  ); 
 $ stmt-> execute(); 
 $ Total = $ stmt-> fetch(); 
   
 
 

但我得到了这个 错误消息:“参数号无效:绑定变量数与令牌数不匹配”

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyinzheng6572
    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:

    点赞 评论

相关推荐