-4

Notice: Undefined variable: mysqli in C:\xampp\htdocs\quizzer\add.php on line 67

Fatal error: Call to a member function query() on null in >C:\xampp\htdocs\quizzer\add.php on line 67

Here's my code which is giving error:

$questions = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $questions->num_rows;
$next = $total+1;
Community
  • 1
  • 1
  • 2
    mysql connection missing – JYoThI May 02 '17 at 06:50
  • have you set the $query? – Mattia Dinosaur May 02 '17 at 06:51
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Tom Udding May 02 '17 at 06:54
  • 2
    The error message is crystal clear. What is it you don't understand in that? You call a member function `$mysqli->query()`, right? The message says you called it on something with value `null`, so on nothing, right? If `$mysqli` is nothing, then how should that work? – arkascha May 02 '17 at 06:54
  • $query = " SELECT * FROM 'questions'"; – Akmaljon Usmonov May 02 '17 at 06:55
  • 1
    @AkmaljonUsmonov If you didn't used the [`mysqli`](http://php.net/manual/en/class.mysqli.php) class, why are you trying to do it with `$mysqli`? you can do this: `$mysqli = new mysqli(host, user, pass, db)`. –  May 02 '17 at 07:00
  • `$con` is different from `$mysqli`... – arkascha May 02 '17 at 07:12

2 Answers2

2

It seems like you didn't set $mysqli.

$mysqli = new mysqli(host, user, pass, db);

(Replace host, user, pass and db with your database values, e.g: localhost, root, root, example_db)

Learn more about MySQLi on PHP Documentation

  • No, `$mysqli` is _not_ a class_! It is an object that has been instantiated on the class `mysqli`. You seem to confuse the terms "class" and "object". – arkascha May 02 '17 at 07:19
  • @arkascha I know that variable is not a class. `new mysqli`, that's where I call the class... –  May 02 '17 at 07:21
  • But that is not what you wrote, sorry. Anyway, you changed your wording to something very generic now, so all is fine. That is why I upvoted. – arkascha May 02 '17 at 07:21
  • @arkascha Thanks. About my wording: I apologize If I had any misspelling(s). please feel free to correct me / update my answer. :-) –  May 02 '17 at 07:24
  • All fine, have fun! – arkascha May 02 '17 at 07:25
2

1) Connection missing

2) Remove single quotes around table name .

3) use Prepared statement

note : if we need to access the connection variable inside any function means .we can access it using that global variable.

//db connection

     global $conn;

        $servername = "localhost";  //host name

        $username = "username"; //username

        $password = "password"; //password

        $mysql_database = "dbname"; //database name

    //mysqli prepared statement 

        $conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());

       mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");

       $query = "SELECT * FROM questions";

        $stmt = $conn->prepare($query);
        $stmt->execute();
        $get_result =$stmt->get_result();

        $row_count= $get_result->num_rows;

        if($row_count>0)
        {

           while($row=$get_result->fetch_assoc())
           {

             print_r($row);
           }

        }
JYoThI
  • 11,977
  • 1
  • 11
  • 26
  • This uses a confusing mix of object-oriented style, which is preferred, and the needlessly verbose procedural methods. – tadman May 02 '17 at 07:30
  • can you tell me your mail please guys, i ll send the whole code. anyway it is not working. my gmail Ausmonov24@gmail.com – Akmaljon Usmonov May 02 '17 at 07:48
  • NOT WORKING means where your facing issue ? – JYoThI May 02 '17 at 07:49
  • I don't think the `global $conn` will only work in a function or method context (or if it does, it should nevertheless be removed - the dangers of copy and paste programming!). Will undownvote if that is removed. – halfer May 02 '17 at 08:09
  • @Akmaljon: please do not ask for private support here. The whole point of helping on the internet is to make the assistance public so the material written is useful for future readers as well. Volunteers rarely have time for free debugging sessions anyway, which can balloon into hours worth of work. If you need private support, consider hiring a freelancer from AirPair or TopTal. – halfer May 02 '17 at 08:16
  • it's not copy paste . if we need to acess the connection variable inside any function means ..we can access it using that global variable @halfer – JYoThI May 02 '17 at 08:27
  • Yes, but it is not inside a function, and beginners will copy that `global` line needlessly, since you have not explained that it should go inside a function. – halfer May 02 '17 at 08:29
  • @Akmaljon: please do not post large amounts of code in comments either. If you have a new problem, please ask a new question. However, there is much value in your working through the bugs that you have, one by one - and of course read the error messages to see what PHP is wanting to tell you. – halfer May 02 '17 at 08:32
  • if my answer is useful mark it with green tick its useful for future user reference @AkmaljonUsmonov – JYoThI May 03 '17 at 02:33