0

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>
i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
user
  • 13
  • 7
  • declare `$json_output1 = array();` before you use it to remove the notice you're getting. – i alarmed alien Oct 31 '14 at 08:27
  • 2
    You have a `mysql_close();` before second query. – fdehanne Oct 31 '14 at 08:27
  • `$q1=mysql_query("SELECT * FROM initial_master")or die(mysql_error());` change your second query like this as well. means add `mysql_error()` in that one as well – arif_suhail_123 Oct 31 '14 at 08:28
  • 1
    You [shouldn't use mysql_* functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use PDO or MySQLi. – Daniel Gelling Oct 31 '14 at 08:29

1 Answers1

0

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

mak
  • 13,267
  • 5
  • 41
  • 47