0

I have a website with a form. The user first inputs a name and email into the database (I've got this working) and then he can input a name to search in the database and get a photo with the email. I've got the $return value but it is an Object, and in the image creation method I need it to be a String. I've tried to convert it with a cast but it doesn't work. (I've tried outputing it first)

Here's the 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=''" />

Here's the 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);
?>
3
  • check the docu on mysqli_query and maybe mysqli_fetch_array. Then you will figure it out. Commented Apr 29, 2014 at 22:16
  • Also, see prepared statements Commented Apr 29, 2014 at 22:21
  • By the way, you are currently not protected from SQL injection. You should bind the params or use mysqli_real_escape_string() instead. Commented Apr 29, 2014 at 22:21

2 Answers 2

3

You have not fetched your result that's why you are getting a mysqli_result object .Use mysqli_fetch_assoc() to mysqli_fetch_array() :

$result = mysqli_query($con,"SELECT email FROM Users WHERE name='$name' ");
while ($row = mysqli_fetch_array($result))
      echo $row['email'];
Sign up to request clarification or add additional context in comments.

1 Comment

@DrumPower3004 : Glad to help you!Mark as answered
0

Use this:

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

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

echo $result;

When you use:

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

You aren't actually selecting the value of the 'email' column for that row, but the full object. What you want to do is select everything from the row, and form an array from this. That way you can then select the 'email' value from this array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.