开发者

create mysql query from a colum in a second table

I have a piece of code that should get the names of 开发者_Go百科food items that are not ingredients from one table and then generate a query to create another table with columns matching the names of those items this is not generating a table:

function create_table(){
$query = "SELECT * FROM food";
$tablequery = "CREATE TABLE (id int(6) NOT NULL auto_increment,";
if($result=mysql_query($query)){
    while($row = mysql_fetch_array($result)){
        if($result['ingredient'] != 1){
            $tablequery = $tablequery.$result['name']."varchar(30) NOT NULL,";
        }
    }
    $tablequery = $tablequery."PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
}
mysql_query($tablequery);

}

I did call the function so that is not the problem and somehow it worked once but with just the id.


Try this:

while($row = mysql_fetch_assoc($result)){
    if($row['ingredient'] != 1){
        $tablequery = $tablequery.$row['name']."varchar(30) NOT NULL,";
    }
}


I think you need to add to your code a line to drop the table if it already exists:

 DROP TABLE IF EXISTS `<your table>`;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜