2014-01-06 25 views
0

我使用下面的方法從我的SQL數據庫查詢:獲取行從SQL用PHP

function query() { 
    global $link; 
    $debug = false; 
    //get the sql query 
    $args = func_get_args(); 
    $sql = array_shift($args); 

    //secure the input 
    for ($i=0;$i<count($args);$i++) { 
     $args[$i] = urldecode($args[$i]); 
     $args[$i] = mysqli_real_escape_string($link, $args[$i]); 
    } 

    //build the final query 
    $sql = vsprintf($sql, $args); 

    if ($debug) print $sql; 

    //execute and fetch the results 
    $result = mysqli_query($link, $sql); 
    if (mysqli_errno($link)==0 && $result) { 

     $rows = array(); 

     if ($result!==true) 
     while ($d = mysqli_fetch_assoc($result)) { 
      array_push($rows,$d); 
     } 
     //return json 
     return array('result'=>$rows); 
    } else { 
      //error 
     return array('error'=>'Database error'); 
    } 
} 


$result = $result = query("SELECT * FROM users WHERE email='$email' limit 1"); 

$name = (what goes here?) 

我試圖獲取用戶的字符串名稱,我該怎麼辦呢?

+0

試試這個:$名稱= $結果[ '結果'] [0] [ '名稱']; –

回答

0

如果您的查詢是正確的,然後

試試這個:

function query() { 
     global $link; 
     $debug = false; 
     //get the sql query 
     $args = func_get_args(); 
     $sql = array_shift($args); 

     //secure the input 
     for ($i=0;$i<count($args);$i++) { 
      $args[$i] = urldecode($args[$i]); 
      $args[$i] = mysqli_real_escape_string($link, $args[$i]); 
     } 

     //build the final query 
     $sql = vsprintf($sql, $args); 

     if ($debug) print $sql; 

     //execute and fetch the results 
     $result = mysqli_query($link, $sql); 
     if (mysqli_errno($link)==0 && $result) { 

      $rows = array(); 

      if ($result!==true) 
      while ($d = mysqli_fetch_assoc($result)) { 
       array_push($rows,$d); 
      } 
      //return json 
      return array('result'=>$rows); 
     } else { 
       //error 
      return array('error'=>'Database error'); 
     } 
    } 


    $result = query("SELECT * FROM users WHERE email='$email' limit 1"); 

    $name = $result['result'][0]['name']; 
+0

@ user906357:你可以試試嗎? –

+0

就是這樣!非常感謝! – user906357

0

你忘了價值傳遞給函數

function query($sql) { 
    global $link; 
    $debug = false; 
    //get the sql query 
    $args = func_get_args(); 
    $sql = array_shift($args); 

    //secure the input 
    for ($i=0;$i<count($args);$i++) { 
     $args[$i] = urldecode($args[$i]); 
     $args[$i] = mysqli_real_escape_string($link, $args[$i]); 
    } 

    //build the final query 
    $sql = vsprintf($sql, $args); 

    if ($debug) print $sql; 

    //execute and fetch the results 
    $result = mysqli_query($link, $sql); 
    if (mysqli_errno($link)==0 && $result) { 

     $rows = array(); 

     if ($result!==true) 
     while ($d = mysqli_fetch_assoc($result)) { 
      array_push($rows,$d); 
     } 
     //return json 
     return array('result'=>$rows); 
    } else { 
      //error 
     return array('error'=>'Database error'); 
    } 
} 


$result = $result = query("SELECT * FROM users WHERE email='$email' limit 1") 
+0

你在哪裏傳遞了缺少的參數? –

+0

我更新了這個,以通過參數和所有作品相同。該查詢在更改 – user906357

0
$name = ""; 
if(! isset($result["error"]) and isset($result["name"])) // check it returns error or not. then check name field exist or not. 

{ 
    $name = $result["name"]; 
} 

echo $name; 
+0

之前正在工作謝謝您的回覆,但是這並不奏效。 – user906357

+0

你在做什麼? –

+0

沒有輸出 – user906357