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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。