Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Unknown amount of columns in select statement
Old 11-22-2007, 10:35 PM Unknown amount of columns in select statement
Junior Talker

Posts: 3
Trades: 0
I am trying to execute some queries in php, but I don't know how many columns there are, or what their names are. Basically, I want to recreate a SQL table in php, dynamically/generically. Any ideas? Thanks
ohdigeydoc is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-23-2007, 02:29 AM Re: Unknown amount of columns in select statement
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
For the number of fields: mysql_num_fields();
http://www.php.net/manual/en/functio...num-fields.php

And to get the name of each of them:
PHP Code:
$sql="Select * from a";
$intField=0;  //Used to get the field type later
$sqlIns="create table a(";
$r=mysql_query($sql);
while(
$ary=mysql_fetch_array($rMYSQL_ASSOC)){
  foreach( 
$ary as $col => $val){
      
//This foreach will scan each column sequentially
      //by their names. Do whatever you want here.
      
$type=mysql_field_type($r,$intField);
      
$len=mysql_field_len($r,$intField);
      
$sqlIns.="$col $type($len),\r\t";
      
$intField++;
  }
}
$sqlIns.=");";
//And here you have your create table query...
echo $sqlIns
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 11-23-2007 at 02:30 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-23-2007, 10:13 AM Re: Unknown amount of columns in select statement
Junior Talker

Posts: 3
Trades: 0
Thanks for your help. Though, I get this error:

Warning: mysql_field_type(): Field 3 is invalid for MySQL result index 3

The line it references is: $type=mysql_field_type($r,$intField);
and then: $len=mysql_field_len($r,$intField);


Any ideas? THanks again.
ohdigeydoc is offline
Reply With Quote
View Public Profile
 
Old 11-23-2007, 10:58 AM Re: Unknown amount of columns in select statement
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Hmm, yeah. I believe I have misplaced the counter declaration.
Try to move it into the while loop, like this:
PHP Code:
$sql="Select * from a";
$sqlIns="create table a(";
$r=mysql_query($sql);
while(
$ary=mysql_fetch_array($rMYSQL_ASSOC)){
  
$intField=0;  //Used to get the field type later
  
foreach( $ary as $col => $val){
    
//This foreach will scan each column sequentially
    //by their names. Do whatever you want here.
    
$type=mysql_field_type($r,$intField);
    
$len=mysql_field_len($r,$intField);
    
$sqlIns.="$col $type($len),\r\t";
    
$intField++;
  }
}
$sqlIns.=");";
//And here you have your create table query...
echo $sqlIns

</span></span>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Unknown amount of columns in select statement
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30736 seconds with 12 queries