1

I was trying to validate if the username and email in my users table. The validation is working if the username or email is existing in the table it will not be stored but after the submission I only see a white screen in my browser and when I check the server logs I got this error.

PHP Fatal error: Uncaught Error: Call to undefined method

mysqli::mysqli_num_rows() in /var/www/html/NutriCare/signup.php:86\nStack trace:\n#0 {main}\n thrown in /var/www/html/NutriCare/signup.php on line 86

Is there something wrong in my code in database connection?

Here is the code in my config.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "db";

$conn = new mysqli($servername, $username, $password, $db);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?> 

Here is the code where I validate the username and email in my signup.php and included the config.php file here

<?php
require 'config.php';
$query = "SELECT username FROM users WHERE username='$username'";
$result = mysqli_query($conn, $query);
$query2 = "SELECT email FROM users WHERE email='$email'";
$result2 = mysqli_query($conn, $query2);

if ($conn->mysqli_num_rows($result) != 0) {
    ?>
    <script>
        sweetAlert("Error!", "Username already exist!", "error");
            return false;
    </script>
    <?php
}

if ($conn->mysqli_num_rows($result2) != 0) {
    ?>
    <script>
        sweetAlert("Error!", "Email address already exist!", "error");
            return false;
    </script>
    <?php
}
?>

But when I remove this code to validate the email and the username the processing of storing data works with no errors. I had included the $conn->close(); code in the end of the php file I just posted the code I put when I encountered the error. Looking for help. Thanks in advance.

Jaaayz
  • 1,533
  • 7
  • 27
  • 59
  • 2
    Dont look at the browser when you get an `Internal Server Error`, look at your error logs on the server. – chris85 Aug 25 '17 at 01:31
  • 2
    you didn't query – Funk Forty Niner Aug 25 '17 at 01:32
  • 2
    `$query` is just a string, not a result object. You need to execute the query, but dont, you also need to parameterize that first. – chris85 Aug 25 '17 at 01:32
  • 1
    you also can do this in one single query – Funk Forty Niner Aug 25 '17 at 01:34
  • Like there? I updated the code but still wont work – Jaaayz Aug 25 '17 at 01:41
  • Closer, what does the error log actually say? – chris85 Aug 25 '17 at 01:43
  • 1
    add require 'config.php'; in signup.php – Barclick Flores Velasquez Aug 25 '17 at 01:44
  • It's just a whitespace in the browser I didn't see any error but it when I remove the code to validate the email and the user it works. – Jaaayz Aug 25 '17 at 01:46
  • 1
    You **cant** get server errors in your browser you need to check the actual **server** for the error. – chris85 Aug 25 '17 at 01:47
  • the server responded with a status of 500 (Internal Server Error) here? – Jaaayz Aug 25 '17 at 01:53
  • 1
    as told Barclick Flores Velasquez you didnt include config.php, that's way your $conn variable is empty so mysqli_query function will raise error on the server side, returning 500 error to you with a blank page if your http server supresses php errors – diavolic Aug 25 '17 at 01:55
  • That is in your browser. You **NEED** to go to the server and open the error logs. https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel – chris85 Aug 25 '17 at 01:55
  • 1
    @BarclickFloresVelasquez Please dont use the `edit` feature to introduce code changes. Post an answer, or comment (as you have done). – chris85 Aug 25 '17 at 01:57
  • Here is the error [Fri Aug 25 09:39:24.609496 2017] [:error] [pid 4438] [client ::1:34590] PHP Fatal error: Uncaught Error: Call to undefined method mysqli::mysqli_num_rows() in /var/www/html/NutriCare/signup.php:86\nStack trace:\n#0 {main}\n thrown in /var/www/html/NutriCare/signup.php on line 86, referer: http://localhost/NutriCare/signup.php sorry I didn't knew how to check server errors in that way. Now I know already thanks. – Jaaayz Aug 25 '17 at 02:20
  • @chris85 In response to [this/your comment](https://stackoverflow.com/questions/45873116/php-error-when-i-validate-if-the-username-and-email-already-existed-in-the-table/45881328#comment78705439_45873116) *"Post an answer"*, that doesn't look like they've actually posted a solution, it's the way they're (now) querying/checking if a row exists which is incorrect. I revisited the question and shown them where they went wrong. – Funk Forty Niner Aug 25 '17 at 12:53
  • @Fred-ii- It was more a note for future usage. The user had proposed an edit to the original code which would've have changed the execution and issue the OP was having. (can be seen here, https://stackoverflow.com/review/suggested-edits/17139233) – chris85 Aug 25 '17 at 12:59
  • @chris85 Oh, I see. Thanks for pointing that out. – Funk Forty Niner Aug 25 '17 at 12:59

1 Answers1

1

Both of your queries to check if a row exist are incorrect.

if ($conn->mysqli_num_rows($result) != 0)
    ^^^^^^^

and

if ($conn->mysqli_num_rows($result2) != 0)
    ^^^^^^^

You don't pass the connection variable for a result set (from a query), you use the variable(s) from the query's (successful) results only.

Modify your code to read as:

if (mysqli_num_rows($result) != 0)
                 // ^^^^^^^ $result is all you need here

and

if (mysqli_num_rows($result2) != 0)
                 // ^^^^^^^^ $result2 is all you need here

NOTE: If the != logical operator does not work, use > 0 instead. The != 0 could throw a false positive.

You could also do this in one query by using the logical OR or AND operators. But, this could be out of the scope of the question.

Bear in mind that your code is open to an SQL injection. Use a prepared statement:

You can also view one of my answers that contain a few prepared statement methods:

Check for errors on the PHP and MySQL/query side also.

References:

Reference on PHP's mysqli_num_rows():


(Disclaimer)

I provided this answer for support on the PHP/MySQL part and cannot offer support on the use of the SweetAlert script you are using in conjunction with this.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141