user1056307
user1056307

Reputation: 69

mysql query for each of the values of an array

for ($i=0; $i<=$gsayi-1; $i++)
  {
    $a[] = mysql_result($aylik,$i);
  }
foreach ($a as $value)
  {
    $m = mysql_query("SELECT puan FROM sorular WHERE ID = $value");
    echo $m; 
  }

a[ ] array is

2,2,1

I am trying to execute a mysql query with each of that values. I am using that values as ID.

I tried foreach loop but it shows me Resource id #7Resource id #7

How can I solve this problem?

Upvotes: 0

Views: 1939

Answers (2)

jcorry
jcorry

Reputation: 11

You have to get the results in some form.

http://php.net/manual/en/function.mysql-query.php

mysql_query returns a resource...not the results of the query. To get the results you have to use one of the mysql_fetch_* functions, in this example we fetch an object with the query results.

http://www.php.net/manual/en/function.mysql-fetch-object.php

Try something like this:

for ($i=0; $i<=$gsayi-1; $i++) {
    $a[] = mysql_result($aylik,$i);
}
foreach ($a as $value) {
    $result = mysql_query("SELECT puan FROM sorular WHERE ID = $value");
    while($row = mysql_fetch_object($result) {
        echo $value . ' = ' . $row->puan . ' | ';
    }
}

Upvotes: 1

remiheens
remiheens

Reputation: 367

You have to fetch the result

$m = mysql_query("SELECT puan FROM sorular WHERE ID = $value");
$result = mysql_fetch_assoc($m);
echo $result['puan'];

Upvotes: 4

Related Questions