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)) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?