save variable to txt file in php

Associate
Joined
8 Mar 2007
Posts
2,176
Location
between here and there
Hey guys,

I'm stuck on a little project I'm working on.

Basically, I need to save my $variable to a text file, but I don't have much knowledge.

So far I've got this;

Code:
$stuff_to_store = serialize($my_varaible);

$handle = fopen('file.txt','w+');

fwrite($handle, $stuff_to_store);

But instead of writing "7" like it should, I'm getting "b:0;"

I'd also like to confirm that the value of $variable is indeed 7. How can I echo it to screen??

I've tried "echo $variable" but that does nothing.

(I'm running xp with wamp installed and I'm running my php script from a command prompt)

Any ideas??

Cheers
 
Hey guys,

I'm stuck on a little project I'm working on.

Basically, I need to save my $variable to a text file, but I don't have much knowledge.

So far I've got this;

Code:
$stuff_to_store = serialize($my_varaible);

$handle = fopen('file.txt','w+');

fwrite($handle, $stuff_to_store);

But instead of writing "7" like it should, I'm getting "b:0;"

I'd also like to confirm that the value of $variable is indeed 7. How can I echo it to screen??

I've tried "echo $variable" but that does nothing.

(I'm running xp with wamp installed and I'm running my php script from a command prompt)

Any ideas??

Cheers
serialize — Generates a storable representation of a value
http://uk.php.net/serialize

unserialize — Creates a PHP value from a stored representation
http://uk.php.net/unserialize

So .... untested.....

If you do file_get_contents() or what ever into $var

then do

echo unserialize($var)

and you should have the number 7.

Hope this is right, its not tested but its what I under stand from reading the manual.
 
cheers for the reply.

I've managed to get connected to my remote mysql server OK, but I'm having trouble with getting values from the database.

I have a table called 'main'. in this table there is s column called 'id'. I want to know what the max number is in this column. I have the sql statement right, but it returns a 'resource' - "mysql query"

How can I see the value that the sql statement produces???

Cheers in advance
 
Sorry, lost my password....


You need to do...

$data = mysql_fetch_assoc($query);

OR

while ($data = mysql_fetch_assoc($query) {
//processing
}

to get the data out into string format, as at the moment its just a pointer to a mysql set of data which it doesn't understand.
 
Back
Top Bottom