Convert to String

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
afr_dnf2011
Forum Newbie
Posts: 14
Joined: Mon Mar 21, 2011 8:20 pm

Convert to String

Post by afr_dnf2011 »

Is there any way to convert Resource id # to a string ?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Convert to String

Post by social_experiment »

Resource id # can be a result received from a mysql query. You have to pass the resource id to something like mysql_fetch_array().

Code: Select all

<?php
 $query = mysql_query("SELECT * FROM table");
 echo $query; 
 // echoes 'Resource id #
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
afr_dnf2011
Forum Newbie
Posts: 14
Joined: Mon Mar 21, 2011 8:20 pm

Re: Convert to String

Post by afr_dnf2011 »

social_experiment wrote:Resource id # can be a result received from a mysql query. You have to pass the resource id to something like mysql_fetch_array().
Dear social experiment, thanks for your valuable reply. Yes, you are correct. now, Let's check the code below.

Code: Select all

$file = fopen("myfile.txt", "w");
var_dump($file);
Isn't the variable $file is resource id # type?
User avatar
afr_dnf2011
Forum Newbie
Posts: 14
Joined: Mon Mar 21, 2011 8:20 pm

Re: Convert to String

Post by afr_dnf2011 »

Just look at the twist below.

Code: Select all

$file = fopen("myfile.txt", "w");
var_dump($file); //resource(3) of type (stream) (output)
echo "<br>";
fclose($file);
var_dump($file); // resource(3) of type (Unknown) (output)
Why?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Convert to String

Post by social_experiment »

With twist, are you refering to the second result when the value of $file is dumped? You are closing the file pointer in the previous line so it makes sense that you would get the result you are getting.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply