Solved

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

Posted on 2014-09-26
4
88 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now