2017-08-11 69 views
-1

我在我的SQL語法中遇到了問題,我希望它作爲字符串返回或至少顯示語法的結果。無法轉換爲字符串

我的代碼是:

<?php 
$host='localhost'; 
$user='root'; 
$password=''; 
$db='employee101'; 

$PLATE_NUM = 'ABC123'; 
$sql = "select * from employee_data where PLATE_NUM like '$PLATE_NUM';"; 

$con = mysqli_connect($host,$user,$password,$db); 

$result = mysqli_query($con, $sql); 

$response = array(); 

$myText = (string)$result; 

$row = mysql_fetch_array($result); 

echo $result->fetch_object()->memTotal; 



mysqli_close($con); 




?> 

是我得到的錯誤:

Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\wamp64\www\666.php on line 17

+0

爲什麼你把它鑄造成字符串! 更改這兩行: '$ myText = $ result; $ row = mysqli_fetch_array($ result);' – Mohammad

+0

什麼是字符串鑄造? – rtfm

回答

0

剛,閱讀錯誤信息,您不能在mysqli_result對象轉換成字符串。

刪除$myText = (string)$result;或將其更改爲正確的代碼。

取而代之的是:

$myText = (string)$result; 
$row = mysql_fetch_array($result); 
echo $result->fetch_object()->memTotal; 

我想,taht你想這樣的事情:

$row = mysqli_fetch_array($result); 
$myText = $row['my_text_colum']; 
echo $row['memTotal']; 
0

轉換

$myText = (string)$result; 
$row = mysql_fetch_array($result); 

$myText = $result; 
$row = mysqli_fetch_array($result); 

希望它能正常工作。

+0

你是對的,但錯誤來自'$ myText =(string)$ result;'其中OP試圖將$結果轉換爲字符串:) –

+0

TBH - 我沒有看到即使離開$ myText在線 - 它從來沒有用過! –