Solved

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

Posted on 2014-09-26
4
95 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

729 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