2

I'm using a array to get a range between 1011-2371 but I'didnt need all digits. It's a long list thats why I import the CSV to the database.

I want to create a array from a database table. But I can't get it.

Here's a rough mockup of the old 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';
    }
}
?>

and this is what I try do get the array from the database:

<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';
    }
}
?>

the db_config.php file looks like:

<?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'); 
    } 
} 
?>
4
  • You need to fetch the result then store it in $postcode. php.net/manual/en/function.mysql-fetch-array.php Commented Apr 24, 2015 at 12:43
  • So you have a query ($result = ...) but you don't fetch/execute it. Moreover, you don't use the results in your code. Commented Apr 24, 2015 at 12:43
  • @chris85 I tried but don't get the right message :( Commented Apr 24, 2015 at 12:59
  • Look at my answer and message.. Commented Apr 24, 2015 at 13:00

2 Answers 2

1

you have to use mysql_fetch_array or mysql_fetch_assoc to fetch the result from database.

<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';
}
}
?>

Try this code.

Sign up to request clarification or add additional context in comments.

1 Comment

change the $postcode[] = $row; to ` $postcode[] = $row['postcode'];`
1

You need to fetch the result. Your regex will never match your sample data from the db though so you will always get the nog niet message. or voorbeeld. Never 'FreshFoods is 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';
    }
}
?>

2 Comments

thanks @chris85, this is what I want. I wil focus now on the fetch process
Any particular reason this was reverted from being the answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.