duanni5726 2015-10-03 03:15
浏览 18
已采纳

我在php中编写一个madlib,使用mysql将其保存到db,然后尝试按降序排出每个新故事

I am not sure how to get the story out of the db, I am getting this error:

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);

?>
  • 写回答

1条回答 默认 最新

  • doushifang4382 2015-10-03 03:22
    关注

    You are not executing the query before trying to fetch. The query is just a string in $query2.

    You need to execute the query by calling mysqli->query

    Since you already did this above it looks like you just missed it all together on the second query by accident. ( I've done this before).

    After this line

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

    You can do something like

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

    and change this accordingly

    while($row = mysqli_fetch_array($result2)) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法