-4

我尝试在我的数组中获取元素的位置,但这不起作用,这是我的代码:

<?php
function getWorldRank(){
$query = ("SELECT nickname FROM users ORDER BY score, updateDate Desc ");

mysql_query("SET NAMES 'utf8'");

//GOES TO THE DB WITH THE QUERY
$result = mysql_query($query);

//CHECKS IF THE QUERY IS LEGAL
if(!$result)
return false;

//CHECKS IF THERE ARE NO ROWS FOUND IN THE TABLE
if(mysql_num_rows($result)==0)
return false;

    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
         $returned_array[] = $row;
    }
    return $returned_array;

       }

      $a=getWorldRank();



    echo $key = array_search('vvvv', $a); 
    echo $key = array_search('bbbb', $a); 

 ?>
4

2 回答 2

1

尝试这个:

function getWorldRank() {
  $query = ("SELECT nickname FROM users ORDER BY score, updateDate Desc ");
  mysql_query("SET NAMES 'utf8'");

  //GOES TO THE DB WITH THE QUERY
  $result = mysql_query($query);

  // CHECKS IF THE QUERY IS LEGAL
  if(!$result) {
    return false;
  }

  // CHECKS IF THERE ARE NO ROWS FOUND IN THE TABLE
  if(!mysql_num_rows($result)) {
    return false;
  }

  $returned_array = array();

  while($row = mysql_fetch_assoc($result)) {
    $returned_array[] = $row['nickname'];
  }
  return $returned_array;
}

$a = getWorldRank();

if(!$a) {
  print 'No results returned by database';
} else {
  echo 'vvvv key: ' . array_search('vvvv', $a); 
  echo 'bbbb key: ' . array_search('bbbb', $a); 
}
于 2013-03-25T22:48:33.820 回答
0

你想改变一行:

$returned_array[] = $row;

$returned_array[] = $row['nickname'];

那么你将返回一个数组,而不是数组数组。

于 2013-03-25T22:47:09.127 回答