gpt4 book ai didi

php - 我正在 php 中编写 madlib,使用 mysql 将其保存到数据库,然后尝试按降序提取每个新故事

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

我不确定如何从数据库中获取故事,我收到此错误:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in /home/ubuntu/workspace/projects/project_1/madlib.php on line 33 Call Stack: 0.0009 248160 1. {main}() /home/ubuntu/workspace/projects/project_1/madlib.php:0 0.0058 257240 2. mysqli_fetch_array() /home/ubuntu/workspace/projects/project_1/madlib.php:33

<?php
//connecting to the db
$dbc = mysqli_connect('localhost', 'root', '', 'project1')
or die('Error connecting to MySQL server.');

//creating variables
$noun = $_POST['noun'];
$verb = $_POST['verb'];
$adjective = $_POST['adjective'];
$body_part = $_POST['bodypart'];
$food_item = $_POST['fooditem'];
$full_story = "Here are some things to do at recess."
"Start a game of touch $body_part-ball."
"Put a $noun in someones lunch."
"Start a $food_item fight in the school $adjective room."
"Choose sides and have a $verb ball tournament.";

//inserting form info into db
$query = "INSERT INTO my_game (noun, verb, adjective, body_part, food_item) " .
"VALUES ('$noun', '$verb', '$adjective', '$body_part', '$food_item')";

//get query result from db
$result = mysqli_query($dbc, $query)
or die('Error querying database.');

if(empty($noun) || empty($verb) || empty($adjective) || empty($body_part) || empty($food_item)) {
echo "You have not filled in all of the fields.Please go back and try again.";
}

$query2 = "SELECT * FROM my_game WHERE id ORDER BY desc";

//while loop grabs name from array
while($row = mysqli_fetch_array($query2)) {
$noun = $row['noun'];
$verb = $row['verb'];
$adjective = $row['adjective'];
$body_part = $row['body_part'];
$food_item = $row['food_item'];
$full_story = "Here are some things to do at recess."
"Start a game of touch $body_part-ball."
"Put a $noun in someones lunch."
"Start a $food_item fight in the school $adjective room."
"Choose sides and have a $verb ball tournament.";

//outputs story
echo $full_story;

}

//close connection
mysqli_close($dbc);

?>

最佳答案

在尝试获取之前您没有执行查询。查询只是 $query2 中的一个字符串。

您需要通过调用 mysqli->query 来执行查询

由于您已经在上面执行了此操作,看起来您只是在第二个查询中意外地错过了所有这些内容。 (我以前做过这个)。

此行之后

$query2 = "SELECT * FROM my_game WHERE id ORDER BY desc";

你可以做类似的事情

 //get query result from db  
$result2 = mysqli_query($dbc, $query2)
or die('Error querying database.');

并相应地更改它

while($row = mysqli_fetch_array($result2)) {

关于php - 我正在 php 中编写 madlib,使用 mysql 将其保存到数据库,然后尝试按降序提取每个新故事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32918531/

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