gpt4 book ai didi

php mysqli异常,插入和读取数组

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

所以我正在测试我的代码,本质上我向数据库插入一些信息,然后按时间顺序在数组中读取它,因此首先查看插入的最新内容。

问题:插入新数据但由于某种原因出现在网站的第4位,找不到异常。

这是插入查询:

<?php
//values to be inserted in database table
//session_start();
include('connect.php');


$title = $_POST['title'];
$tip= $_POST['tip'];

$query = "INSERT INTO tips (title, paragraph) VALUES(?, ?)";
$statement = $mysqli->prepare($query);

//bind parameters for markers, where (s = string, i = integer, d = double, b = blob)
$statement->bind_param('ss', $title, $tip);

if($statement->execute()){
print 'Success! ID of last inserted record is : ' .$statement->insert_id .'<br />';
echo'<a href="membership.php" class="blar">Membership page</a>';
}else{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$statement->close();
?>

这是读取数据的代码:

$category = array(
"dates" => array(),
"paragraphs" => array(),
"titles" => array()
);

if ($result = $mysqli->query("SELECT * FROM tips")){
if($result->num_rows){
while($row = $result->fetch_array(MYSQLI_ASSOC)){
//printf ("%s %s %s\n",$row["date"], $row["title"], $row["paragraph"]);
$date[] = $row['date'];
$paragraphs[] = $row['paragraph'];
$titles[] = $row['title'];
}
}
$result->free();
}
?>

然后按顺序阅读这种东西:

<div class="cd-timeline-block">
<div class="cd-timeline-img cd-picture">
<img src="img/cd-icon-location.svg" alt="Location">
</div> <!-- cd-timeline-img -->

<div class="cd-timeline-content">
<h2><?php echo $titles[count($titles)-1]?> </h2>
<p><?php echo $paragraphs[count($paragraphs)-1]?></p>

<span class="cd-date"><?php echo $date[count($date)-1]?></span>
</div> <!-- cd-timeline-content -->
</div> <!-- cd-timeline-block -->

<div class="cd-timeline-block">
<div class="cd-timeline-img cd-movie">
<img src="img/cd-icon-location.svg" alt="Location">
</div> <!-- cd-timeline-img -->

<div class="cd-timeline-content">
<h2><?php echo $titles[count($titles)-2]?></h2>
<p><?php echo $paragraphs[count($titles)-2]?></p>

<span class="cd-date"><?php echo $date[count($titles)-2]?></span>
</div> <!-- cd-timeline-content -->
</div> <!-- cd-timeline-block -->

这是前两个数组,不明白异常在哪里,有指针吗?

最佳答案

更改您的查询以添加订单

SELECT * FROM tips order by date

关于php mysqli异常,插入和读取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33792378/

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