I am parsing xml file from url(in code below), using file_get_contents() function, and simpleXML, to insert data into the table, i did well, but i have problem with encoding(russian words) i get this ->Черногория; file and database encoding is set to utf-8;
require_once 'mysql_connect.php';
/**
*
*
*/
error_reporting(E_ALL);
$sql = "CREATE TABLE IF NOT EXISTS `db_countries` (
`id` int(11) unsigned NOT NULL auto_increment,
`countrykey` varchar(255) NOT NULL default '',
`countryname` varchar(255) NOT NULL default '',
`countrynamelat` varchar(500) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
mysql_query($sql);
$data = file_get_contents("http://www2.turtess-online.com.ua/export/dictionary/countries/");
$xml = new SimpleXMLElement($data);
echo $xml->body->dictionary->element["countryName"];
foreach ($xml->body->dictionary->element as $element) {
$countryname = mysql_real_escape_string($element["countryName"]);
$countrynamelat = mysql_real_escape_string($element["countryNameLat"]);
$countrykey = $element["countryKey"];
if ($countrykey) {
$q = $insert = 'INSERT INTO db_countries (countrykey, countryname, countrynamelat) VALUES ("' . $countrykey . '", "' . $countryname . '", "' . $countrynamelat . '")';
mysql_query($q);
} else {
echo "not valid key of country";
}
}
Make sure you insert Unicode content as well, database charset is not doing any "automagic" conversion.
As an alternative, I would suggest utf8_encode($countryname) as in :
if ($countrykey) {
$q = $insert = 'INSERT INTO db_countries (countrykey, countryname, countrynamelat) VALUES ("' . $countrykey . '", "' . cp1251_to_utf8($countryname) . '", "' . $countrynamelat . '")';
mysql_query($q);
} else {
echo "not valid key of country";
}
update : indeed, the XML source file shows a Windows 1251 charset
UPDATE(2) : i tested the code against this nifty little function and it works at last :)
function cp1251_to_utf8($s)
{
if ((mb_detect_encoding($s,'UTF-8,CP1251')) == "WINDOWS-1251")
{
$c209 = chr(209); $c208 = chr(208); $c129 = chr(129);
for($i=0; $i<strlen($s); $i++)
{
$c=ord($s[$i]);
if ($c>=192 and $c<=239) $t.=$c208.chr($c-48);
elseif ($c>239) $t.=$c209.chr($c-112);
elseif ($c==184) $t.=$c209.$c209;
elseif ($c==168) $t.=$c208.$c129;
else $t.=$s[$i];
}
return $t;
}
else
{
return $s;
}
}
credit goes to Martin Petrov
Related
I have been working on a page that stores opening hours of days of the week using html and php to the database. In order to use the data entries to compare it with the current time.. I did my best but its not working. I'm gonna show you the html, php, sql codes so you can tell me how can I solve it.
HTML+CSS Code of the form:
http://jsfiddle.net/Naz970/4y4sd91u/
SQL :
CREATE TABLE IF NOT EXISTS `markers` (
`id` int(11) NOT NULL,
`name` varchar(60) NOT NULL,
`phone` int(100) NOT NULL,
`address` varchar(80) NOT NULL,
`email` varchar(100) NOT NULL,
`link` varchar(200) NOT NULL,
`lat` float(10,6) NOT NULL,
`lng` float(10,6) NOT NULL,
`type` varchar(30) NOT NULL,
`days` varchar(100) CHARACTER SET utf8 NOT NULL,
`openingHours` varchar(255) NOT NULL,
`img` blob NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=51
PHP Code:
<?php
include ("../Connections/Connection.php");
if (isset($_POST["submit"])) {
$name = $_POST["name"];
$type = $_POST["type"];
$address = $_POST["address"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$link = $_POST["link"];
$lng = $_POST["lng"];
$lat = $_POST["lat"];
$days = implode(";", $_POST['days']);
$openingHours = implode(";", $_POST['openingHours']);
$img = time().$_FILES['photo']['name'];
if (!move_uploaded_file($_FILES['photo']['tmp_name'],'../Uploads/'.$img)) die("error");
$sql = "INSERT INTO markers (name, type, address, email, phone, link, lng, lat, days, openingHours, img)
VALUES('$name', '$type', '$address', '$email', '$phone', '$link', '$lng', '$lat', '$days', '$openingHours', '$img')";
$query = mysql_query($sql);
if (!$query) {
die("Error : " . mysql_error());
}
if (empty($name) || empty($type) || empty($address) || empty($email) || empty($phone) || empty($lng) || empty($lat) || empty($days) || empty($openingHours) || empty($img)) {
echo "You did not fill out the required fields.";
die(); // Note this
}
echo "<center></center>";
}
?>
But the form (the time table (schedule)) is not working and not storing the data properly. I am using it to do the following:
PHP code of the marker comparison
<?php
require("config.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Opens a connection to a mySQL server
$connection=mysql_connect ('localhost', $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$days = explode(";", $row['days']);
$openingHours = explode(";", $row['openingHours']);
$currentTime = date('H');
if (in_array($currentTime, $openingHours)) {
$status = 'Open';
}
else {
$status = 'Closed';
}
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo 'status="' . $status . '" ';
echo 'currentTime="' . $currentTime . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
?>
I know the code is long but this is the only way to show you so you can tell me how can fix the issue of time of opening hour and link it with the marker and the current time.
If you have any question dont hesitate to ask me
Thanks in advance
I have a problem while uploading database with key-connection between the tables.
Script works well as far as there is no connection between the tables.
What MySql command do I have to include in my php class to drop all foreign keys ? So I'd be able to drop the tables and then after all tables have been created add the key-connection back onto the tables.
Here it is the class I am using:
<?php
class BackupDB
{
private $host = '';
private $username = '';
private $passwd = '';
private $dbName = '';
private $charset = '';
function __construct($host, $username, $passwd, $dbName, $charset = 'utf8')
{
$this->host = $host;
$this->username = $username;
$this->passwd = $passwd;
$this->dbName = $dbName;
$this->charset = $charset;
$this->initializeDatabase();
}
protected
function initializeDatabase()
{
$conn = mysql_connect($this->host, $this->username, $this->passwd);
mysql_select_db($this->dbName, $conn);
if (!mysql_set_charset($this->charset, $conn))
{
mysql_query('SET NAMES ' . $this->charset);
}
}
/**
* Backup the whole database or just some tables
* Use '*' for whole database or 'table1 table2 table3...'
* #param string $tables
*/
public
function backupTables($tables = '*', $outputDir = '.')
{
try
{
/**
* Tables to export
*/
if ($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while ($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',', $tables);
}
$sql = 'CREATE DATABASE IF NOT EXISTS ' . $this->dbName . ";\n\n";
$sql.= 'USE ' . $this->dbName . ";\n\n";
/**
* Iterate tables
*/
foreach($tables as $table)
{
echo "Backing up " . $table . " table...";
$result = mysql_query('SELECT * FROM ' . $table);
$numFields = mysql_num_fields($result);
$sql.= 'DROP TABLE IF EXISTS ' . $table . ';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE ' . $table));
$sql.= "\n\n" . $row2[1] . ";\n\n";
for ($i = 0; $i < $numFields; $i++)
{
while ($row = mysql_fetch_row($result))
{
$sql.= 'INSERT INTO ' . $table . ' VALUES(';
for ($j = 0; $j < $numFields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n", "\\n", $row[$j]);
if (isset($row[$j]))
{
$sql.= '"' . $row[$j] . '"';
}
else
{
$sql.= '""';
}
if ($j < ($numFields - 1))
{
$sql.= ',';
}
}
$sql.= ");\n";
}
}
$sql.= "\n\n\n";
echo " OK" . "<br />";
}
}
catch(Exception $e)
{
var_dump($e->getMessage());
return false;
}
return $this->saveFile($sql, $outputDir);
}
/**
* Save SQL to file
* #param string $sql
*/
protected
function saveFile(&$sql, $outputDir = '.')
{
if (!$sql) return false;
try
{
$outputfilename = $outputDir . '/db-backup-' . $this->dbName . '-' . date("d.m.Y_H.i.s") . '.sql';
$result = mysql_query('INSERT INTO backuplog (backup) VALUES ("' . $outputfilename . '")');
$handle = fopen($outputfilename, 'w+');
fwrite($handle, $sql);
fclose($handle);
echo '<span class="message">Zapisano ' . $outputfilename . ' link do bazy. </span>';
echo '<span class="message">Właśnie pobrano kopie zapasową. Dziękujemy Serdecznie. Życzymy miłego dnia.</span>';
}
catch(Exception $e)
{
$result = mysql_query('DELETE FROM backuplog WHERE backup ="' . $outputfilename . '"');
var_dump($e->getMessage());
echo '<span class="error">Notacja.Udało się pobrać bazedanych... ale #NIE zaladowano linku do formularza, aby odzyskać dane trzeba ręcznie wpisać nazwę folderu/orazpliku.sql</span>';
echo '<span class="error">UWAGA! Wystąpił błąd podczas zapisywania danych w bazie... Zadanie nie ukończone.</span>';
return false;
}
return true;
} //end f
public
function loadDB($filename)
{
// $result=exec('mysql --user='.$this->user.' --password='.$this->pass .'<DatabaseBackUp/'.$filename);
// mysql -u user_name -p <file_to_read_from.sql
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '') continue;
// Add this line to the current segment
$templine.= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line) , -1, 1) == ';')
{
// Perform the query
if (!mysql_query($templine))
{
echo "Błąd ładownia pliku.sql";
return false;
} //echo $templine //TEST!
// Reset temp variable to empty
$templine = '';
}
}
return true;
} //end function
} //End calss
/* USE EXAMPLE
// DOWNLOAD DB FROM SERVER AS SQL FILE TO
$backupDatabase = new BackupDB(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$status = $backupDatabase->backupTables(TABLES, OUTPUT_DIR) ? 'OK' : 'KO';
echo "<br /><br /><br />Backup result: ".$status;
// RETRIVE DATA FROM SQL FILE TO DATABASE
$backupDatabase->loadDB("Path/to/mysqlfile.sql");
*/
?>
If you know how to extract foreign keys from a database, drop them one after the other, delete all tables and then put the keys back on again, please share your knowledge. Thanks again for looking into it.
Again for those who like to use this script it works fine... if! tables in database are not connected with foreign keys. Let's hope this state will soon change.
Short example of outputed .sql file:
CREATE DATABASE IF NOT EXISTS DATABASEONE;
USE DATABASEONE;
DROP TABLE IF EXISTS st_glowne_st_pages;
CREATE TABLE `st_glowne_st_pages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fk_glowne` varchar(50) COLLATE utf8_polish_ci NOT NULL,
`fk_pages` varchar(50) COLLATE utf8_polish_ci NOT NULL,
`kolejnosc` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`),
KEY `fk_glowne` (`fk_glowne`),
KEY `fk_pages` (`fk_pages`),
CONSTRAINT `st_glowne_st_pages_ibfk_1` FOREIGN KEY (`fk_glowne`) REFERENCES `st_glowne` (`name`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `st_glowne_st_pages_ibfk_2` FOREIGN KEY (`fk_pages`) REFERENCES `st_pages` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;
INSERT INTO st_glowne_st_pages VALUES("1","admin.php","pageadmin.php","1");
INSERT INTO st_glowne_st_pages VALUES("3","index.php","pageindex.php","1");
INSERT INTO st_glowne_st_pages VALUES("4","work.php","pagework.php","1");
INSERT INTO st_glowne_st_pages VALUES("7","register.php","pageregister.php","1");
INSERT INTO st_glowne_st_pages VALUES("8","login.php","pagelogin.php","1");
I haven't tried this myself, but apparently you can turn checking off.
SET FOREIGN_KEY_CHECKS = 0;
you'll need to read all the foreign key constraints and also store them in your backup file if you want to recreate them afterwards.
see this answer for some more clues.
you really should consider using sqldump though ...
I have the following function:
function insert($database, $table, $data_array)
{
# Connect to MySQL server and select database
$mysql_connect = connect_to_database();
mysql_select_db ($database, $mysql_connect);
# Create column and data values for SQL command
foreach ($data_array as $key => $value)
{
$tmp_col[] = $key;
$tmp_dat[] = "'$value'";
}
$columns = join(",", $tmp_col);
$data = join(",", $tmp_dat);
# Create and execute SQL command
$sql = "INSERT INTO ".$table."(".$columns.")VALUES(". $data.");";
$result = mysql_query($sql, $mysql_connect);
# Report SQL error, if one occured, otherwise return result
if(mysql_error($mysql_connect))
{
echo "MySQL Update Error: ".mysql_error($mysql_connect);
$result = "";
}
else
{
return $result;
}
}
The values in php are the following:
$content_table = "p_content";
$insert_array['title'] = $title;
$insert_array['content'] = $content;
$insert_array['url'] = $get_source;
$insert_array['video'] = $video;
$insert_array['date'] = $date;
insert(DATABASE, $content_table, $insert_array);
The result of all this adds a row with id (key, autoimcrement), url, and date. Title, content and video are blank. If I echo title I get the correct result, if i var_dump the title I get string(15)"blablablabla", again correct.
Now if I hand set the $title = "asdf"; it is getting inserted correctly. Same goes for content and video.
table structure
id int(8) unsigned NO PRI NULL auto_increment
title varchar(1000) YES NULL
content longtext YES NULL
video varchar(3000) YES NULL
url varchar(300) YES NULL
date date YES NULL
Try adding quotes to your variables. :-) The reason is that you MYSQL column types are set as VARCHAR. And inserting data requires that you surround your inserts with quotes.
B.T.W. if this is new code I would recommend to switch to the MYSQLI or PDO library.
Try:
function insert($database, $table, $data_array)
{
# Connect to MySQL server and select database
$mysql_connect = connect_to_database();
mysql_select_db($database, $mysql_connect);
$cols = array();
$vals = array();
foreach ($data_array as $key => $value) {
$cols[] = "`" . $key . "`";
if (is_int($value) || is_float($value)) {
$vals[] = $value;
} else {
$vals[] = "'" . mysql_real_escape_string($value) . "'";
}
}
$sql = "INSERT INTO " . $table
. ' (' . implode(', ', $cols) . ') '
. 'VALUES (' . implode(', ', $vals) . ')';
$result = mysql_query($sql, $mysql_connect);
# Report SQL error, if one occured, otherwise return result
if(mysql_error($mysql_connect)) {
echo "MySQL Update Error: " . mysql_error($mysql_connect);
$result = ""; // FIXME should probably return false here
} else {
return $result;
}
}
IMPORTANT
Your code and the above is potentially vulnerable to SQL Injections. Go read about them.
I have a database containing three tables:
practices - 8 fields
patients - 47 fields
exacerbations - 11 fields
The majority of the fields in these tables are recorded in varchar format, other fields include integers, doubles and dates.
I have to transform this data into numerically classified data so that it can be used by a statistician to extrapolate any patterns in the data. To acheive this I will have to convert varchar fields into integers that represent the classification that string belongs to, an example being 'Severity' which has the following possible string values:
Mild
Moderate
Severe
Very Severe
This field in the patients table has a finite list of string values that can appear, other fields have an endless possibility of string values that cannot be classified until they are encountered by my database (unless I implement some form of intelligent approach).
For the time being I am just trying to construct the best approach to converting each field for all entries in each of the 3 tables to numeric values. The pseudo code I have in my head so far is as follows (it's not complete):
function profileDatabase
for each table in database
for each field that is of type varchar
select all distinct values and insert into classfication table for that field
end for
end for
function classifyDatabase
for each table in database
for each field that is of type varchar
// do something efficient to build an insert string to place into new table
end for
end for
Can someone suggest the best way of performing this process so that it is efficient giving that there are currently in excess of 100 practices, 15,000 patients and 55,000 exacerbations in the system. I have no need to implement this in PHP, build I would prefer to do so. Any pointers as to how to structure this would be great as I am not sure my approach the best approach.
This process will have to run every month for the next two years as the database grows to have a total of 100,000 patients.
Maybe http://dev.mysql.com/doc/refman/5.1/en/procedure-analyse.html will help to get an overview to find out how fields are used.
I have managed to build my own solution to this problem which runs in reasonable time. For anyone interested, or anyone who may encounter a similar issue here is the approach I have used:
A PHP script that is run as a cron job by calling php scriptName.php [database-name]. The script builds a classified table for each table name that is within the database (that is not a lookup table for this process). The setting up of each classification creates a new table which mimics the format of the base table but sets all fields to allow NULL values. It then creates blank rows for each of the rows found in the base table. The process then proceeds by analysing each table field by field and updating each row with the correct class for this field.
I am sure I can optimise this function to improve on the current complexity, but for now I shall use this approach until the run-time of the scripts goes outside of an acceptable range.
Script code:
include ("../application.php");
profileDatabase("coco");
classifyDatabase("coco");
function profileDatabase($database) {
mysql_select_db($database);
$query = "SHOW TABLES";
$result = db_query($query);
$dbProfile = array();
while ($obj = mysql_fetch_array($result)) {
if (!preg_match("/_/", $obj[0])) {
$dbProfile[$obj[0]] = profileTable($obj[0]);
}
}
return $dbProfile;
}
function profileTable($table) {
$tblProfile = array();
$query = "DESCRIBE $table";
$result = db_query($query);
while ($obj = mysql_fetch_array($result)) {
$type = substr($obj[1], 0, 7);
//echo $type;
if (preg_match("/varchar/", $obj[1]) && (!preg_match("/Id/", $obj[0]) && !preg_match("/date/", $obj[0]) && !preg_match("/dob/", $obj[0]))) {
$x = createLookup($obj[0], $table);
$arr = array($obj[0], $x);
$tblProfile[] = $arr;
}
}
return $tblProfile;
}
function getDistinctValues($field, $table) {
$distinct = array();
$query = "SELECT DISTINCT $field as 'value', COUNT($field) as 'no' FROM $table GROUP BY $field ORDER BY no DESC";
$result = db_query($query);
while ($obj = mysql_fetch_array($result)) {
$distinct[] = $obj;
}
return $distinct;
}
function createLookup($field, $table) {
$query = "CREATE TABLE IF NOT EXISTS `" . $table . "_" . $field . "`
(
`id` int(5) NOT NULL auto_increment,
`value` varchar(255) NOT NULL,
`no` int(5) NOT NULL,
`map1` int(3) NOT NULL,
`map2` int(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1";
db_query($query);
$distinct = getDistinctValues($field, $table);
$count = count($distinct);
foreach ($distinct as $val) {
$val['value'] = addslashes($val['value']);
$rs = db_query("SELECT id FROM " . $table . "_" . $field . " WHERE value = '" . $val['value'] . "' LIMIT 1");
if (mysql_num_rows($rs) == 0) {
$sql = "INSERT INTO " . $table . "_" . $field . " (value,no) VALUES ('" . $val['value'] . "', " . $val['no'] . ")";
} else {
$sql = "UPDATE " . $table . "_" . $field . " (value,no) VALUES ('" . $val['value'] . "', " . $val['no'] . ")";
}
db_query($sql);
}
return $count;
}
function classifyDatabase($database) {
mysql_select_db($database);
$query = "SHOW TABLES";
$result = db_query($query);
$dbProfile = array();
while ($obj = mysql_fetch_array($result)) {
if (!preg_match("/_/", $obj[0])) {
classifyTable($obj[0]);
//echo "Classfied $obj[0]\n";
}
}
}
function classifyTable($table) {
$query = "SHOW TABLES";
$result = db_query($query);
$dbProfile = array();
$setup = true;
while ($obj = mysql_fetch_array($result)) {
if ($obj[0] == "classify_" . $table)
$setup = false;
}
if ($setup) {
setupClassifyTable($table);
//echo "Setup $table\n";
}
$query = "DESCRIBE $table";
$result = db_query($query);
while ($obj = mysql_fetch_array($result)) {
if (preg_match("/varchar/", $obj[1]) && (!preg_match("/Id/", $obj[0]) && !preg_match("/date/", $obj[0]) && !preg_match("/dob/", $obj[0]))) {
$rs = db_query("
SELECT t.entryId, t.$obj[0], COALESCE(tc.map1,99) as 'group' FROM $table t
LEFT JOIN " . $table . "_$obj[0] tc ON t.$obj[0] = tc.value
ORDER BY tc.map1 ASC");
while ($obj2 = mysql_fetch_object($rs)) {
$sql = "UPDATE classify_$table SET $obj[0] = $obj2->group WHERE entryId = $obj2->entryId";
db_query($sql);
}
} else {
if ($obj[0] != "entryId") {
$rs = db_query("
SELECT t.entryId, t.$obj[0] as 'value' FROM $table t");
while ($obj2 = mysql_fetch_object($rs)) {
$sql = "UPDATE classify_$table SET $obj[0] = '" . addslashes($obj2->value) . "' WHERE entryId = $obj2->entryId";
db_query($sql);
}
}
}
}
}
function setupClassifyTable($table) {
$tblProfile = array();
$query = "DESCRIBE $table";
$result = db_query($query);
$create = "CREATE TABLE IF NOT EXISTS `classify_$table` (";
while ($obj = mysql_fetch_array($result)) {
if (preg_match("/varchar/", $obj[1]) && (!preg_match("/Id/", $obj[0]) && !preg_match("/date/", $obj[0]) && !preg_match("/dob/", $obj[0]))) {
//echo $obj[1]. " matches<br/>";
$create .= "$obj[0] int(3) NULL,";
} else {
$create .= "$obj[0] $obj[1] NULL,";
}
}
$create .= "PRIMARY KEY(`entryId`)) ENGINE=MyISAM DEFAULT CHARSET=latin1";
db_query($create);
$result = mysql_query("SELECT entryId FROM $table");
while ($obj = mysql_fetch_object($result)) {
db_query("INSERT IGNORE INTO classify_$table (entryId) VALUES ($obj->entryId)");
}
}
?>
how can I parse php array like this:
$cars= array(
"Ford"=>array("C-Max"=>array("length"=>"4333","width"=>"1825","height"=>"1560"),"Escape"=>array("length"=>"4480","width"=>"1845","height"=>"1730")
,"Explorer"=>array("length"=>"4912","width"=>"1872","height"=>"1849"),"Fiesta"=>array("length"=>"3950","width"=>"1973","height"=>"1433")
,"Focus"=>array("length"=>"4488","width"=>"1840","height"=>"1497"),"Fusion"=>array("length"=>"4013","width"=>"1724","height"=>"1543")
,"Galaxy"=>array("length"=>"4820","width"=>"1854","height"=>"1723"),"Kuga"=>array("length"=>"4443","width"=>"1842","height"=>"1677")
,"Mondeo"=>array("length"=>"4844","width"=>"1886","height"=>"1500"),"Ranger"=>array("length"=>"5075","width"=>"1805","height"=>"1745")
,"S-Max"=>array("length"=>"4768","width"=>"1854","height"=>"1658"),
"Hummer"=>array("H2"=>array("length"=>"5170","width"=>"2063","height"=>"2012"),"H3"=>array("length"=>"4782","width"=>"1989","height"=>"1872")));
to insert into MySQL table like this:
CREATE TABLE IF NOT EXISTS `cars_dimensions` (
`id` int(10) NOT NULL auto_increment,
`brand` varchar(120) character set utf8 NOT NULL,
`model` varchar(120) character set utf8 NOT NULL,
`length` varchar(5) character set utf8 NOT NULL,
`width` varchar(5) character set utf8 NOT NULL,
`height` varchar(5) character set utf8 NOT NULL,
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
When I use foreach and like $cars["Hummer"]["H2"]["length"]; I somehow can't get another array dimension and cache the actual brand/model at the same time... Tho, my actual array is about 3000 items in the first dimension (brands).
You need two loops, one for the brands and one for their models:
foreach ($cars as $brand => $models) {
foreach ($models as $model => $specs) {
$query = "INSERT INTO cars_demensions (brand, model, length, weight, height)
VALUES ('$brand', '$model', {$specs['length']}, {$specs['width']}, {$specs['height']});";
}
}
foreach ( $cars as $brandname => $carinfo )
{
foreach ( $carinfo as $modelname => $car )
{
// brand = $brandname
// model = $modelname
// length = $car['length']
// do insert query here
}
}
$rows = '';
foreach($cars AS $brand) {
foreach($brand AS $model) {
if(!empty($rows)) $rows .= ', ';
$rows = "({$car['width']},...)";
}
}
$sql = "INSERT INTO cars_dimensions (width,...) VALUES $rows";
Full executable code and TESTED. This is more efficient and faster way to do this. Using this way you can insert multiple row using a single insert query.
<?php
$col = [];
foreach ($cars as $brand => $models) {
foreach ($models as $model => $specs) {
if (isset($specs['length']) || isset($specs['width']) || isset($specs['height'])) {
$length = $specs['length'];
$width = $specs['width'];
$height = $specs['height'];
} else {
foreach ($specs as $k => $v) {
$length = $v['length'];
$width = $v['width'];
$height = $v['height'];
}
}
$col[] = "('" . $brand . "', '" . $model . "', '" . $length . "', '" . $width . "', '" . $height . "')";
}
}
$query = "INSERT INTO `cars_dimensions`(`brand`, `model`, `length`, `width`, `height`) VALUES" . implode(',', $col) . ";";
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec($query);
} catch (PDOException $e) {
echo $e->getMessage();
}
?>
Hopefully it should help you. Thank you.