简体   繁体   中英

Echoing string from a mysql query - PHP

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;

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.

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>";

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

<?php  ?>

PS 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM