• PHP Help PHP Newbies
  • [RESOLVED] Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given(not connectin

I have been for about 4 hours trying to get this to work.
Here's the code:

<?php
if ($id_files) {
$db = mysqli_connect("localhost", "root", "");
mysqli_select_db($db, "cagrape");
$sql = "SELECT id_files, name, tel, email, address1, address2, city, state, zip, order_status, order_request, intake_stamp, taken_by FROM holidayorders WHERE id_files=$id_files";
//$result = mysql_query($sql, $db);
$result = mysqli_query($db, $sql) or die(mysqli_error($db));

while ($info = mysqli_fetch_array($result)) {
	$name = stripslashes($info['name']);
	$tel = stripslashes($info['tel']);
	$email = stripslashes($info['email']);
	$address1 = stripslashes($info['address1']);
	$address2 = stripslashes($info['address2']);
	$city = stripslashes($info['city']);
	$state = stripslashes($info['state']);
	$zip = stripslashes($info['zip']);
	$order_status = stripslashes($info['job_status']);
	$order_request = stripslashes($info['job_request']);
	$taken_by = stripslashes($info['taken_by']);
	$intake_stamp = stripslashes($info['intake_stamp']);

}
}
//mysqli_free_result($result);
mysqli_close($db);

var_dump($db);
?>

and I've been getting this error non-stop. No matter I do:

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\cagrape\update.php on line 26

var_dump says NULL and sometimes this:
Warning: var_dump() [function.var-dump]: Property access is not allowed yet in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Property access is not allowed yet in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Couldn't fetch mysqli in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Couldn't fetch mysqli in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Couldn't fetch mysqli in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Couldn't fetch mysqli in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Couldn't fetch mysqli in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Couldn't fetch mysqli in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Couldn't fetch mysqli in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Couldn't fetch mysqli in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Couldn't fetch mysqli in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Couldn't fetch mysqli in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Couldn't fetch mysqli in C:\xampp\htdocs\cagrape\update.php on line 28

Warning: var_dump() [function.var-dump]: Couldn't fetch mysqli in C:\xampp\htdocs\cagrape\update.php on line 28
object(mysqli)#1 (17) { ["affected_rows"]=> NULL ["client_info"]=> NULL ["client_version"]=> int(50141) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> NULL ["error"]=> NULL ["field_count"]=> NULL ["host_info"]=> NULL ["info"]=> NULL ["insert_id"]=> NULL ["server_info"]=> NULL ["server_version"]=> NULL ["sqlstate"]=> NULL ["protocol_version"]=> NULL ["thread_id"]=> NULL ["warning_count"]=> NULL }

It will connect fine with the other forms connecting to the same database.
Any reason Why?
Please help!

    I'd start the debugging by verifying whether or not mysqli_connect() is succeeding. Check to see if it's return value is false, and log/echo some debug info if it is, e.g.:

    <?php
    $db = mysqli_connect("localhost", "root", "");
    if($db == false)
    {
       die(mysqli_connect_error());
    }
    

    For the "real" code I would error_log() the error details and do something more user-friendly for the actual error output, but this should suffice for now to determine if your connection is working.

    PS: The general rule which this illustrates is the value of coding "defensively," checking return values and never assuming that things will work.

      It worked when I commented line 2 if (id_files) { . And I set a real value for id_files. I.E
      from:
      $sql = "SELECT id_files, name, tel, email, address1, address2, city, state, zip, order_status, order_request, intake_stamp, taken_by FROM holidayorders WHERE id_files=$id_files";
      To:
      $sql = "SELECT id_files, name, tel, email, address1, address2, city, state, zip, order_status, order_request, intake_stamp, taken_by FROM holidayorders WHERE id_files=1";

      But when I uncommented it the if ($id_files) {. The response was: Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\cagrape\update.php on line 38

        for some reason it is not taking the variable (id_files) from the previous page and using it in the sql search it comes up empty or with this:
        Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\cagrape\update.php on line 38 or " You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1" urg! this script works well on a webhoster but not on my laptop running XAMPP.

          it was called from main.php but how would I redefine a variable that constantly changes.
          What I'm trying to do is have a main grid with indivusal rows you click on view and it's supposed to go to another page that allows you to expand on that row (I.e change info, delete or add more info). This works on my website flawlessly same code too. But I'm trying to make this work on a localhost and now luck.

          <?php
          //if ($_COOKIE["auth"] == "1") {
          $company = $_COOKIE["auth"];
          
          include "open_db.inc";
          
          $sql = "SELECT * FROM holidayorders ";
          $sql .= "WHERE company = '".$company."' ";
          $sql .= "ORDER BY order_status ASC";
          $result = mysql_query($sql, $db);
          $rows = mysql_num_rows($result);
          
          echo "Sort By <strong><a href='main.php'>Job Status</a></strong> | <a href='main1.php'>Name</a> | <a href='main2.php'>Telephone</a> |   [ <a href='http://localhost/cagrape/tools'>Tools</a> ] <br>";
          echo "Job Status Sort By <a href='main.php'>All</a> | <a href='mainnew.php'>New</a> | <a href='mainpaid.php'>Paid</a> | <strong><a href='mainsched.php'>Scheduled</a></strong> | <a href='maincancel.php'>Cancelled</a> | <a href='mainrtb.php'>Ready to Bill</a> | <a href='mainshipped.php'>Shipped</a> ";
          echo "<table width=\"100%\" border=\"1\">\n";
          echo " <tr>\n";
          echo "  <td>Name </td>\n";
          echo "  <td>Telephone </td>\n";
          echo "  <td>Email </td>\n";
          echo "  <td>Address </td>\n";
          echo "  <td>P.O RR </td>\n";
          echo "  <td>City </td>\n";
          echo "  <td>State </td>\n";
          echo "  <td>Zip </td>\n";
          echo "  <td>Order Status </td>\n";
          echo "  <td>Holiday </td>\n";
          echo "  <td>Order Request </td>\n";
          echo "  <td>Taken By </td>\n";
          echo "  <td> </td>\n";
          echo "  <td> </td>\n";
          echo "  <td> </td>\n";
          echo " </tr>\n";
          
          for ($i = 0; $i < $rows; $i++) {
            $data = mysql_fetch_object($result);
          
            echo " <tr>\n";
           echo "  <td>" . stripslashes($data->name) . "</td>\n";
           echo "  <td>" . stripslashes($data->tel) ."</td>\n";
           echo "  <td>" . stripslashes($data->email) ."</td>\n";
           echo "  <td>" . stripslashes($data->address1) ."</td>\n";
           echo "  <td>" . stripslashes($data->address2) ."</td>\n";
           echo "  <td>" . stripslashes($data->city) ."</td>\n";
           echo "  <td>" . stripslashes($data->state) ."</td>\n";
           echo "  <td>" . stripslashes($data->zip) ."</td>\n";
           echo "  <td>" . stripslashes($data->order_status) ."</td>\n";
           echo "  <td>" . stripslashes($data->holiday) ."</td>\n";
           echo "  <td>" . stripslashes($data->order_request) ."</td>\n";
           echo "  <td>" . stripslashes($data->taken_by) ."</td>\n";
            echo "  <td><a href='ship.php?id_files=$data->id_files'>Report Payment</a></td>\n";
            echo "  <td><a href='superview.php?id_files=$data->id_files'>SuperView</a></td>\n";
            echo "  <td><a href='update.php?id_files=$data->id_files'>View</a></td>\n";
            echo " </tr>\n";
          }

            If it's passed in the query string, then the variable to use would be $_GET['id_files']. See this manual page for more information: [man]variables.external[/man].

            If using just $id_files without ever defining that variable works on your webhost, then you need to get register_globals disabled - it's been declared as a security risk and disabled by default (even removed in PHP6) for a long time. See this page for more info about that: [man]security.globals[/man].

            EDIT: Also note that if you're testing to see if external variables exist (something you should always do before using them), don't just do if($_GET['var']) - that will cause an E_NOTICE error message to be generated. Instead, use [man]isset/man or [man]empty/man.

              The funny thing is I was thinking about $_GET[] but I tried it and got the same error. However, because I'm a sucker for punishment, i tried it in my sql statement:
              <?php

              $id_files = $GET["id_files"];
              .
              .
              $sql = "SELECT * FROM holidayorders WHERE id_files = '".$
              GET["id_files"]."'";

              And then it worked. I thank you very much for your help. The computer can cancel it's in flight movie 😛

                Don't forget, you should never place user-supplied data directly into a SQL query, else you'll be vulnerable to SQL injection attacks and/or just plain SQL errors. Instead, it must first be sanitized with a function such as [man]mysql_real_escape_string/man.

                If this 'id' field is supposed to be an integer, however, you could simply cast it to (int) - or use [man]intval/man - instead (and also remember that numeric data types do not get surrounded by quotes in a SQL query).

                  Write a Reply...