我编写了一些PHP / SQL来填充选择列表,其中包含来自数据库的数据,其中选项是根据文本框中的先前条目填充的。$ pk被完全接受并且是有效的ARTICLE_NO,并且查询有效完全由mysql直接执行。我在每个事件之后都放置了输出语句,并且在执行时除了tetsing之外的所有事永远不会输入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作为字符串,它工作正常。
答案 0 :(得分: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变量。
希望它对您有所帮助,随时向我提出任何其他问题,并发布您的错误。
答案 1 :(得分: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)) {