2013-04-10 93 views
0

似乎我的第二個MySQL查詢失敗,原因不明。只有當從$ dbLink-> query()獲得結果時,連接時纔會出現問題。PHP MySQL第二次查詢失敗

$photos = getPhotos(getID($_COOKIE[$uname_cookie], $_COOKIE[$passwd_cookie])); 

function getID($uName, $passwd) { 
    // Just in case, for modularity :P 
    include 'misc.php'; 
    $query = "SELECT ID FROM Users WHERE UserName = '" . $uName . "' AND Password = '" . $passwd . "';"; 
    $dbLink = new mysqli($host, $user, $pass, $dbname); 
    if ($dbLink->connect_errno) { 
     echo "Failed to connect to database: " . $dbLink->connect_error; 
     return null; 
    } else { 
     $results = $dbLink->query($query); 
     print_r($results->fetch_array()); 
     $rows = $results->fetch_array(); 
     return $rows["ID"]; 
    } 
} 

function getPhotos($id) { 
    // Just in case, for modularity :P 
    include 'misc.php'; 
    $query = "SELECT Location FROM Photos WHERE UserID = " . $id . ";"; 
    $dbLink = new mysqli($host, $user, $pass, $dbname); 
    if ($dbLink->connect_errno) { 
     echo "Failed to connect to database: " . $dbLink->connect_error; 
     return null; 
    } else { 
     $results = $dbLink->query($query); 
     print_r($results->fetch_array()); 
     $rows = $results->fetch_array(); 
     return $rows["ID"]; 
    } 
} 

編輯:明白了周圍的$ DBLINK傳遞給函數IE getPhotos($ ID,$ DBLINK)

+4

你迫切需要在[適當的SQL轉義(HTTP讀了起來: 'mysqli'。您應該使用'bind_param',而不是將原始用戶數據插入到查詢字符串中。 – tadman 2013-04-10 21:10:30

+0

沒有錯誤或任何?另外,你可能想分開實例化數據庫,以避免有許多不同但平等的連接。 – Think 2013-04-10 21:11:15

+1

@tadman我不斷點擊向上箭頭,但它只能讓我給你一個upvote! (jk) – 2013-04-10 21:11:31

回答

0
$query = "SELECT Location FROM Photos WHERE UserID = '" . $id . "';";