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

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

问题描述:

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.

当我在phpmyadmin中使用新的字段名创建新表时,会出现一些小问题。 我正在使用for循环尝试为字段名称插入数据。 但由于某种原因,我收到错误 p>

“您的SQL语法有错误;请查看 对应于您的MySQL服务器版本的手册 正确的语法在第3行使用 ''[0] VARCHAR(30)'附近“。 p> blockquote>

这是我的代码: p> \ n

 包含'simple_html_dom.php'; 
include('connection.php'); 
 
 
函数getsquad($ url,$ tablename){
 
 $ html = file_get_html($ url)  ); 
 
 $ player = array(); 
 
foreach($ html-&gt; find('td [align = left]')as $ element){
 if($ element-&gt; children(  0)){//仅在儿童存在时工作
 array_push($ player,$ element-&gt; children(0) - &gt; innertext); 
} 
} 
 
 $ length = count($ player  ); 
 
for($ i = 0; $ i&lt; = $ length; $ i ++){
 //在所选数据库中创建一个MySQL表
mysql_query(“CREATE TABLE $ tablename(
 
 
nplayer [$  i] VARCHAR(30)“)
ot die(mysql_error()); 
} 
 
echo”Table Created!“; 
 
} 
 
 $ Squad = new squad(); 
 $  Squad-&gt; getsquad('site','Players'); 
 
?&gt; 
  code>  pre> 
 
  

我知道我的函数的第一部分正在工作,所以我认为它不是PHP错误,但不是100%确定。 p> div>

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());

You have missed to close the ) at the end of the query and also missed $ in player variable

CREATE TABLE $tablename( $player[$i] VARCHAR(30) )

Your code should be like:

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