0

How do I echo a simple string from a MySQL Query?

I'm trying trying to accomplish this with the following code but it is not working...The data I am pulling is fine so I know that my mysql_query is working (I've checked that via a different URL GET method.

<?php
$myQuery = mysql_query("fetch some stuff....");
$myResult = mysql_fetch_object($myQuery);
echo $myResult;
1
  • 2
    You may wish to elaborate on "is not working". There are two things wrong with your script as shown here. Please always post actual code. Commented Jun 28, 2012 at 1:21

4 Answers 4

2

you need to know what is returned type. in what your doing you assume that it printable but most of what db queries return are either in object form or an array

try doing a

echo "<pre>" ,print_r($myResult, TRUE),"</pre>";
Sign up to request clarification or add additional context in comments.

Comments

2

First of all use var_dump($myResult) to see the data and it's structure.

Since it's an object it will have properties named as the columns returned by the SELECT statement you used.

echo $myResult->column_name; // Should work fine

Usually if echo $variable; doesn't work it means that the variable is either en empty string '' or a null value NULL or a false value FALSE which all show "nothing" when echoed.

But when using var_dump() on them you get a report of the type of data and size of it.

Comments

0

Providing your query is correct, it looks like your php tags are incorrect:

<?php  ?>

P.S. It might help if you post the actual query so it can be troubleshooted here. It's hard to ask why something is not working and get an answer if you don't show any of it.

Comments

0

First, var_dump($myResult);. If you see NULL, your query is failing. If you see a big block o' jumbled text, the query is, in fact, working. Since you are echoing $myResult, it is no surprise that nothing is being output, as you are trying to echo the object directly rather than the property you want. Try echoing $myResult->myColumn;

Also, please use MySQLi or PDO, as php_mysql is deprecated.

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.