Solved

I've got to create a table on the fly based on this loop. How?

Posted on 2014-09-26
4
93 Views
Last Modified: 2014-09-26
Here's my code:

$jorja = "select distinct posted_day from verizon order by posted_day";
$jorja_query=mysqli_query($cxn, $jorja)
or die("Jorja didn't happen.");
while($jorja_row=mysqli_fetch_assoc($jorja_query))
{
  $harry = "CREATE TABLE IF NOT EXISTS $jorja_row['posted_day'] (
    `id` int(11) NOT NULL AUTO_INCREMENT,
  `actor_id` varchar(150) NOT NULL,
  `actor_display_name` varchar(150) NOT NULL,
  `posted_time` varchar(150) NOT NULL,
  `display_name` varchar(150) NOT NULL,
  `geo_coords_0` varchar(150) NOT NULL,
  `geo_coords_1` varchar(150) NOT NULL,
  `location_name` varchar(150) NOT NULL,
  `posted_day` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";

   $harry_query=mysqli_query($cxn,$harry);
}

Open in new window


I want to create a table based on the distinct "posted_day" values. I'm not getting an error, but I'm not getting my table.

What am I missing?
0
Comment
Question by:brucegust
  • 2
  • 2
4 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40346803
Wrap the recordset records in {}

$harry = "CREATE TABLE IF NOT EXISTS {$jorja_row['posted_day']} (

Open in new window


Else PHP will just see $jorja_row as the variable.
0
 

Author Comment

by:brucegust
ID: 40346828
Gary, here's my code:

$jorja = "select distinct posted_day from verizon where posted_day<>'0000-00-00' order by posted_day";
$jorja_query=mysqli_query($cxn, $jorja)
or die("Jorja didn't happen.");
while($jorja_row=mysqli_fetch_assoc($jorja_query))
{
extract($jorja_row);
  $harry = "CREATE TABLE IF NOT EXISTS '{$jorja_row['posted_day']}' (
    `id` int(11) NOT NULL AUTO_INCREMENT,
  `actor_id` varchar(150) NOT NULL,
  `actor_display_name` varchar(150) NOT NULL,
  `posted_time` varchar(150) NOT NULL,
  `display_name` varchar(150) NOT NULL,
  `geo_coords_0` varchar(150) NOT NULL,
  `geo_coords_1` varchar(150) NOT NULL,
  `location_name` varchar(150) NOT NULL,
  `posted_day` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
echo $harry;

Open in new window


I introduced the curly braces and ran it - no errors, but not table. I threw in the single quotes just to see if that would make a difference, again, no error but no table.

Here's what is "echo-ed..."

TABLE IF NOT EXISTS '2013-11-06' ( `id` int(11) NOT NULL AUTO_INCREMENT, `actor_id` varchar(150) NOT NULL, `actor_display_name` varchar(150) NOT NULL, `posted_time` varchar(150) NOT NULL, `display_name` varchar(150) NOT NULL, `geo_coords_0` varchar(150) NOT NULL, `geo_coords_1` varchar(150) NOT NULL, `location_name` varchar(150) NOT NULL, `posted_day` date NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

What do you think?
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40346860
Use backticks around the table name (like in the col names) and remove all the extraneous spaces in the sql, I ran the SQL with these changes and it was fine.

$harry = "CREATE TABLE IF NOT EXISTS `{$jorja_row['posted_day']}` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`actor_id` varchar(150) NOT NULL,
`actor_display_name` varchar(150) NOT NULL,
`posted_time` varchar(150) NOT NULL,
`display_name` varchar(150) NOT NULL,
`geo_coords_0` varchar(150) NOT NULL,
`geo_coords_1` varchar(150) NOT NULL,
`location_name` varchar(150) NOT NULL,
`posted_day` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";

Open in new window

0
 

Author Closing Comment

by:brucegust
ID: 40346899
Perfect!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Ajax and PHP 9 53
MSSQL - Lock Row from reading by other programs 9 57
PHP delete contents of file- before writing to it 6 49
Get data from two MySQL tables 6 18
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question