0

Is there a way I could retrieve data from a mysql db table and save them in a binary file?

My table looks like this

id  domain_name  isAuth
1   www.yah.com  1
2   www.go.com   0
3   www.goo.com  1
4   www.foo.com  1
5
  • i have been using writing to a file(fwrite) and reading(fread) from concept for one of my jobs.. it seems a bit time consuming, so i saw a suggestion in the internet to use a binary file.. no technical information.. put it on stack to get a head start with some help from programming expeerts Commented Jun 1, 2015 at 9:00
  • It depends on what you want to do with the data after saving it to a file. Commented Jun 1, 2015 at 9:04
  • cant you guys give me a simple example of doing above or anything related to above Commented Jun 1, 2015 at 9:06
  • 1
    No. Your question is vague and incomplete, and thus cannot be answered. I can give you a hundred examples of "writing data to a file", but then you just have a file that contains data you can't read. In that case it's better not to create the file in the first place. So, again: What do you want with that file? Do you want to read it again in PHP? Do you want to send it over the internet? Do you want to be able to restore your database from it? Commented Jun 1, 2015 at 9:24
  • thanks for the explanation, what i want to do is, get the table data into binary format and read it again using php.. Commented Jun 1, 2015 at 9:32

1 Answer 1

1

Since performance seems to be the issue here, assuming $data is the result of a call to PDOStatement::fetchAll or mysqli_fetch_all, I suggest serialize and unserialize:

/* fetch $data */
file_put_contents('my_file.dat', serialize($data));
/* do some other stuff */
$data = unserialize(file_get_contents('my_file.dat'));
/* use $data */
Sign up to request clarification or add additional context in comments.

1 Comment

@SoththiUpali With pack you could write actual binary data, thus creating a much denser file. In a language with reasonable performance (like C or Java) you would normally do this because it has better performance than anything else and the resulting file is no larger than needed. However, writing a function in PHP that iterates over an array and packs it is most likely a lot slower than using a built-in function like serialize, which is written in C.

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.