0
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?

5
  • If you have 2, 2, 1, your first two echos will be the same Commented Nov 26, 2011 at 19:36
  • I made it 2,1 and I want to do SELECT puan FROM sorular WHERE ID = 2 SELECT puan FROM sorular WHERE ID = 1 but in a functional way as you understand from my question Commented Nov 26, 2011 at 19:41
  • What is $aylik? Would it be possible to combine the two queries into one so you do not need to loop twice? Commented Nov 26, 2011 at 19:43
  • $aylik is a query that takes question id's which user solved from a table in a certain period of time. Commented Nov 26, 2011 at 19:47
  • Note that the mysql extension is outdated and on its way to deprecation. All new code should use mysqli or PDO, both of which have many advantages, including prepared statements. Instead of interpolating values directly into the statement, you should use a prepared statement, which lets you re-use the query and keeps the value separate. Commented Nov 26, 2011 at 22:59

2 Answers 2

4

You have to fetch the result

$m = mysql_query("SELECT puan FROM sorular WHERE ID = $value");
$result = mysql_fetch_assoc($m);
echo $result['puan'];
Sign up to request clarification or add additional context in comments.

2 Comments

now the results are 3,4 when I do array_sum($result) it shows me 4 not 7. Can you help me about that also?
You have to be careful with the type of your values in this array. This should be int value.
1

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

1 Comment

now the results are 3,4. when I do array_sum($result) it shows me 4 not 7. Can you help me about that also?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.