0

我编写了一些 PHP/SQL 来使用数据库中的数据填充选择列表,并根据文本框中的先前条目填充选项。$ pk 被完全接受并且是有效的 ARTICLE_NO,并且查询在执行时完美运行直接用mysql。我在每个事件之后都放置了输出语句,除了测试时执行之外的所有语句。永远不会进入 while 循环,我不确定为什么。这是我的代码:

编辑:我已将问题缩小到返回 0 行的事实,但我不知道为什么 phpmyadmin 中的相同查询会给出正确的结果。

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

说明 errno 为 0。因此没有获取记录,也没有错误,但它是一个有效的查询。

<?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\n". 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>";
        }
    }
}

编辑:

我试过这段代码:

<?php
$mysqli = new mysqli("localhost", "", "", "");
$pk = $_GET["pk"];
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", 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\n", $col1, $col2);
    }
    /* close statement */
    $stmt->close();
}
/* close connection */
$mysqli->close();
?>

这在没有 $pk 的情况下工作,如果我去掉参数它工作正常。通过 GET 获取 pk 不是问题,因为如果我指定 $pk = 1; 相反,它仍然失败。

编辑:问题是 mysqli 无法处理 bigint,我现在使用 k 作为字符串,它工作正常。

4

2 回答 2

0

我注意到的第一件事是数据库的名称。它应该是一个字符串'twa312',我还检查了 $_POST['post'] 检查下面的代码:

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

    ?>

您也没有定义 $msgp 变量。

希望它对您有所帮助,请随时问我任何其他问题,并发布您的错误。

于 2013-05-31T17:33:08.170 回答
0

像这样的东西应该工作。您没有valid_postcodes在查询中包含该表,因此服务器对不存在的字段感到困惑。

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>";
}
}

此外,您可以通过确保查询仅在知道变量中有值时才运行来减少出错的机会。.=并通过使用 PHP 的连接来减少您必须写出变量名的次数。

像这样检查返回的数组:

$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)) {
于 2013-05-31T17:35:35.180 回答