doucanshou6998 2014-10-31 08:24
浏览 39
已采纳

MySQL查询没有执行:“warning:mysql_fetch_assoc()期望参数1是资源”[重复]

I want to execute two queries at a time by using single script. My code is executing only the first query, and the second query shows an error.

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\hms\getpatient.php on line 29 Notice: Undefined variable: json_output in C:\xampp\htdocs\hms\getpatient.php on line 32

can any one please help why both the queries not executing but separately they will execute.

php

 <html>
    <head>
    <title>Paging Using PHP</title>
    </head>
    <body>

    <?php

        mysql_connect("localhost","root",""); 
        mysql_select_db("hms");
    ?> 
        <?php
        $q1=mysql_query("SELECT * FROM initial_master");



        while($row1=mysql_fetch_assoc($q1))
                $json_output1[]=$row1;

        print(json_encode($json_output1));

        mysql_close();
        ?>
       <?php


        $q=mysql_query("SELECT * FROM patient_main_category");

        while($row=mysql_fetch_assoc($q))
                $json_output[]=$row;

        print(json_encode($json_output));

        mysql_close();
            ?>

    </body>
    </html>
</div>
  • 写回答

1条回答 默认 最新

  • douzhanyan5015 2014-10-31 08:28
    关注

    Your second query is failing because you're calling mysql_close() after the first query. Simply removing it should fix your problem.

     <html>
        <head>
        <title>Paging Using PHP</title>
        </head>
        <body>
    
        <?php
    
            mysql_connect("localhost","root",""); 
            mysql_select_db("hms");
        ?> 
            <?php
            $q1=mysql_query("SELECT * FROM initial_master");
    
            $json_output1 = array(); // fixes Undefined variable
    
            while($row1=mysql_fetch_assoc($q1))
                    $json_output1[]=$row1;
    
            print(json_encode($json_output1));
    
            //mysql_close();
            ?>
           <?php
    
    
            $q=mysql_query("SELECT * FROM patient_main_category");
    
            while($row=mysql_fetch_assoc($q))
                    $json_output[]=$row;
    
            print(json_encode($json_output));
    
            mysql_close();
                ?>
    
        </body>
        </html>
    

    Fix Undefined variable notice by declaring$json_output1 before using it

    $json_output = array();
    

    You should also do some error checking to prevent such problems in the future

    $q = mysql_query('SELECT ...');
    if (!$q) {
       // handle error, for example:
       die('Database errror');
    }
    

    Lastly you should look into using a database library which is more secure, more robust and will catch such mistakes.

    <?php
    $db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
    
    try {
        //connect as appropriate as above
        $result = $db->query('SELECT * FROM ...');
    } catch(PDOException $ex) {
        echo "An Error occured!"; //user friendly message
        some_logging_function($ex->getMessage());
    }
    

    See this guide on how to use the PDO library instead of mysql_* functions. http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集