How to create new tables with PHP in MySQL?
I've got a loop which goes through various products being imported. Within this loop I also have a foreach which goes th开发者_开发技巧rough each property of the product class and assigns values from a file to the current instance's properties, to be displayed later.
while(true)
{
$currentProduct = new productImport();
$line = fgets($fileHandle);
if($line == null) break;
$cells=explode(' ', $line);
$i=0;
foreach($currentProduct as &$ProductProperty)
{
if(isset($cells[$i]))
{
$ProductProperty = $cells[$i];
}
else return false;
$i++;
}
$AddedProducts[]=$currentProduct;
}
fclose($fileHandle);
return true;
Now that I've got that working I want to change this to instead create a table, unless one already exists, based on one property of each product. More specifically, the product class contains the value $Vendor_Name;
Using PDO (PHP Database Objects), how do I create a new table named $Vendor_Name if it doesn't exist and put values in there instead of this array?
Edit: If it helps, the first value of each instance is a unique key.
PHP has an object that will help you create a table and has an additional parameter "IF NOT EXISTS" that says "if the table not exists, then create it".
Here is a code syntax:
CREATE TABLE IF NOT EXISTS `" . $Vendor_Name . "` (
`media_id` int(11) NOT NULL AUTO_INCREMENT,
`filename` varchar(50) NOT NULL,
`thumb` varchar(50) NOT NULL,
PRIMARY KEY (`media_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
精彩评论