I have two arrays:
name - facebook,google,yahoo
url - facebook.com,google.com,yahoo.com
In my database, I have a table (tbl_websites).
structure : id, site_name, site_url
I want to insert the data from those two arrays into a single row in that table.
example:
(row 1)
id : 1
site_name : facebook
site_url : facebook.com
(row 2)
id : 2
site_name : google
site_url : google.com
(row 3)
id : 3
site_name : yahoo
site_url : yahoo.com
How do I do that?
Any help will be appreciated.
Assuming there is a direct 1:1 match between the arrays, you can simply loop over one of them as in:
This assumes the
idis an auto-increment value. I have, for simplicity brevity, used themysql_query()function here. But it is recommended to use a prepared statement via an api like PDO instead. The process of retrieving them from the arrays is the same.PDO version (assumes connection already made in
$db):The code above doesn’t include any error checking. Read the PDO manual for examples on error checking with a PDO statement. It can be done either with a
try/catchexception block or with PHP warnings.