php mysql获取数组和循环行

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.

 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

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';  
}

Try this:

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

then you loop by it like this:

foreach($moviedetails as $num->row)
{
    echo $row['title'];
}