Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
once i wrote 50 columns manually.. :) by the way you can put all the columns data in array at a time by $moviedetails[]=$row; –  Feroz Akbar May 18 at 15:40

3 Answers 3

up vote 0 down vote accepted
 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

share|improve this answer
    
this is exactly what i was looking for. works perfectly. Thank you –  Urban Tan Creams May 18 at 18:00

Try this:

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

then you loop by it like this:

foreach($moviedetails as $num->row)
{
    echo $row['title'];
}
share|improve this answer

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';  
}
share|improve this answer
    
whatttttttt!!!! –  Feroz Akbar May 18 at 15:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.