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在线 - 它从来没有用过! –