Jump to content

Want some more help from seniors


Recommended Posts

function checkusername($Username){
    global $cn;
    $sql = "SELECT username FROM admin WHERE username=:username";
    $stmt = $cn->prepare($sql);
    $stmt->bindValue(":username",$Username);
    $stmt->execute();
    $Result = $stmt->rowCount();
    if($Result==1) {
        return true;
    }else {
        return false;
    }
}

hi my code all code is running well  this function i made checking the username in the database when i execute the code this code didn't check the username in the database its insert the data into the database with out checking the duplicate entry  

Link to comment
Share on other sites

From the manual (https://www.php.net/manual/en/pdostatement.rowcount.php)

Quote

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

You could use this to see if a row was returned...

$stmt->execute();
return $stmt->fetch();

However a better way would be to put a UNIQUE constraint on the username column. You then just insert the new username but check for a "duplicate key" error. This saves you from having check every insert and you only need to take action if the insert failed.

Edited by Barand
typo
Link to comment
Share on other sites

1 hour ago, Ehsan_collboy said:

when i execute the code this code didn't check the username in the database

How did you determine that he code didn't do the check you wanted?  

And what are you saying after the quoted line?  I don't understand your mention of "insert" here.  I realize that you may have some English difficulties.

Did the rowcount call not give you anything?  Do you know for sure that the username does exist exactly as provided?

And Why Do You Think We Are "seniors"?

Edited by ginerjm
Link to comment
Share on other sites

I have made this code in such a way that if any username is already present in the database then it will show error but this code is not working but added to the database..You are better than me and have big Experience  That's why I call you Senior

Link to comment
Share on other sites

I prefer not to be called Senior.

 

You are saying that the username is ON the database but this query is not telling you it was found?  How about doing an echo of the $Username variable and the rowcount value so we can see what you were looking for?

 $stmt->execute();
 $Result = $stmt->rowCount();
 echo "Looked for $Username and returned # of rows $Result";

 

Link to comment
Share on other sites

just posting a snippet of your code doesn't help, because it doesn't show how you are using that code. for all we know, you aren't actually calling that function, aren't calling it with any input value, aren't using the return value at all, or could be assigning a value in a comparison, rather than making a comparison.

when you have code that doesn't work, and you cannot determine why, you need to post all the relevant code needed to reproduce the problem.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.