0

I have a problem, so I want to do that if by chance the user searched on and the code for it was no longer activated. In this way, I made a simple code:

$loginu = $_SESSION['login'];
$query = "SELECT `usrlogin`,`idcode` FROM `aktywacja` WHERE idcode = $numer AND usrlogin = $loginu ";
$result2 = mysql_query($query);
echo mysql_error();
if (mysql_num_rows($result2) == 0) {
    not found
}
else {
    found
}

and crashes me the following errors

Unknown column 'Kamil' in 'where clause' Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\kod.php on line 56 Where in this case I made a mistake?

Please help,

greet.

Kevin
  • 41,694
  • 12
  • 53
  • 70
Kejl
  • 53
  • 1
  • 1
  • 9

3 Answers3

2

Just make sure that those strings are wrapped with quotes if they are not integers:

WHERE idcode = $numer AND usrlogin = '$loginu'

And make sure that this variable $numer is indeed defined, since in your question there is no definition of it.

Obligatory note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Here's what it would look like when used in mysqli with prepared statements:

$loginu = $_SESSION['login'];
$numer = // make sure this is defined!    

$db = new mysqli('localhost', 'username', 'password', 'database_name');

$query = "SELECT `usrlogin`,`idcode` FROM `aktywacja` WHERE idcode = ? AND usrlogin = ?";
$select = $db->prepare($query);
// binding them
$select->bind_param('is', $numer, $loginu);
$select->execute();

if($select->num_rows > 0) {
    // found
} else {
    // not found
}
Community
  • 1
  • 1
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Thanks, I used your code, however, even as there is no record that is displayed. – Kejl Nov 08 '14 at 10:59
  • @Kejl could you paraphrase `however, even as there is no record that is displayed.`? sorry i don't understand\ – Kevin Nov 08 '14 at 11:05
  • I have used and I have modified your code and displays despite the fact that there are no records in the database that the record found. My English is not good :( – Kejl Nov 08 '14 at 11:23
  • @Kejl you're saying that even if the table is empty `num_rows` is greater than zero? – Kevin Nov 08 '14 at 11:27
  • @Kejl whats the value of `$znaleziono`? – Kevin Nov 08 '14 at 11:43
  • variable found is the numerical value $znaleziono - 0 when there is no record in the database, or 1 if there were record – Kejl Nov 08 '14 at 11:52
  • @Kejl i don't think there is a problem in the code anymore, just that its interchanged, i think when numrows is greater than zero it should be `$znaleziono = 1` otherwise zero – Kevin Nov 08 '14 at 11:55
  • help me fix it somehow and present his version of the code so that the variable $finded (old $znaleziono) = 1 when it finds or 0 if it does not find in the database record – Kejl Nov 08 '14 at 12:00
  • @Kejl just change them accordingly, or just plain `echo $select->num_rows;` – Kevin Nov 08 '14 at 12:04
0
    check this code and if condtion check to not blank a session data.
    and check a query value is yes to found and else to not found print.

    $loginu = $_SESSION['login'];
    if(isset($loginu))
    {
        $query="SELECT `usrlogin`,`idcode` FROM `aktywacja` WHERE idcode='".$numer."' AND usrlogin ='".$loginu."'";
        $result = mysql_query($query);
        if(mysql_num_rows($result) > 0) 
        {
            echo "found";
        }
        else
        {
            echo "not found";
        }

    }
0

Try changing this line in you query WHERE idcode='".$numer."' AND usrlogin ='".$loginu."'