gpt4 book ai didi

PHP - MySQL 查询拒绝执行

转载 作者:行者123 更新时间:2023-11-29 13:54:06 25 4
gpt4 key购买 nike

我有以下代码:

<?php

$usStates = array(
'Alabama',
'Alaska',
'Arizona',
'Arkansas',
'California',
'Colorado',
'Connecticut',
'Delaware',
'Florida',
'Georgia',
'Hawaii',
'Idaho',
'Illinois',
'Indiana',
'Iowa',
'Kansas',
'Kentucky',
'Louisiana',
'Maine',
'Maryland',
'Massachusetts',
'Michigan',
'Minnesota',
'Mississippi',
'Missouri',
'Montana',
'Nebraska',
'Nevada',
'New Hampshire',
'New Jersey',
'New Mexico',
'New York',
'North Carolina',
'North Dakota',
'Ohio',
'Oklahoma',
'Oregon',
'Pennsylvania',
'Rhode Island',
'South Carolina',
'South Dakota',
'Tennessee',
'Texas',
'Utah',
'Vermont',
'Virginia',
'Washington',
'West Virginia',
'Wisconsin',
'Wyoming',
);

$link = new mysqli("localhost", "drup197", "*******", "census");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}


foreach ($usStates as $state) {
printf("Entering loop\n");
printf("Current state = %s\n", $state);

if (mysqli_query($link, "CREATE TABLE '$state'_age_and_gender_demographics LIKE national_age_and_gender_demographics")) {
printf("%s_age_and_gender_demographics successfully created\n", $state);
}
if (mysqli_query($link, "CREATE TABLE '$state'_race_demographics_-_one_race LIKE national_race_demographics_-_one_race")) {
printf("%s_race_demographics_-_one_race successfully craeted\n");
}
if (mysqli_query($link, "CREATE TABLE '$state'_race_demographics_-_multi-race LIKE national_race_demographics_-_multi-race")) {
printf("%s_race_demographics_-_multi-race successfully craeted\n");
}
if (mysqli_query($link, "CREATE TABLE '$state'_race_demographics_-_hispanic_or_latino LIKE national_race_demographics_-_hispanic_or_latino")) {
printf("%s_race_demographics_-_hispanic_or_latino successfully craeted\n");
}
}
printf("Tables successfuly created");

mysqli_close($link);
?>

基本上,我尝试为所有 50 个州创建表,使用州名称作为表名称的通配符。但是,这些查询似乎并未执行。您能告诉我问题所在吗?

最佳答案

从查询字符串中删除单引号。您正在尝试创建名为'California'_age_and_gender_demgraphics的表。

或者尝试:

if (mysqli_query($link, "CREATE TABLE ". $state ."_age_and_gender_demographics LIKE national_age_and_gender_demographics")) {
printf("%s_age_and_gender_demographics successfully created\n", $state);
}
if (mysqli_query($link, "CREATE TABLE ". $state ."_race_demographics_-_one_race LIKE national_race_demographics_-_one_race")) {
printf("%s_race_demographics_-_one_race successfully craeted\n");
}
if (mysqli_query($link, "CREATE TABLE ". $state ."_race_demographics_-_multi-race LIKE national_race_demographics_-_multi-race")) {
printf("%s_race_demographics_-_multi-race successfully craeted\n");
}
if (mysqli_query($link, "CREATE TABLE ". $state ."_race_demographics_-_hispanic_or_latino LIKE national_race_demographics_-_hispanic_or_latino")) {
printf("%s_race_demographics_-_hispanic_or_latino successfully craeted\n");
}

关于PHP - MySQL 查询拒绝执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16158724/

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