1

Ok, so it seems like my SELECT count query doesn't work here:

<?php

    $servername = " "; 
    $username = " "; 
    $password = " "; 
    $dbname = " "; 

    // Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 
    // Check connection 
    if ($conn->connect_error) { 
        die("Connection failed: " . $conn->connect_error); 
    } 


    if (isset($_POST['submitted1'])) {

        $result= mysqli_query("SELECT COUNT (Motspillerid) 
                              FROM SESSION 
                              WHERE Motspillerid= 3 ");
        echo $result ;
    } else {
        echo "Wrong" ;     
    }

?>

And when I press submitt nothing happens, I don't get any error message and I don't get the result. So it's something wrong with the SELECT query I guess.

I'm noob I know, I'm new to this.

:)

  • Try to echo the query and perform it through phpMyAdmin to see what happen. **Please also note: mysql_** syntax was deprecated in PHP 5.5.0, and **it was removed** in PHP 7.0.0. Instead, the **[MySQLi](http://nl3.php.net/manual/en/book.mysqli.php)** or **[PDO_MySQL](http://nl1.php.net/manual/en/ref.pdo-mysql.php)** extension should be used. – fusion3k Feb 10 '16 at 22:19
  • 1
    Change `echo $result` to `var_dump($result)`. Then, [stop using mysql and start using PDO or mysqli](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189). – random_user_name Feb 10 '16 at 22:19
  • 1
    Might want to fetch a row from the result first! – AbraCadaver Feb 10 '16 at 22:22
  • Ok @cale_b , I did that, and know I'm getting something. The error message "Wrong" xD. So it's still something wrong here – William Zhong Feb 10 '16 at 22:23
  • Please dont use the `mysql_` database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). Try a tutorial like this one http://www.jayblanchard.net/demystifying_php_pdo.html – RiggsFolly Feb 10 '16 at 22:26
  • Ok, anyways, if I change the variable criteria to a specific data like : motspillerid = 3 ; I still get the error message Wrong. – William Zhong Feb 10 '16 at 22:27
  • If you change your question in future please post a comment saying so, that way people who have already given an answer will know that they may need to change their answer accordingly. _Although you are not supposed to change an question once an answer has been provided as it makes existing answers look ridiculous_ You are only supposed to edit the question to add more detail! – RiggsFolly Feb 10 '16 at 23:45
  • oki, I apologize for that – William Zhong Feb 10 '16 at 23:52

1 Answers1

0

You're not calling mysqli_query() correctly, the first argument has to be the database connection object returned by mysqli_connect.

And after performing the query, you have to fetch the results using one of the mysqli_fetch_X() functions.

if (isset($_POST['submitted1'])) {

    $result= mysqli_query($conn, "SELECT COUNT(*) AS count
                          FROM SESSION 
                          WHERE Motspillerid= 3 ");
    $row = mysqli_fetch_assoc($result);
    echo $row['count'];
} else {
    echo "Wrong" ;     
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Now I get the error message : Couldn't fetch mysqli in ... and mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in – William Zhong Feb 10 '16 at 22:56
  • Did you set `$conn` before this? How did you set it? – Barmar Feb 10 '16 at 22:59
  • $servername = " "; $username = " "; $password = " "; $dbname = " "; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } @Barmar – William Zhong Feb 10 '16 at 23:15
  • Are you running the query in a different function than the one that opens the connection? You need to pass `$conn` as a parameter to the function, or use `global $conn;`. – Barmar Feb 10 '16 at 23:22
  • Couldn't fetch mysqli in .....Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in .... – William Zhong Feb 10 '16 at 23:29
  • Do you understand PHP variable scoping? – Barmar Feb 10 '16 at 23:30
  • `couldn't fetch mysqli` means there's something wrong with the `$conn` argument to `mysqli_query`. All you have to do is google for that error message and you'll find other SO questions that explain this. – Barmar Feb 10 '16 at 23:31
  • Ops, I'm bit sleepy hehe, no I don't know what variable scoping is – William Zhong Feb 10 '16 at 23:32
  • A variable that's assigned outside a function isn't visible inside the function, unless you use the `global` declaration in the function. – Barmar Feb 10 '16 at 23:32
  • Read http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and/16959577#16959577 – Barmar Feb 10 '16 at 23:34