Forum Moderators: coopster

Message Too Old, No Replies

an array turned into database table names.

         

undream2

5:57 am on Mar 19, 2011 (gmt 0)

10+ Year Member




hi im trying to filter data i want from a .txt file. I can properly retreive the data I want, but I would like to take the data that is stored in the $output[0] array. and create a database table with each $output data as the names of the tables..


thanks for any help

preg_match_all("/[0-9a-zA-Z]+[0-9a-zA-Z]+[0-9a-zA-Z]+[0-9a-zA-Z\s]*:/", $input, $output);
$result = $output[0];
// output

foreach($result as $a) {

// connect to database
$connection = mysql_connect('127.0.0.1', 'root', '')
or die('Cannot connect to MySQL');

mysql_select_db('things')
or die('Cannot connect to database');

// clear the table down
$sql = "CREATE TABLE $a
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";


// run the first query to clear table
mysql_query($sql) or die(mysql_error());

Matthew1980

9:47 am on Mar 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there undream2,

Couple of things with this, firstly, you're connecting to the database within the loop which is a waste of processing power and CPU usage for the php parser.

Secondly, having declared the connection outside the loop, loop through the queries as you almost are doing - if all is well you should be able to do what your after.

Though looking at the code you have posted, you may want to change the default settings for your mysql database, always a good idea to change the root password (never leave it blank - even on a localhost environment) and then create a duplicate account with the same privileges and just give it an appropriate username/password (and change that every so often too).

So your code might look like this:- (Typed OTF my contain error's)

preg_match_all("/[0-9a-zA-Z]+[0-9a-zA-Z]+[0-9a-zA-Z]+[0-9a-zA-Z\s]*:/", $input, $output);
$result = $output[0];
// output

// connect to database
$connection = mysql_connect('127.0.0.1', 'root', '') or die('Connection Error:'.mysql_error());

mysql_select_db('things', $connection) or die('Database Error:'.mysql_error());

foreach($result as $a) {

//Pop an echo on the front so that you can see what the loop is producing too, this is good to see
echo $sql = "CREATE TABLE `".$a."`
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";

// run the first query to clear table
mysql_query($sql, $connection) or die("Query Error:".mysql_error());

}//end loop



I'm not commenting on the preg_match stuff as that is my programming Achilles heel :( so I can't tell if it's good or bad, but if its working then you should be ok, there may be ways of adding case insensitivity flags there but I'm not sure.

Hope that makes sense, happy coding & good luck.

Cheers,
MRb

undream2

12:31 pm on Mar 19, 2011 (gmt 0)

10+ Year Member



yes, this worked absolutely perfect. thanks a lot this place always has nice people. and, other good points on passwords, and obv not looping the mysql connection statements

Matthew1980

2:47 pm on Mar 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



^^^

Hi there undream2, glad you're all working now, have fun with the rest of your project.

Cheers,
MRb