Smudger
Smudger

Reputation: 10781

display contents of PHP Mysql Array

Simple question (I think).

I have the below PHP / MySQL script:

$risksql="select risk from jobsrisks where job='$job'";
$executerisks=mysql_query($risksql);
$test=mysql_fetch_array($executerisks);
$riskrows=mysql_num_rows($executerisks);

I want to print the values of the array (for testing), with code:

print_r($test);

This produces the below output:

The query, in php actually outputs 3 records, not just one that is repeated. Any ideas whay I am doing incorrectly? where are the other records and why not in the array? is mysql_fetch_array the correct code to use?

I then want to then use the PHP array in another mysql query:

$ids = join(',',$test);   
$sql = "SELECT * FROM table WHERE risk IN ($ids)"; 

Would this then be correct?

Help is appreciated as always.

Thanks, R

Upvotes: 0

Views: 4520

Answers (2)

Mihai Iorga
Mihai Iorga

Reputation: 39704

why not join the selects?

$sql = "SELECT * FROM table WHERE risk IN (SELECT risk FROM jobsrisks WHERE job='$job')"; 

Upvotes: 0

pomaxa
pomaxa

Reputation: 1746

you need to fetch each row, ie:

while ($row = mysql_fetch_assoc($result)) {
    echo $row['firstname'];
    echo $row['lastname'];
    echo $row['address'];
    echo $row['age'];
}

check the manual

$ids = join(',',$test);   
$sql = "SELECT * FROM table WHERE risk IN ('$ids')"; 

almost right. don't forget about quotes

Upvotes: 2

Related Questions