how to insert array without key to mysql?

I have array

Array
(
    [0] => john
    [1] => melinda
)

And have database table


|id|name|

How I can insert the name into my database using php?

I can do using looping on mysql_query, but it not good for my resource.

the code I made now is

foreach( $data as $row ) {
    mysql_query("INSERT INTO badwords (word) VALUES('".$row."')");
    }

it work but I think there is another simple way.

Thank you

Answers


You can do a batch insert instead of looping if you'd like.

"INSERT INTO badwords (word) VALUES ('".implode( "'),('", $data)."')";

Not tested, But this should produce something like

"INSERT INTO badwords (word) VALUES ('john'),('melinda')

you can try batch insert:

$queryValues = '';
foreach ($data as $row)
    $queryValues .= (($queryValues!='')?',':'').'(\''.mysql_escape_string($row).'\')';
if ($queryValues)
    mysql_query('INSERT INTO badwords (word) VALUES' . $queryValues);

NOTE: do not use mysql, use mysqli or pdo extensions


Need Your Help

Guess a number game, score counter giving incorrect result

javascript jquery html

I am making a simple 1 - 10 guess the number game with Javascript.

UIButton and UIControlEventState issue

iphone objective-c button

I'm having a very specific "bug" in my iPhone application. I'm setting two images for the highlighted and normal states of a button. It works as expected when you "press" and then "touch up" at a s...