从数据库创建数组

时间:2015-04-24 12:40:55

标签: php mysql arrays

我正在使用数组来获得1011-2371之间的范围,但我不需要所有数字。这是一个很长的列表,这就是我将CSV导入数据库的原因。

我想从数据库表创建一个数组。 但我无法得到它。

这是旧php的粗略模型:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
    <input type="text" placeholder="1234AB" name="postcode" />
    <input type="submit" value="verstuur" />
</form>
<?php

if($_SERVER['REQUEST_METHOD'] == "POST") {
    $postcode = range(1011,2371);
    if(preg_match('/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/', $_POST['postcode'])) {
        if(in_array($_POST['postcode'],$postcode)) {
            echo 'FreshFoods is beschikbaar bij jou in de buurt.';
        } else {
            echo  'FreshFoods is nog niet beschikbaar bij u in de buurt.';
        }
    } else {
        echo 'Voer 4 cijfers en 2 letters in als postcode. Voorbeeld 1234AB';
    }
}
?>

这就是我尝试从数据库中获取数组:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
    <input type="text" placeholder="1234AB" name="postcode" />
    <input type="submit" value="verstuur" />
</form>
<?php
require_once 'db_config.php'; 

if($_SERVER['REQUEST_METHOD'] == "POST") {
    $postcode = array();
    $result = mysql_query("SELECT postcode FROM postcode_check");

    if(preg_match('/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/', $_POST['postcode'])) {
        if(in_array($_POST['postcode'],$postcode)) {
            echo 'FreshFoods is beschikbaar bij jou in de buurt.';
        } else {
            echo  'FreshFoods is nog niet beschikbaar bij u in de buurt.';
        }
    } else {
        echo 'Voer 4 cijfers en 2 letters in als postcode. Voorbeeld 1234AB';
    }
}
?>

db_config.php文件如下所示:

<?php 
$db = array ( 
    'host' => 'localhost', 
    'user' => 'root', 
    'pass' => 'root', 
    'dbname' => 'testzip' 
); 

if(!mysql_connect($db['host'], $db['user'], $db['pass'])) 
{ 
    trigger_error('Fout bij verbinden: '.mysql_error()); 
} 
elseif(!mysql_select_db($db['dbname'])) 
{ 
    trigger_error('Fout bij selecteren database: '.mysql_error()); 
} 
else 
{ 
    $sql = "SET SESSION sql_mode = 'ANSI,ONLY_FULL_GROUP_BY'"; 
    if(!mysql_query($sql)) 
    { 
        trigger_error('MySQL in ANSI niet mogelijk'); 
    } 
} 
?>

2 个答案:

答案 0 :(得分:1)

您必须使用mysql_fetch_arraymysql_fetch_assoc从数据库中获取结果。

<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" placeholder="1234AB" name="postcode" />
<input type="submit" value="verstuur" />
</form>
<?php
require_once 'db_config.php'; 

if($_SERVER['REQUEST_METHOD'] == "POST") {
$postcode = array();
$result = mysql_query("SELECT postcode FROM postcode_check");
while ($row = mysql_fetch_array($result)) {
    $postcode[] = $row['postcode'];
}
if(preg_match('/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/', $_POST['postcode'])) {
    if(in_array($_POST['postcode'],$postcode)) {
        echo 'FreshFoods is beschikbaar bij jou in de buurt.';
    } else {
        echo  'FreshFoods is nog niet beschikbaar bij u in de buurt.';
    }
} else {
    echo 'Voer 4 cijfers en 2 letters in als postcode. Voorbeeld 1234AB';
}
}
?>

试试这段代码。

答案 1 :(得分:1)

您需要获取结果。您的正则表达式永远不会匹配数据库中的示例数据,因此您将始终获得nog niet消息。或者voorbeeld。从来没有'FreshFoods是beschikbaar bij jou in de buurt'

<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
    <input type="text" placeholder="1234AB" name="postcode" />
    <input type="submit" value="verstuur" />
</form>
<?php
require_once 'db_config.php';
if($_SERVER['REQUEST_METHOD'] == "POST") {
    $postcode = array();
    $result = mysql_query("SELECT postcode FROM postcode_check");
    while($row = mysql_fetch_assoc($result)){
        $postcode[] = $row['postcode'];
    }
    if(preg_match('/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/', $_POST['postcode'])) {
        if(in_array($_POST['postcode'],$postcode)) {
            echo 'FreshFoods is beschikbaar bij jou in de buurt.';
        } else {
            echo  'FreshFoods is nog niet beschikbaar bij u in de buurt.';
        }
    } else {
        echo 'Voer 4 cijfers en 2 letters in als postcode. Voorbeeld 1234AB';
    }
}
?>