Thread: Mysqli_error

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    133
    Rep Power
    0

    Mysqli_error


    Hi


    I am having this problem.. it is saying this error .. how I can get rid of it :/ .. I tried to put and remove $con but it is not going away. Pleas guide




    Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\CMS\mysql\functions.php on line 36

    Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\CMS\mysql\functions.php on line 36



    My codes I have put in files are

    PHP Code:
    function readAll(){
      global 
    $con;
      
    $query "SELECT * FROM  users";
      
    $result mysqli_query($con$query) or die (mysqli_error($con));

      while(
    $row mysqli_fetch_assoc($result)){
        
    print_r($row);
      }
    //while loop





    PHP Code:
    <?php
    include "functions.php";
    include 
    "include/header.php";
    ?>


        <section class="container">
          <div class="row">
            <div class="col-md-6">
              <?php  readAll(); ?>
           </div>
        </section>



    <?php include "include/footer.php"?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,451
    Rep Power
    652
    You don't show the code where you set $con but that is where the problem is. I doubt you should be using 'global'. That in itself makes your coding technique suspect.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    133
    Rep Power
    0
    What I do now ??

    PHP Code:
    <?php

    $con 
    mysqli_connect('localhost','root','','dbcms');
    if (!
    $con ) {
      die(
    "Database Connection not Established");
    }
      
    mysqli_select_db($con,"dbcms");

     
    ?>
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,451
    Rep Power
    652
    You need to post all your code. Assuming your code did not execute the 'die' then the problem is likely that the $con variables are different. Again that you are using 'global' indicates a basic flaw in your technique. Why do you think you need that?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    133
    Rep Power
    0
    This is all the code I have for this learning website I am trying to do .. I am pasting all my code here .. I am using Global $con because this is database variable I made in db.php !

    login-read.php

    PHP Code:
    <?php
    include "functions.php";
    include 
    "include/header.php";
    ?>


        <section class="container">
          <div class="row">
            <div class="col-md-6">
              <?php  readAll(); ?>
           </div>
        </section>



    <?php include "include/footer.php"?>
    PHP Code:

    <?php
    include "db.php";


    function 
    readAll(){
      global 
    $con;
      
    $query "SELECT * FROM  users";
      
    $result mysqli_query($con$query) or die (mysqli_error($con));

      while(
    $row mysqli_fetch_assoc($result)){
        
    print_r($row);
      }
    //while loop


    }


    ?>

    db.php



    PHP Code:

    <?php

    $con 
    mysqli_connect('localhost','root','','dbcms');
    if (!
    $con ) {
      die(
    "Database Connection not Established");
    }
      
    mysqli_select_db($con,"dbcms");

     
    ?>
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,451
    Rep Power
    652
    Remove the global declaration and pass $con as an argument to readAll. BTW, where do you include the PHP code that defines 'readAll'? There is no real reason for breaking up the code like that and the mysqli_select_db is unnecessary/redundant.
    Last edited by gw1500se; June 5th, 2018 at 05:10 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    136
    Rep Power
    65
    Given that the error is on line 36 in functions.php, there's a lot of code that hasn't been posted. I'm going to guess that it is in some of this non-posted code that the connection is being destroyed (set to null.)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    133
    Rep Power
    0
    Guys what else code should I post here ? all others are working fine .. basically its about CRUD .. in this login-read.php file it is creating issues :/ else is fine !
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    133
    Rep Power
    0
    It means I have to work on referencing ? right ?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,451
    Rep Power
    652
    All your code.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    136
    Rep Power
    65
    The task at hand is to find out why a variable on line 36 of C:\xampp\htdocs\CMS\mysql\functions.php, being used in a mysqli context, is a null value.

    The code you have posted and implied is from that file is missing approximately 29-30 lines of code. It could be that missing code that is causing the problem.

    Either you are not posting the whole code or you have multiple files at different paths and you are posting the wrong code.

    Posting all the code, which would show us what IS working, would both let us see what you are doing different for this problem code and it would let us see what the rest of the code could be doing that could cause this problem if you are running some of the other code prior to running the code at line 36 in the file where the error is occurring.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    133
    Rep Power
    0
    Guys its working now ... I put the connection string inside this function .. so nice of you all giving the good advice Thanks a lot

IMN logo majestic logo threadwatch logo seochat tools logo