0

I am willing to insert an array to a database as normal text.

$subjectcodes[1] = "";
$subjectcodes[2] = "";
$subjectcodes[3] = "";
$subjectcodes[4] = "";
$subjectcodes[5] = "";
$subjectcodes[6] = "";
$subjectcodes[7] = "";

etc...

I give my array an stringname called list_update. So you get this ( I am having "\" so it doesn't see it as PHP )

    $list_update="
    \$subjectcodes\[1\] = \"\";
    \$subjectcodes\[2\] = \"\";
    \$subjectcodes\[3\] = \"\";
    \$subjectcodes\[4\] = \"\";
    \$subjectcodes\[5\] = \"\";
    \$subjectcodes\[6\] = \"\";
    \$subjectcodes\[7\] = \"\";
"

Now when I use my query to input my list into a table it doesn't do anything.

 mysqli_query($connect,"INSERT INTO subjects (list)
        VALUES (" . $list_update . ") WHERE user='". $userid ."'");

Does someone know how I can solve this?

Thank you very much in advance ;)

6
  • 1
    So what does mysqli_error() tell you? Please also learn to always either use mysqli_escape_string() or prepared statements. Commented Feb 3, 2014 at 17:59
  • @johannes It says: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[1] = ""; $subjectcodes[2] = ""; $subjectcodes[3] = "' at line 3 Commented Feb 3, 2014 at 18:02
  • I think, its a really bad idea to store it in array for the 1st place, because retrieving & querying data wont be efficient. Commented Feb 3, 2014 at 18:03
  • @AkhilSidharth Well do you know another way to store an "array" in mysql. And I can retrieve the queries easy and use it after doing eval() But storing is the problem :( Commented Feb 3, 2014 at 18:04
  • 1
    json encode your array and insert into db. When you need it , fetch it from db and then again json decode it and you will have your original array. Commented Feb 3, 2014 at 18:08

2 Answers 2

3

A PHP array or object or other complex data structure cannot be transported or stored or otherwise used outside of a running PHP script. If you want to persist such a complex data structure beyond a single run of a script, you need to serialize it. That just means to put the structure into a "lower common denominator" that can be handled by things other than PHP, like databases, text files, sockets. The standard PHP function serialize is just a format to express such a thing, it serializes a data structure into a string representation that's unique to PHP and can be reversed into a PHP object using unserialize. There are many other formats though, like JSON or XML.

From here

Sign up to request clarification or add additional context in comments.

Comments

2

Do it as below:

$subjectcodes[1] = "";
$subjectcodes[2] = "";
$subjectcodes[3] = "";
$subjectcodes[4] = "";
$subjectcodes[5] = "";
$subjectcodes[6] = "";
$subjectcodes[7] = "";

json_encode($subjectcodes);

Insert into your DB.

After you fetch it from DB, then decode it as below:

$array = json_decode($yourArrayItemFromDB);

ANd you will have your original array like $subjectcodes.

Using this way, you can not only save arrays, but you can also save objects in DB.

Comments

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.