Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-PDO,如何返回select语句中的行数_Php_Pdo - Fatal编程技术网

PHP-PDO,如何返回select语句中的行数

PHP-PDO,如何返回select语句中的行数,php,pdo,Php,Pdo,我有一个简单的语句,但似乎无法从select语句中获取行数,当它真的很简单时,我真的很烦。我总是得到空字符串 这是密码 include "connect.php"; $userID = $_POST['userID']; $threadID = $_POST['threadID']; $sql1 = "select * from AT_AddThread where AT_ID = :threadID"; $stmt = $conn->prepare($sql1); $stmt->

我有一个简单的语句,但似乎无法从select语句中获取行数,当它真的很简单时,我真的很烦。我总是得到空字符串

这是密码

include "connect.php";

$userID = $_POST['userID'];
$threadID = $_POST['threadID'];

$sql1 = "select * from AT_AddThread where AT_ID = :threadID";
$stmt = $conn->prepare($sql1);
$stmt->execute(array(':threadID' => $threadID));
$result = $stmt->fetchColumn(7);
//echo $result;

$sql2 = "select * from TJ_ThreadJoined where TJ_T_ID = :threadID"
$q2 = $conn->prepare($sql2);
$q2->execute(array(':threadID' => $threadID));
$q2->execute();
$rows = $q2->fetchColumn();
echo $rows; //THIS IS where i want to return the number of rows from the select statement
试试这个:

echo$q2->rowCount()


->rowCount()

除了fetchColumn,您还可以尝试以下方法:

  • 数一数行

    $rows=$stmt->fetchAll();
    echo“count=”。计数(行)

  • 使用
    rowCount()

    echo“count=”$stmt->rowCount()

  • 将其添加到查询中:

    选择COUNT(*)作为行计数…

    echo$row['rowCount']


  • 您始终可以获取上次查询中返回的行数。只需在
    SELECT
    语句之后添加
    SQL\u CALC\u FOUND\u行

    $sql2 = "select SQL_CALC_FOUND_ROWS * from TJ_ThreadJoined where TJ_T_ID = :threadID"
    

    然后执行查询
    选择查找的行(),获取行,您将获得上次查询的行数。

    对于
    $conn->rowCount
    虽然它不完全可靠,因为它不工作,但所有数据库引擎都会使用它。有一种方法:但它不能保证与所有数据库引擎一起工作。那么必须有另一种方法吗?
    ->rowCount()
    技术上用于检索受INSERT或UPDATE语句影响的最后一行数,但某些数据库服务器会将其返回以进行选择
    fetchColumn
    根据文档:从结果集的下一行返回一个列,因此无论如何您都无法对该列进行计数-它将始终为1(即下一行)。您最好只选择SQL中需要的列,然后在循环中使用
    fetch()
    ,或者
    fetchAll()
    并计算返回的数组大小。
    $sql2 = "select SQL_CALC_FOUND_ROWS * from TJ_ThreadJoined where TJ_T_ID = :threadID"