语句导致while循环错误

I have some PHP/SQL written to populate a selection list with data from a database with the option being populated based on a prior entry in a text box.$pk is accepted perfectly and is a valid ARTICLE_NO, and the query works perfectly when executed directly by mysql. I have put output statements after every event and all except tetsing while executes. The while loop is never entered, and I am unsure why. Here is my code :

edit: I have narrowed the problem down to the fact that 0 rows are returned, but I have no idea why as the same query in phpmyadmin gives the right result.

    if (!$getRecords->fetch()) {
    printf("<p>ErrorNumber: %d
", $getRecords->errno);
}

It shows that the errno is 0. So no records are fetched, and there is no error, yet it is a valid query.

<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
$pk = $_GET["pk"];
$con = mysqli_connect("localhost", "", "", "");
if (!$con) {
    echo "Can't connect to MySQL Server. Errorcode: %s
". mysqli_connect_error();
    exit;
}
$con->set_charset("utf8");
echo "test outside loop";
if(1 < 2) {
    echo "test inside loop";
    $query1 = 'SELECT ARTICLE_NO FROM AUCTIONS WHERE ARTICLE_NO = ?';
    if ($getRecords = $con->prepare($query1)) {
echo "inside second loop";
        $getRecords->bind_param("i", $pk);
echo "test after bind param";
        $getRecords->execute();
echo "test after bind execute";
        $getRecords->bind_result($ARTICLE_NO);
echo "test after bind result";
        while ($getRecords->fetch()) {
            echo "test inside while";
            echo "<h1>".$ARTICLE_NO."</h1>";
        }
    }
}

edit:

I tried with this code:

<?php
$mysqli = new mysqli("localhost", "", "", "");
$pk = $_GET["pk"];
if (mysqli_connect_errno()) {
    printf("Connect failed: %s
", mysqli_connect_error());
    exit();
}
/* prepare statement */
if ($stmt = $mysqli->prepare("SELECT ARTICLE_NAME, WATCH FROM AUCTIONS WHERE ARTICLE_NO = ? LIMIT 5")) {
    $stmt->bind_param("i", $pk);
    $stmt->execute();
    /* bind variables to prepared statement */
    $stmt->bind_result($col1, $col2);
    /* fetch values */
    while ($stmt->fetch()) {
        printf("%s %s
", $col1, $col2);
    }
    /* close statement */
    $stmt->close();
}
/* close connection */
$mysqli->close();
?>

This works without $pk, if I take away the parameters it works fine. It is not a problem with getting pk via GET, because if I assign $pk = 1; instead it still fails.

edit: the problem was that mysqli could not handle bigint, I am now using k as a string and it works fine.

First thing I noticed is the name of the database. It should be a string 'twa312', I also put a check for the $_POST['post'] check the code below:

    <?php

    $conn = mysql_connect("localhost", "*****", "*****");
    mysql_select_db('twa312', $conn)
    or die ('Database not found ' . mysql_error() );

    $options = '<option value="0"></option>';
    if(isset($_POST['post']))
    {
         $postcode = $_POST["post"];


    $sql = "SELECT school_info.Name AS Name, local_schools.postcodeID AS postcode FROM school_info INNER JOIN local_schools ON local_schools.schoolID = school_info.schoolID";
    $sql = $sql . " where postcode = '$postcode' ";

    $rs = mysql_query($sql, $conn)
    or die ('Problem with query' . mysql_error());

    while ($row=mysql_fetch_array($rs)) {
    $name=$row["Name"];
    $options .= '<OPTION VALUE="' . $name . '">' . $name ."</option>";
    }
}
else
{
$options = '';
}

    ?>

You also didn't defined $msgp variable.

Hope it helps you, feel free to ask me any other question, and post also your error.

Something like this should work. You weren't including the valid_postcodes table in your query and so the server was getting confused with a non-existent field.

if (isset($postcode) && !empty($postcode)) {
$sql = "SELECT school_info.Name AS Name, local_schools.postcodeID AS postcode FROM school_info INNER JOIN local_schools ON local_schools.schoolID = school_info.schoolID";
$sql .= " JOIN valid_postcodes ON local_schools.postcodeID = valid_postcodes.id ";
$sql .= " where postcode = '$postcode' ";

$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());

while ($row=mysql_fetch_array($rs)) {
    $name=$row["Name"];
    $options .= '<OPTION VALUE="' . $name . '">' . $name ."</option>";
}
}

Also, you can reduce your chances of an error by making sure the query only runs when it knows there's a value in the variable. And reduce the number of times you have to write out your variable name by using PHP's .= concatenation.

Inspect the returned array like so:

$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());

echo "<pre>";
print_r(mysql_fetch_assoc($rs));
echo "</pre>";

while ($row=mysql_fetch_array($rs)) {