dqpwdai095465
dqpwdai095465
采纳率100%
2014-05-18 15:32

php mysql获取数组和循环行

已采纳

i have a database table 'movies'. in this table there are 25 columns of information per row. Ie, movie title, poster, cast, synopsis etc.

At the moment i am fetching the information like this

$query = "SELECT * FROM `movies` WHERE `title`='$moviename'";

$result = $con->query($query) or die($mysqli->error.__LINE__);


if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {

$moviedetails['title']=$row['title'];
$moviedetails['poster']=$row['poster']; 
}
}
else {
echo 'NO RESULTS';  
}

because i have 25 columns its long work writing out each variable. is there a way to fetch the information and i can then call to it by using

$moviedetails['column name'] 

ie

im new to php and mysql so any help appreciated.

thanks lee

$moviedetails['title']

fetches the information from the 'title' column.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • doutangguan2460 doutangguan2460 7年前
     while($row = $result->fetch_assoc()) {
     foreach ($row as $key => $value){
     $moviedetails[$key]=$value;
     }
     }
    

    This input the same key and the same value into new array

    点赞 评论 复制链接分享
  • douzhi3776 douzhi3776 7年前

    Try this:

    while($moviedetails[]=$result->fetch_assoc()){}
    

    then you loop by it like this:

    foreach($moviedetails as $num->row)
    {
        echo $row['title'];
    }
    
    点赞 评论 复制链接分享
  • douao2000 douao2000 7年前

    Is that what you're looking for?

    $query = "SELECT * FROM `movies` WHERE `title`='$moviename'";
    
    $result = $con->query($query) or die($mysqli->error.__LINE__);
    
    if($result->num_rows > 0) {
        while($moviedetails = $result->fetch_assoc()) {
            //just use moviedetails for what you need
        }
    } else {
        echo 'NO RESULTS';  
    }
    
    点赞 评论 复制链接分享

相关推荐