-1

I am using following code which is not working for me.

$con=mysqli_connect("localhost","root","","my_db");
$check="SELECT COUNT(*) FROM persons WHERE Email = '$_POST[eMailTxt]'";
if (mysqli_query($con,$check)>=1)
{
    echo "User Already in Exists<br/>";
}
else
{
    $newUser="INSERT INTO persons(Email,FirstName,LastName,PassWord) values('$_POST[eMailTxt]','$_POST[NameTxt]','$_POST[LnameTxt]','$_POST[passWordTxt]')";
    if (mysqli_query($con,$newUser))
    {
        echo "You are now registered<br/>";
    }
    else
    {
        echo "Error adding user in database<br/>";
    }
}

Object of class mysqli_result could not be converted to int in C:\xampp\htdocs\Exp\welcome.php

Dharman
  • 30,962
  • 25
  • 85
  • 135
Prodeep Chatterjee
  • 255
  • 2
  • 5
  • 17
  • 5
    http://en.wikipedia.org/wiki/SQL_injection – Hamish Jul 04 '13 at 08:31
  • mysqli_query should return a result set, one element of which will be the column. It is the value of that column you want to check for being greater than or equal to 1, not the result set itself. – Kickstart Jul 04 '13 at 08:32

4 Answers4

5

this code works fine for you...

$con=mysqli_connect("localhost","root","","my_db");
$check="SELECT * FROM persons WHERE Email = '$_POST[eMailTxt]'";
$rs = mysqli_query($con,$check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if($data[0] > 1) {
    echo "User Already in Exists<br/>";
}

else
{
    $newUser="INSERT INTO persons(Email,FirstName,LastName,PassWord) values('$_POST[eMailTxt]','$_POST[NameTxt]','$_POST[LnameTxt]','$_POST[passWordTxt]')";
    if (mysqli_query($con,$newUser))
    {
        echo "You are now registered<br/>";
    }
    else
    {
        echo "Error adding user in database<br/>";
    }
}
Vijay
  • 8,131
  • 11
  • 43
  • 69
3

mysqli_query function returns a resultset handle. You then need to read the rows from it:

$rs = mysqli_query($con,$check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if($data[0] > 1) {
    //user exists;
}

Also note that SELECT count(1) FROM ... will be faster than SELECT count(*) FROM ... You won't see much of a difference in a small table, but with a large table of several hundred thousand rows, the difference may be significant.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
2

mysqli_query returns a mysqli_result object. Rather than comparing this to an integer try using num_rows:

$res = mysqli_query($con,$check);
if($res->num_rows){
    //User exists
}

Edit: The above assumed the query was using SELECT * and this will not work with SELECT COUNT(*). Checkout Aleks G's answer.

Jim
  • 22,354
  • 6
  • 52
  • 80
  • I agree with the basic error, but he is returning a count. Even if no records are found which match on email address a count row will still be returned (with a count of 0 in it). – Kickstart Jul 04 '13 at 08:34
  • 1
    This is incorrect with his query. The OP's query will always return 1 row, but he's interested in the value in that row. – Aleks G Jul 04 '13 at 08:34
-1

You can try the following code:

$query_code = "SELECT COUNT(itemCode) FROM masterData WHERE itemCode='{$itemCode}'";
$result_login = mysqli_query($conn,$query_code);
$anything_found = mysqli_num_rows($result_login);

if($anything_found > 0) {
    $formOk = false;
    echo "ITEM CODE ALREADY EXISTS! Please try again.";  
}
Naktibalda
  • 13,705
  • 5
  • 35
  • 51