Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.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 使用数组查询数据库_Php_Mysql_Arrays - Fatal编程技术网

Php 使用数组查询数据库

Php 使用数组查询数据库,php,mysql,arrays,Php,Mysql,Arrays,当我这样做时,我只得到一个结果Fantasy0.1429当我应该得到7个不同的结果时,任何人都知道我做错了什么 $userid = $_SESSION['sess_id']; $genreQuery = $con ->query ("select distinct(genre) from movies"); $movieGenre = array(); while($row = $genreQuery->fetch_object()) { $movieGenre[] = $r

当我这样做时,我只得到一个结果
Fantasy0.1429
当我应该得到7个不同的结果时,任何人都知道我做错了什么

$userid = $_SESSION['sess_id'];

$genreQuery = $con ->query ("select distinct(genre) from movies");

$movieGenre = array();

while($row = $genreQuery->fetch_object())  {
$movieGenre[] = $row;
}

foreach($movieGenre as $MGenre){
 $query = $con ->query 
 (" select '$MGenre->genre' genre, IFNULL(count(*)/(select count(*) from user_movie_ratings where user_id = '$userid'),0) rating
    from   user_movie_ratings umr,
    movies m
    where  umr.user_id = '$userid'
    and    umr.movie_id = m.id
    and    m.genre = '$MGenre->genre'; ");

$movieTitle = array();

while($row = $query->fetch_object())  {
$movieTitle[] = $row;
}
}   
然后我稍后再重复

<?php foreach($movieTitle as $movie): echo $movie->genre; echo $movie->rating; endforeach; ?>

尝试在foreach之前移动数组的实例化,如:

// Some code ...
$movieTitle = array();

foreach($movieGenre as $MGenre){
 $query = $con ->query 
 (" select '$MGenre->genre' genre, IFNULL(count(*)/(select count(*) from user_movie_ratings where user_id = '$userid'),0) rating
    from   user_movie_ratings umr,
    movies m
    where  umr.user_id = '$userid'
    and    umr.movie_id = m.id
    and    m.genre = '$MGenre->genre'; ");

while($row = $query->fetch_object())  {
$movieTitle[] = $row;
}
} 

错误是什么?显示的是什么?没有错误问题是我应该得到7个结果,因为我只得到一个
var\u dump($movieTitle)
正在显示什么?尝试打印请求并在phpmyadminarray(1){[0]=>object(stdClass){11(2){[“流派”=>string(7)“幻想”[“评级”=>string(6)中运行它"0.1429" } }