SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question mysqli_query null error

    Hi hows it going, I tried switching to mysqli, but not working out as great as I hoped.
    I get a : Warning: mysqli_query() expects parameter 1 to be mysqli, null given in **** line 35 36 37
    which is the mysqli_query below statements.
    What might be wrong with it?

    Thanks
    Chad

    PHP Code:
    function init(){

        
        
    session_start();
        if (isset(
    $_SESSION['active']) != true) {
            
    $_SESSION['active'] = true;
        } 
        
    $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
        
        if(isset(
    $_GET['logout'])){
            
    session_destroy();
            
    header'Location: /index.php' ) ;
        
        }    
        
        
        
    //connect db
        
    $con mysqli_connect("localhost""dbuserr""password""dbname"); 
        
        if (
    mysqli_connect_errno())
          {
            echo 
    mysqli_connect_error();
            exit();
          }

        
        global 
    $siteName;
        global 
    $adminEmail;
        global 
    $siteUrl;
        
    $adminEmail mysqli_query($con"SELECT email FROM admin LIMIT 1");
        
    $siteUrl mysqli_query($con"SELECT url FROM admin LIMIT 1");
        
    $siteName mysqli_query($con"SELECT name FROM admin LIMIT 1");


  2. #2
    SitePoint Enthusiast Belsnickle's Avatar
    Join Date
    Feb 2012
    Location
    Chico, CA
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not seeing anything particularly off, have you double checked your database password, user, location information?

    Also three things of note;

    If this is going to be initialized on every call of the class it's in just make those names match, that automatically does it. So if your class is named TESTCLASS naming the function TESTCLASS automatically runs it every time the class is used.

    Generally it's a good idea to avoid globals, since this is clearly user specfic information why not use Session variables instead if it's something you'll be using across pages or as a Class VAR if it's only in this class.

    Is there a particular reason you're accessing the database three times instead of just doing something like below? Best practice is to minimize queries whenever possible, you'll appreciate being in the habit of it later as that's usally the biggest bottleneck.
    Code:
    $result =  mysqli_query($con,"SELECT name, email, url FROM admin LIMIT 1");
    $row = mysqli_fetch_array($result);
    
     $adminEmail = $row['email'];
     $siteUrl = $row['url'];
     $siteName = $row['namel'];

  3. #3
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by Belsnickle View Post
    I'm not seeing anything particularly off, have you double checked your database password, user, location information?

    Also three things of note;

    If this is going to be initialized on every call of the class it's in just make those names match, that automatically does it. So if your class is named TESTCLASS naming the function TESTCLASS automatically runs it every time the class is used.

    Generally it's a good idea to avoid globals, since this is clearly user specfic information why not use Session variables instead if it's something you'll be using across pages or as a Class VAR if it's only in this class.

    Is there a particular reason you're accessing the database three times instead of just doing something like below? Best practice is to minimize queries whenever possible, you'll appreciate being in the habit of it later as that's usally the biggest bottleneck.
    Code:
    $result =  mysqli_query($con,"SELECT name, email, url FROM admin LIMIT 1");
    $row = mysqli_fetch_array($result);
    
     $adminEmail = $row['email'];
     $siteUrl = $row['url'];
     $siteName = $row['namel'];
    This code is in the function.php file, and every other php page is included to this page.
    I have the init function to start the sql database.
    I did what you said and added the code above, this is code now:

    PHP Code:
    $con mysqli_connect("localhost""db_user""password""db_name"); 
    init();

    function 
    init(){

        
        
    session_start();
        if (isset(
    $_SESSION['active']) != true) {
            
    $_SESSION['active'] = true;
        } 
        
    $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
        
        if(isset(
    $_GET['logout'])){
            
    session_destroy();
            
    header'Location: /index.php' ) ;
        
        }    
        
        if (
    mysqli_connect_errno())
          {
            echo 
    mysqli_connect_error();
            exit();
          }
    $result =  mysqli_query($con"SELECT name, email, url FROM admin LIMIT 1");
    $row mysqli_fetch_array($result);

     
    $adminEmail $row['email'];
     
    $siteUrl $row['url'];
     
    $siteName $row['namel'];

    I checked the database name and pass its the same, and Im not getting an error that is not connecting to the database via: mysqli_connect_errno

    I'm getting these errors now:
    mysqli_query() expects parameter 1 to be mysqli, null given in /home3/clfarmerfunction.php on line 25
    mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home3public_html/function.php on line 26

    which is the:
    PHP Code:
    $result =  mysqli_query($con"SELECT name, email, url FROM admin LIMIT 1");
    $row mysqli_fetch_array($result); 

  4. #4
    SitePoint Enthusiast Strider64's Avatar
    Join Date
    Aug 2012
    Posts
    39
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $query "SELECT name, email, url FROM admin LIMIT 1";
    $result mysqli_query($con$query);

    /* associative array */
    $row mysqli_fetch_array($resultMYSQLI_ASSOC);
    echo 
    $row['name'] . '<br>';
    echo 
    $row['email'] . '<br>';
    echo 
    $row['url'] . '<br>';
    /* free result set */
    mysqli_free_result($result);
    /* close connection */
    mysqli_close($con); 

  5. #5
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    init
    ($con);

    function 
    init($con){ 
    You need to pass $con to the init method for it be be able to use it
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  6. #6
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    PHP Code:

    init
    ($con);

    function 
    init($con){ 
    You need to pass $con to the init method for it be be able to use it
    Wow that did it, thank you so much!


Bookmarks

Posting Permissions

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