Hi everyone,

I've seen a few of these posts but none have solved my issue. I am an extreme newbie, if someone could tell me why I get this error (and what it means) I would be grateful.

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\cms\insert_record.php on line 22

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\wamp\www\cms\insert_record.php on line 25

Unable to query the database: .

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\wamp\www\cms\insert_record.php on line 29

<?php 
	if(isset($_POST['title'])) {

$link = mysqli_connect('localhost', 'XXX, '', 'XXX');
if (mysqli_connect_errno()) {
   echo '<p>Cannot connect to DB: ' . mysqli_connect_error() . '</p>'; 

   }

} else {
$title = $_POST['title'];
$content = $_POST['content'];
$keywords = $_POST['keywords'];

$query = 'INSERT INTO `documents`
(`title`, `contents`, keywords`)
VALUES
(\'' . $title . '\', \'' . $content . '\', \'' . $keywords . '\')'; 

}

$result=mysqli_query($link, $query);

if (!$result) {
	echo '<p>Unable to query the database: ' . mysqli_error($link) . ' .</p>';
	} else {
		echo '<p>Added!</p>';
		}
		mysqli_close($link);

	?>

Thanks in advance, I've been at this one for hours. 🙂

    What happens if you do a [man]var_dump/man on $link just after you try to connect?

      Thanks for the quick reply...

      How strange (or is it)... absolutely nothing happened with the var_dump, same errors appear.

      I know I did that right as it worked on another similar file.

      EDIT - If I put it under any of the other mysqli... I get: NULL

        11 days later

        I am having the same error, however of course I can't find enough info on this error anywhere on the web, I am using wampserver as well

        bubbaness wrote:

        Hi everyone,

        I've seen a few of these posts but none have solved my issue. I am an extreme newbie, if someone could tell me why I get this error (and what it means) I would be grateful.

        Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\cms\insert_record.php on line 22

        Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\wamp\www\cms\insert_record.php on line 25

        Unable to query the database: .

        Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\wamp\www\cms\insert_record.php on line 29

        <?php 
        	if(isset($_POST['title'])) {
        
        $link = mysqli_connect('localhost', 'XXX, '', 'XXX');
        if (mysqli_connect_errno()) {
           echo '<p>Cannot connect to DB: ' . mysqli_connect_error() . '</p>'; 
        
           }
        
        } else {
        $title = $_POST['title'];
        $content = $_POST['content'];
        $keywords = $_POST['keywords'];
        
        $query = 'INSERT INTO `documents`
        (`title`, `contents`, keywords`)
        VALUES
        (\'' . $title . '\', \'' . $content . '\', \'' . $keywords . '\')'; 
        
        }
        
        $result=mysqli_query($link, $query);
        
        if (!$result) {
        	echo '<p>Unable to query the database: ' . mysqli_error($link) . ' .</p>';
        	} else {
        		echo '<p>Added!</p>';
        		}
        		mysqli_close($link);
        
        	?>

        Thanks in advance, I've been at this one for hours. 🙂

          I suspect the problem in the original post was that $POST['title'] was not set for some reason, and the code logic has the mysqli_connect running only if $POST['title'] is set, but the else block that follows does the actual mysqli commands, which are failing because the connection was never even attempted.

          It's a bit easier to see after cleaning up the indenting:

          <?php
          if(isset($_POST['title'])) {
          // connection only done if this condition is true...
             $link = mysqli_connect('localhost', 'XXX', '', 'XXX'); // added missing quote here
             if (mysqli_connect_errno()) {
                echo '<p>Cannot connect to DB: ' . mysqli_connect_error() . '</p>';
          
             }
          // this query is only defined if condition is NOT true
          } else {
             $title = $_POST['title'];
             $content = $_POST['content'];
             $keywords = $_POST['keywords'];
          
             $query = 'INSERT INTO `documents`
             (`title`, `contents`, keywords`)
             VALUES
             (\'' . $title . '\', \'' . $content . '\', \'' . $keywords . '\')';
          
          }
          // this stuff gets executed regarless of the isset() condition...
          $result=mysqli_query($link, $query);
          
          if (!$result) {
             echo '<p>Unable to query the database: ' . mysqli_error($link) . ' .</p>';
          } else {
             echo '<p>Added!</p>';
          }
          mysqli_close($link);
          
          ?>
          
            2 years later

            I had a similar error and it cleared when I added the following code after a successful connection to the database MySQL server and before querying;

            mysqli_select_db($link, "dbname");

            Hope this helps.

              Write a Reply...