Warning: mysqli_query() expects parameter 1 to be mysqli
Results 1 to 6 of 6

Thread: Warning: mysqli_query() expects parameter 1 to be mysqli

  1. #1
    Junior Member
    Join Date
    Mar 2008
    Posts
    2

    Warning: mysqli_query() expects parameter 1 to be mysqli

    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


    Code:
    <?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.

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,435
    What happens if you do a var_dump() on $link just after you try to connect?

  3. #3
    Junior Member
    Join Date
    Mar 2008
    Posts
    2
    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

  4. #4
    Junior Member
    Join Date
    Mar 2008
    Posts
    1

    Me too

    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

    Quote Originally Posted by bubbaness
    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


    Code:
    <?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.

  5. #5
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,534
    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 Code:
    <?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);

    ?>
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  6. #6
    junior member
    Join Date
    Nov 2009
    Location
    Canada
    Posts
    1

    mysql_query error

    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •