php - 如何为数据库中的列插入新名称

标签 php mysql sql

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

"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".

这是我的代码:

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

?>

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

最佳答案

您忘记了玩家前面的$。正确的做法是:

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

更新: 正如 user876345 所说,你忘记关闭括号。所以,代码应该是:

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

关于php - 如何为数据库中的列插入新名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20588300/

相关文章:

php - Wordpress - 获取帖子的链接

php - Gmail REST API - 如何在 PHP 中获取 "Create the email content in some convenient way"

php - Openssl RSA加密-解密

php - 如何为以下内容指定 mysql 级联规则

java - 将Java连接到MySQL数据库

php - 1452 无法添加或更新子行 : a foreign key constraint fails

mysql - 计算出现在时间间隔 2 中的时间间隔 1 中的条目数 - SQL

sql - 为什么这个更新查询不起作用?

sql - 缩短查询

php - 循环sql SELECT 查询