开发者

php function to return SQL results

This is what i have

function GetEventsList(){
    $result = mysql_query("SELECT * FROM `events`") or trigger_error(mysql_error()); 
    while($row = mysql_fetch_array($result)){ 
    for开发者_如何学运维each($row AS $key => $value) { $row[$key] = stripslashes($value); } 
    $id = $row['id'];
    $en = $row['name'];
    $endt = $row['date'];
    $push = "<option value=$id>$en - $endt</option>";
    return $push;
    }
    }

Its only returning 1 result, when there are 3 in the table


That's because you're returning from the function at the end of the first iteration. Try:

function GetEventsList() {
  $result = mysql_query("SELECT * FROM `events`") or trigger_error(mysql_error()); 
  $push = '';
  while ($row = mysql_fetch_array($result)) {
    foreach($row AS $key => $value) {
      $row[$key] = stripslashes($value); 
    }
    $id = $row['id'];
    $en = $row['name'];
    $endt = $row['date'];
    $push .= "<option value=$id>$en - $endt</option>";
  }
  return $push;
}

On a side note: if you used good formatting/indenting that would've been far easier for you to spot.

Also, are you sure you need to use stripslashes()?


If I'm reading the code correctly, return $push; is exiting the while loop prematurely. You want to return only after all results are gathered and stored in something.


Either use echo $push in your foreach loop; or put each iteration of the option element into an array, then insert the array as needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜