gpt4 book ai didi

php - 如何在PHP中动态创建具有多个字段的表

转载 作者:行者123 更新时间:2023-11-29 09:08:17 26 4
gpt4 key购买 nike

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}

echo 'Connected successfully<br />';



$name_of_field="id_student";


function field_dynamic($field_num,$field_name){

for($i=1;$i<=$field_num;$i++){

if($i!=$field_num){

$field[$i]="".$field_name."_".$i." VARCHAR(5) NOT NULL,"."";

}else{

$field[$i]="".$field_name."_".$i." VARCHAR(5) NOT NULL";

}

}
return $field;
}

$num=13;

$sql_mathematic = "CREATE TABLE mathematic(".

field_dynamic($num,$name_of_field).")";

mysql_select_db("adrian");
$retval_mathematic = mysql_query( $sql_mathematic, $conn );

if(! $retval_mathematic )
{
die('Could not create table: ' . mysql_error());
}

echo "Table created successfully\n";

它不工作:

Connected successfully
Could not create table: 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 ')' at line 1

请帮助我。

最佳答案

这可以实现您正在谈论的内容,但您不应该使用它。

for($i=1;$i<=$field_num;$i++){
$field[$i]="".$field_name."_".$i." VARCHAR(5) NOT NULL";

}
return implode(',',$field);

关于php - 如何在PHP中动态创建具有多个字段的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6429697/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com