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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料