mysql查询返回对象,但我需要String

时间:2014-04-29 22:10:33

标签: php mysql sql

我有一个表格的网站。用户首先在数据库中输入一个名称和电子邮件(我已经开始工作),然后他可以输入一个名称在数据库中搜索并通过电子邮件获取照片。我有$ return值,但它是一个Object,在图像创建方法中我需要它是一个String。我试图用演员表转换它,但它不起作用。 (我已尝试先输出它)

这是HTML:

<form name="getEmail"
action="getEmail.php"
method="GET">
Name: <input type="text" name="getEmail" id="getEmail" value="Email..." maxlength="100"         size="20" onclick="this.value=''" />

这是PHP:

<?php
$con=mysqli_connect("localhost","root","","assignment1");

if (mysqli_connect_errno()) {
echo "Could not connect to the mySQL database: " . mysqli_connect_error();
}



// create a 100*30 image
$im = imagecreate(100, 30);

// white background and blue text
$bg = imagecolorallocate($im, 120, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);

$name=$_GET['getEmail'];
$result = mysqli_query($con,"SELECT email FROM Users WHERE name='$name' ");


echo (String)$result;
// write the string at the top left
imagestring($im, 5, 0, 0,$result, $textcolor);

// output the image
header("Content-type: image/png");
imagepng($im);


mysqli_close($con);
?>

2 个答案:

答案 0 :(得分:3)

您尚未提取结果,这就是您获得mysqli_result个对象的原因。使用mysqli_fetch_assoc()mysqli_fetch_array()

$result = mysqli_query($con,"SELECT email FROM Users WHERE name='$name' ");
while ($row = mysqli_fetch_array($result))
      echo $row['email'];

答案 1 :(得分:0)

使用此:

$q = mysqli_query($con,"SELECT *
FROM Users WHERE
name='$name' ");

$ar = mysqli_fetch_assoc($q);
$result = $ar['email'];

echo $result;

使用时:

$result = mysqli_query
($con,"SELECT email
FROM Users WHERE
name='$name' ");
echo (String)$result;

您实际上并没有为该行选择“email”列的值,而是选择完整对象。 你想要做的是从行中选择所有内容,并从中形成一个数组。 这样,您就可以从此数组中选择“电子邮件”值。