我有一个PHP脚本,可以成功连接到MySQL数据库并包含两个mysqli查询。第一个查询按预期运行。第二个查询只会回显: 33111111 我想我需要free()
一些东西,但我不确定是什么,我试图释放第一个结果。
我确信这对我来说是一个简单的错误。感谢您的帮助!
pffages:
aid | aTitle | aActive | aVersion
---------------------------------
3 | 16-17 | 1 | 1
4 | 14-15 | 1 | 1
5 | 15-16 | 1 | 1
pffchecklist(适用于php):
lid | lTitle | lText | aid | cid
--------------------------------
1 | Check1 | hi | 3 | 1
2 | Check2 | world | 4 | 1
ulist.php:
<?php
include('connections/conn.php');
$getList = $_GET['lid'];
$testsql = 'SELECT * FROM pffchecklist where lid=' . $getList . ';';
$testresult = mysqli_query($conn, $testsql);
$getrow = mysqli_fetch_array($testresult);
echo $getrow["lTitle"]; //works
echo $getrow['lText']; // works
$getAgesSql = 'SELECT * FROM pffages where aActive = 1;';
$getAgesResult = mysqli_query($conn,$getAgesSql);
$getAges = mysqli_fetch_array($getAgesResult);
foreach($getAges as $theAges){
echo $theAges['aTitle']; // returns 33111111
echo $theAges['aid']; // returns 33111111
}
?>
答案 0 :(得分:1)
$getages
是myqli_fetch_array返回的数组。这将是一维数组,具有标量值。要查看其中包含的内容(用于调试),我们可以使用var_dump
$getAges = mysqli_fetch_array($getAgesResult);
-- for debugging, show what is returned
var_dump($getAges);
-- show the values returned for the 'aTitle' and 'aid' columns
echo $getAges['aTitle'];
echo $getAges['aid'];
我怀疑这就是我们追求的目标。
可以使用foreach
循环来访问数组的内容。例如:
-- this displays the values in the array but not the key
foreach($getAges as $theAges) {
echo $theAges;
}
使用默认的MYSQLI_BOTH,我们将获得每个值的两个副本。一组带有数字索引,另一组带有列名
-- this will show the key along with the value
foreach($getAges as $key => $val) {
echo "{$key} => {$val} ";
}
<强>后续强>
“所以,我需要它来返回aid和aTitle的值 - 4条记录。如何解决这个问题呢?”
我会这样做的。规范是针对“四条记录”的,我们将其视为“四行”...如果这是我们想要显示的行数的上限,我们可以在查询中添加LIMIT 4
子句以避免返回超过四行。我们还可以添加一个ORDER BY子句,以便查询更具确定性,而不是MySQL以任何顺序返回行。
$sql = 'SELECT p.aid, p.aTitle FROM pffages p WHERE p.aActive = 1 ORDER BY p.aid LIMIT 4';
我们将坚持程序风格,因为这是原始代码中使用的内容。我就是这样做的:
if(!$sth = mysqli_query($conn,$sql)) {
// error occurred
die(mysqli_error($conn));
}
$i = 0;
while( $row = mysqli_fetch_array($sth, MYSQLI_ASSOC) ) {
$i++;
echo "row=" . $i . " aid=" . $row['aid'] . " aTitle=" . $row['aTitle'] . "\n";
}
($i
的用法就像演示一样:计算获取的行数。循环次数。$i
的引用可以删除,实际上不需要它们。)
if(!$sth = mysqli_query($conn,$sql)) {
die(mysqli_error($conn));
}
while( $row = mysqli_fetch_array($sth, MYSQLI_ASSOC) ) {
echo "aid=" . $row['aid'] . " aTitle=" . $row['aTitle'] . "\n";
}
答案 1 :(得分:0)
<?php
include('connections/conn.php');
$getList = $_GET['lid'];
$testsql = 'SELECT * FROM pffchecklist where lid=' . $getList . ';';
$testresult = mysqli_query($conn, $testsql);
$getrow = mysqli_fetch_array($testresult, MYSQLI_BOTH);
echo $getrow["lTitle"]; //works
echo $getrow['lText']; // works
$getAgesSql = 'SELECT * FROM pffages where aActive = 1;';
$getAgesResult = mysqli_query($conn,$getAgesSql);
while( $getAge = mysqli_fetch_array($getAgesResult, MYSQLI_BOTH) ){
$getAges[] = $getAge;
}
foreach($getAges as $theAges){
echo $theAges['aTitle']; // returns 33111111
echo $theAges['aid']; // returns 33111111
}
?>
答案 2 :(得分:0)
$testsql = 'SELECT * FROM pffages order by aTitle;';
$testresult = mysqli_query($conn, $testsql);
while($testrow = mysqli_fetch_array($testresult)){
echo $testrow['aTitle'];
}