1

I want to output the results of a query in text on a page so I can see what it looks like. Problem is that I am receiving errors if I use an echo like I did below or even use a print.

The error I am receiving is something about not being able to convert an object into a string

My question is what is best way to output query results on the page?

Below is the code:

$answersql = "INSERT INTO Answer (Answer) 
    VALUES (?)";

      if (!$insertanswer = $mysqli->prepare($answersql)) {
      // Handle errors with prepare operation here
    }  

    $insertanswer->bind_param("s", $_POST['value'][$i]);

        //$insertanswer->execute();

        echo $insertanswer;

        if ($insertanswer->errno) {
          // Handle query error here
        }

        $insertanswer->close();
4
  • you're not trying to output the results there, you're trying to echo a pdo object. what exactly were you hoping for with this? Commented Oct 9, 2012 at 23:59
  • Here's a PDO guide. You might be looking for a way to check affected rows or something since that query above is an insert. phpro.org/tutorials/Introduction-to-PHP-PDO.html Commented Oct 10, 2012 at 0:02
  • PDO is nice, but he is using the MySQL-Improved driver, not PDO. Commented Oct 10, 2012 at 0:07
  • You can echo and print objects that have a __toString method that returns a string, but I don't think that's what you actually need here. What errors are you getting? Commented Oct 10, 2012 at 0:20

3 Answers 3

1

You aren't outputting the query result. It appears that you are trying to output the query itself.

However, $insertanswer is an instance of mysqli_statement. This class does not appear to offer access to the query.

That is why you cannot print it as a string. It isn't a string.

Sign up to request clarification or add additional context in comments.

Comments

0

You may try to type cast it using the string. for instance you declare a variable to hold the resultant conversion, like $answersql = "INSERT INTO Answer ((string)Answer)) VALUES (?)"; or you may type cast the assigned variable thus: (string)$answersql = "INSERT INTO Answer (Answer) VALUES (?)"; depending on how or where you need your result.

Comments

0

Couple items. On your binding, you'll want to assign $_POST to a variable first. I see you have an counter there, but don't see where you are iterating over any thing. I'll assume you can assign the $_POST['value'] to a scale variable.

 $input = $_POST['value'];

Then INSERT returns true or false and an ID if you have set an auto_increment attribute on your db table column. If so, you can then use

 $insertid = $insertanswer->insert_id;

However the way you have the statement written, it wouldn't return any value. Just true or false and an id (if auto_increment attribute is set).

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.