dongqiao1151 2013-12-14 21:09
浏览 53
已采纳

如何在数据库中插入列的新名称

There is a slight problem when I'm creating a new table in phpmyadmin with new field names. I'm using a for loop trying to insert data for the field names. But for some reason I'm getting a error

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[0] VARCHAR(30)' at line 3".

Here is my code:

include 'simple_html_dom.php';
include ('connection.php');


function getsquad($url, $tablename){

$html = file_get_html($url);

$player = array();

foreach($html->find('td[align=left]') as $element) {
   if ($element->children(0)) { // work only when children exists
          array_push($player ,$element->children(0)->innertext);
   }
}

$length = count($player);

for($i = 0;$i<=$length;$i++){
// Create a MySQL table in the selected database
mysql_query("CREATE TABLE $tablename(

player[$i] VARCHAR(30)")
or die(mysql_error());  
}

echo "Table Created!";

}

$Squad = new squad();
$Squad->getsquad('site', 'Players'); 

?>

I know the first part of my function is working so I don't think it is a PHP error but not 100% sure.

  • 写回答

2条回答 默认 最新

  • dongyan1936 2013-12-14 21:16
    关注

    You forgot the $ before player. The right way is:

    mysql_query("CREATE TABLE $tablename($player[$i] VARCHAR(30)") or die(mysql_error());
    

    update: And as user876345 said, you forget to close the parentheses. So, the code should be:

    mysql_query(" CREATE TABLE $tablename( $player[$i] VARCHAR(30) ) ") or die(mysql_error());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站