2

i want to insert data from multidimensional array into mysql but while looping it loop more than necessary. i mean i want it to enter 6 records but i got many records. i knew i have problem with my mysql query location but have tried many techniques but all in void. this is my code please help me

<?php
$primary = array(
    "array 1"=> array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
    "array 2 "=> array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
    "array  3" =>  array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
    "array  4" => array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
    "array  5" =>  array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
    "array  6" =>  array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
);
foreach($primary as $t => $value){
    $class =$t;
    $clas = $primary[$t]["clas"];
    $pupil =  $primary[$t]["pupil"];
    $sub =$primary[$t]["subject"];
    mysql_query("insert into tablename( f1, f2, f3) values('$clas','$pupil','$sub')");
}
3
  • This would only ever insert 6 records... but unless you delete the inserts done by previous test runs of the script, you'll keep adding 6 records each time. Commented Jul 21, 2011 at 21:52
  • "i want it to enter 6 records but i got many records" smells like you need to define some unique key for your data, or create a unique index over the whole... Commented Jul 21, 2011 at 21:54
  • thanks but i dont know how i will define the unique key because am new to php and programing. Commented Jul 22, 2011 at 0:12

2 Answers 2

2

You might try serializing them and storing them as a blob. Serializing would turn them into a giant array of bytes that you could retrieve and unserialize.

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

4 Comments

Unless you don't query for the data, ever, this is about the worst solution I could come up with. How many rows & fields will that table have? 1 & 1? What if I only want those records with a specific pupil value? I have to get them all out of the blob and than loop through all of them with script?
Oh it's awful I know. I'm just saying it would be cool if it worked. You should really have an identifier linking to a separate table for a variable sized array like this. Even one table with a static amount of columns and just leave the ones you don't use empty would be better than what I suggested. But he asked that question and I gave the simplest answer I could.
Please only give those kinds of advice after you've established with 100% certainty I never have to work with the people that take it, or ever have to see their code... It almost physically hurts...
thanks for the insight, i now get by defining a counter function,sub array and insert function.then it works fine
0

Serialize the array isn't actually the badest idea if you don't need to search for the data. Another option is to choose another data structure for the data for example a binary tree and an object. The object you can serialze and unserialize and because it's a binary tree you can search it very fast in memory. It's a thing I actually think of myself.

6 Comments

If you don't need to search them why use a database at all? Save the data into a file.
ypercube: Because I can and also I want to use a binary tree to store and search for the data. I've never used such a data structure before and if it's not hundreds of megabytes I can be very efficient, too.
I really don't understand your point. Do you want to search/query the data or not? Do you propose using the database for that or a custom made binary tree in memory?
I need to search the data but it's also many attributes so I think to use a binary tree and serialize it. I don't need the database, too, but it has some useful functions over a file.
So long as he's only storing strings, even serialized it should still be legible and even searchable by the LIKE operator I think. Could be wrong on this, might depend on the encoding.
|

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.