2015-05-27 35 views
1

我正在嘗試使用PHP腳本從服務器獲取Status列的值。我收到以下錯誤:PHP SQl查詢中可捕獲的致命錯誤

<b>Catchable fatal error</b>: Object of class mysqli_result could not be converted to string in <b>C:\xampp\htdocs\loginstatuscheck.php</b> on line <b>21</b><br /> 

我對PHP腳本非常陌生。插入工作正常,在我嘗試執行提取查詢的同一行上。我現在的PHP腳本如下:

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "iFocusBlogs"; 

     $obtaineduserName = urldecode($_POST['enteredusername']); 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$query_get_status = "SELECT Status FROM LoginTable WHERE UserName ='" .$obtaineduserName. "'"; 

// perform the query and store the result 
$result = $conn->query($query_get_status); 

echo $result; 

//if ($conn->query($query_get_status) === TRUE) { 
    // echo $result; 
//} else { 
    echo "Error: " . $result . "<br>" . $conn->error; 
//} 

mysql_close(); 
$conn->close(); 
?> 

請讓我知道我是正確的查詢和請讓我知道必須做讓我的查詢執行什麼樣的變化。所有建議都歡迎。提前致謝。

+1

您無法echo $ result;這不是一個字符串嘗試print_r($結果)或var_dump ... –

+2

'$ result'是一個對象,它不能被回顯爲字符串,除非類已經實現了'__toString'來顯示一些東西。 http://php.net/manual/en/language.oop5.magic.php#object.tostring –

回答

1

對象上不能使用echo

如果你想看到一個對象的內容,您可以使用var_dump($var);

這裏是如何轉儲符合您的要求,所有的SQL行:

while($obj = $result->fetch_object()){ 
    var_dump($obj); 
} 

因爲你似乎會問了唯一的行,你可以簡單地使用:

$query_get_status = "SELECT Status FROM LoginTable WHERE UserName ='" .$obtaineduserName. "'"; 

// perform the query and store the result 
$result = $conn->query($query_get_status); 

var_dump($result->fetch_object()); 

使用fetch_object()你mysql的結果轉換爲對象,那麼你就可以訪問它的屬性一這樣的。

$result = $conn->query($query_get_status)->fetch_object(); 
$status = $result->Status; 
+0

這工作我得到的響應如下所示: 響應的值是對象(stdClass)#3(1 ){ [「狀態」] => 字符串(1)「1」 } 但是,你能告訴我如何獲取Status列中包含的值嗎? – Keshav1234

+0

提取後我需要將該值發送到我的Java類 – Keshav1234

+0

我編輯了我的答案 –

相關問題