gpt4 book ai didi

php - 如何循环遍历 MySQL 行?

转载 作者:行者123 更新时间:2023-11-29 01:29:14 28 4
gpt4 key购买 nike

我正在尝试实现一个 web PHP 程序,它将以 HTML 形式显示 MySQL 数据库中记录的数据。我有几个导航按钮,我希望它们可以循环到数据库中的下一条和上一条记录。

我的问题是,我似乎无法在记录中来回循环。 mysql_fetch_assoc 只获取一行,或者只有在涉及 while 循环时才有用。

任何帮助将不胜感激!这是我目前所拥有的...

    $page = intval($_GET['page']);

$limitStart = $page - 1;
if($limitStart < 0) {
$limitStart = 0;
}

$query = "SELECT caleadID, region, siteaddr1, siteaddr2, siteaddr3, siteaddr4, sitepcode, ";
$query .= "addgennotes, description, value, award, awardaddr1, awardaddr2, awardaddr3, awardaddr4, awardpcode, ";
$query .= "phone_number, fax_number, fldAwardedWebsite, fldAwardedEmail, contact_name, date FROM tblTradesman_Awarded WHERE 1";
$query .= ' LIMIT ' . $limitStart . ',2';
$result = mysqli_query($connection, $query);
confirm_query($result);

$result_array = array();
while($row = mysqli_fetch_assoc($result)) {
$result_array[] = $row;
}
if(intval($page) === 0) {
$previousRecord = NULL;
$currentRecord = $result_array[0];
$nextRecord = $result_array[1];
} else {
$previousRecord = $result_array[0];
$currentRecord = $result_array[1];
$nextRecord = $result_array[2];
}
if($previousRecord) {

}

if($nextRecord) {

}
?>
<?PHP
$caleadID = $currentRecord['caleadID'];
$region = $currentRecord['region'];
$siteaddr1 = $currentRecord['siteaddr1'];
$siteaddr2 = $currentRecord['siteaddr2'];
$siteaddr3 = $currentRecord['siteaddr3'];
$siteaddr4 = $currentRecord['siteaddr4'];
$sitepcode = $currentRecord['sitepcode'];
$addgennotes = $currentRecord['addgennotes'];
$description = $currentRecord['description'];
$value = $currentRecord['value'];
$award = $currentRecord['award'];
$awardaddr1 = $currentRecord['awardaddr1'];
$awardaddr2 = $currentRecord['awardaddr2'];
$awardaddr3 = $currentRecord['awardaddr3'];
$awardaddr4 = $currentRecord['awardaddr4'];
$awardpcode = $currentRecord['awardpcode'];
$phone_number = $currentRecord['phone_number'];
$fax_number = $currentRecord['fax_number'];
$fldAwardedWebsite = $currentRecord['fldAwardedWebsite'];
$fldAwardedEmail = $currentRecord['fldAwardedEmail'];
$contact_name = $currentRecord['contact_name'];
$date = $currentRecord['date'];

?>

<div id="tradesmen_data">
<div id="primary">
<label for="caleadID">Tradesman Lead ID: </label>
<input type="text" name="caleadID" value="<?PHP echo $caleadID ?>" />&nbsp;&nbsp;&nbsp;&nbsp;
<label for="value">Job Value GBP: </label>
<input type="text" name="value" value="<?PHP echo $value ?>" />&nbsp;&nbsp;&nbsp;&nbsp;
<label for="date">Tradesman Publish Date: </label>
<input type="text" name="date" value="<?PHP echo $date ?>" />
</div>
<div class="columns">
<fieldset id="Site">
<legend>Site Details</legend>
<dl>
<dt><label for="siteaddr1">Site Address: </label></dt>
<dd><input type="text" name="siteaddr1" value="<?PHP echo $siteaddr1 ?>" /></dd>
</dl>
<dl>
<dt><label></label></dt>
<dd><input type="text" name="siteaddr2" value="<?PHP echo $siteaddr2 ?>" /></dd>
</dl>
<dl>
<dt><label></label></dt>
<dd><input type="text" name="siteaddr3" value="<?PHP echo $siteaddr3 ?>" /></dd>
</dl>
<dl>
<dt><label></label></dt>
<dd><input type="text" name="siteaddr4" value="<?PHP echo $siteaddr4 ?>" /></dd>
</dl>
<dl>
<dt><label for="sitepcode">Postcode: </label></dt>
<dd><input type="text" name="sitepcode" value="<?PHP echo $sitepcode ?>" /></dd>
</dl>
<dl>
<dt><label for="region">Region: </label></dt>
<dd><input type="text" name="region" value="<?PHP echo $region ?>" /></dd>
</dl>
<dl>
<dt><label for="addgennotes">General Notes: </label></dt>
<dd><textarea rows="4" cols="30" name="addgennotes"><?PHP echo $addgennotes ?></textarea></dd>
</dl>
<dl>
<dt><label for="description">Description: </label></dt>
<dd><textarea rows="4" cols="30" name="description"><?PHP echo $description ?></textarea></dd>
</dl>
</fieldset>
</div>
<div class="columns">
<fieldset id="Contractor">
<legend>Contractor Details</legend>
<dl>
<dt><label for="award">Awarded To: </label></dt>
<dd><input type="text" name="award" value="<?PHP echo $award ?>" /></dd>
</dl>
<dl>
<dt><label for="awardaddr1">Address: </label></dt>
<dd><input type="text" name="awardaddr1" value="<?PHP echo $awardaddr1 ?>" /></dd>
</dl>
<dl>
<dt><label></label></dt>
<dd><input type="text" name="awardaddr2" value="<?PHP echo $awardaddr2 ?>" /></dd>
</dl>
<dl>
<dt><label></label></dt>
<dd><input type="text" name="awardaddr3" value="<?PHP echo $awardaddr3 ?>" /></dd>
</dl>
<dl>
<dt><label></label></dt>
<dd><input type="text" name="awardaddr4" value="<?PHP echo $awardaddr4 ?>" /></dd>
</dl>
<dl>
<dt><label for="awardpcode">Postcode: </label></dt>
<dd><input type="text" name="awardpcode" value="<?PHP echo $awardpcode ?>" /></dd>
</dl>
<dl>
<dt><label for="phone_number">Telephone: </label></dt>
<dd><input type="text" name="phone_number" value="<?PHP echo $phone_number ?>" /></dd>
</dl>
<dl>
<dt><label for="fax_number">Fax: </label></dt>
<dd><input type="text" name="fax_number" value="<?PHP echo $fax_number ?>" /></dd>
</dl>
<dl>
<dt><label for="fldAwardedWebsite">Website: </label></dt>
<dd><input type="text" name="fldAwardedWebsite" value="<?PHP echo $fldAwardedWebsite ?>" /></dd>
</dl>
<dl>
<dt><label for="fldAwardedEmail">Email: </label></dt>
<dd><input type="text" name="fldAwardedEmail" value="<?PHP echo $fldAwardedEmail ?>" /></dd>
</dl>
<dl>
<dt><label for="contact_name">Contact Name: </label></dt>
<dd><input type="text" name="contact_name" value="<?PHP echo $contact_name ?>" /></dd>
</dl>
</fieldset>
<?PHP
$contact_made = true;
if ($contact_made) {
echo "<div id=\"contact_made\">";
echo "Contact has been made";
echo "</div>";
}
?>
</div>
<div class="clear_float"></div>
</div>
<div id="tradesmen_data_navigation">
<a href="#"><input type="submit" name="previous" value="Previous Record" /></a>
<a href="#"><input type="submit" name="view_lead" value="Generate Lead" /></a>
<a href="#"><input type="submit" name="next" value="Next Record" /></a>
</div>

最佳答案

遍历数组。

通过返回 mysqli_fetch_assoc,您可以有效地返回一行。

while($row = mysqli_fetch_assoc($result)) {
$returningRows[] = $row;
}

return $returningRows;

关于php - 如何循环遍历 MySQL 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22608132/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com