0

我有以下代码:

<?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 个州创建表,使用州名作为表名的通配符。但是,这些查询似乎没有执行。你能告诉我这个问题吗?

4

1 回答 1

1

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

或尝试:

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");
}
于 2013-04-23T00:02:48.933 回答