0

I have following code:

<?php
include_once 'init/init.funcs.php';
$pollid=$_GET['pollid'];
$result = mysql_query('SELECT question FROM questions where survey_id="' . $pollid . '"');
$question = mysql_result($result, 0);
echo $pollid;
echo $question;

?>

And it will echo first question which has survey_id=$pollid. But I want to make an array of all the questions, which have survey_id=$pollid.How I can do that?

user244902
  • 143
  • 4
  • 14
  • Seriously :| mysql_result! – h2O Apr 16 '14 at 14:37
  • possible duplicate of [how to fetch all the row of the result in php mysql?](http://stackoverflow.com/questions/10940332/how-to-fetch-all-the-row-of-the-result-in-php-mysql) – mario Apr 16 '14 at 14:39

2 Answers2

1

Just loop through the results and add them to an array:

$pollid = (int) $_GET['pollid'];
$questions = array();
$result = mysql_query('SELECT question FROM questions where survey_id="' . $pollid . '"');
while($row = mysql_fetch_assoc($result)) {
    $questions[$pollid] = $row['question '];
}

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

You are also wide open to SQL injections. In my example I casted $_GET['pollid'] to an integer to help protect against them. A better approach would be to use prepared statements as mentioned above.

Zoe
  • 27,060
  • 21
  • 118
  • 148
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

Try to use mysqli_ instead of mysql_,

// MYSQLI
$mysqli = new mysqli('localhost', 'example', 'example', 'test');

print '<h3>MYSQLI: simple select</h3>';
$rs = $mysqli->query( YOUR SELECT QUERY);
while($row = $rs->fetch_object())
{
    //DATA
}