PHP函数,用于计算(MySql)查询循环中返回null的单个行

PHP函数,用于计算(MySql)查询循环中返回null的单个行,php,mysql,Php,Mysql,我想在查询循环中包含一个php函数,它只输出表中的所有行 此函数将返回另一个查询,计算单个用户的行数,并与第一个查询一起编码 该函数可以工作,如果我回显userId变量,我就正确了,但是出于某种原因,count查询返回null 我需要一些帮助,谢谢 <?php include_once 'includes/db_connect.php'; $query = "SELECT * FROM UPLOADS"; //Creating resonse json array, with ano

我想在查询循环中包含一个php函数,它只输出表中的所有行

此函数将返回另一个查询,计算单个用户的行数,并与第一个查询一起编码

该函数可以工作,如果我回显userId变量,我就正确了,但是出于某种原因,count查询返回null

我需要一些帮助,谢谢

<?php

include_once 'includes/db_connect.php';

$query = "SELECT * FROM UPLOADS";

//Creating resonse json array, with another array inside
$jsonResponse = array( "info" =>array() );

if($result = mysqli_query($mysqli, $query)) {
    while($row = mysqli_fetch_assoc($result)){

     $jsonRow = array(        
         'user'             =>      $row['USER'],
         'userId'           =>      $row['USERID'],
         'filename'         =>      $row['FILENAME'],
         'description'      =>      $row['DESCRIPTION'],
         'location'         =>      $row['LOCATION'],
         'address'          =>      $row['ADDRESS'],
         'likes'            =>      $row['LIKES'],
         'city'             =>      $row['CITY'],
         'lat'              =>      $row['LAT'],
         'lng'              =>      $row['LNG'],    
         'countPost'        =>      countUserPosts($row['USERID'])
        );          

        //adding the $jsonRow array to the end of the "users" array as key/value
        array_push($jsonResponse["info"], $jsonRow);
    }   

}

//encoding to json for the app
echo json_encode($jsonResponse);    

function countUserPosts($userId){

    $result = mysqli_query($mysqli, "SELECT count(*) FROM UPLOADS WHERE USERID = '$userId' ");
    $row = mysqli_fetch_row($result);
    $num = $row[0];

    return $num;

}       

?>

尝试将mysqli对象插入函数参数中:

function countUserPosts($userId, $mysqli){
                              //   ^ this one so it won't be out of scope.
    $result = mysqli_query($mysqli, "SELECT count(*) FROM UPLOADS WHERE USERID = '$userId' ");
    $row = mysqli_fetch_row($result);
    $num = $row[0];

    return $num;

}   

首先,我猜and ID是一个整数,整数不需要介于“”之间,其次,如果你回显你的查询并将其粘贴到你的mysql中,会返回null吗?@EdmondTamas是的,你需要作用域上的连接。我很高兴这有帮助