Jump to content

output select statement


jimfog

Recommended Posts

I am trying to echo the result of a select statement, but I cannot, here is the statement:

$conn = db_connect();  $result=$conn->query("select username from hairdressers where username='".$username."'");echo $result;

and here is the message I getObject of class...could not be converted to string. What should I do to convert the object into a string and subsequently print it to the browser?

Link to comment
Share on other sites

use mysql_fetch_array(). Then var_dump(). http://www.w3schools...fetch_array.asp Echo doesn't work on arrays other than to ID them as an array. It's one of the most used php functions. If you're selecting more than one row you'll have to use a while loop.

while ($result2 = mysql_fetch_array($result)) {	var_dump($result2) . '<br/>';}

Edited by niche
Link to comment
Share on other sites

why var_dump is needed? I run the code below:

echo mysql_fetch_array($result);

and I got the following message: Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in . That happens of course because mysql_fetch_array does not work on objects. Is that the reason you I should use var_dump-to overcome the above issue?

Link to comment
Share on other sites

I get the same error again: Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in and plus that in the browser i see the the word null printed. No results so far. Just to remind you, here is the sql statement I want to run:

 $result=$conn->query("SELECT username FROM `hairdressers` WHERE id=1");

Link to comment
Share on other sites

Then I just learned something too. Sorry, I cant help you then. I'll follow and see how this topic gets resolved.

Link to comment
Share on other sites

I finally used fetch_object() which did the job after all.

  • Like 1
Link to comment
Share on other sites

Very zen Jimfrog (answering your own question)! I don't use objects and didn't know they change the nature of a mysql resource. For everything there is a first time.

Edited by niche
Link to comment
Share on other sites

Very zen Jimfrog (answering your own question)! I don't use objects and didn't know they change the nature of a mysql resource. For everything there is a first time.
mysql_connect() returns db resource. mysqli::query() returns result object which is not same as mysql resource. different api returns different object like PDO::query() would return pdostatement object. resource and object are two different data types in php. resource and object are two diferent data types in php. Edited by birbal
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...