Ehsan_collboy Posted April 18, 2022 Share Posted April 18, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314711-want-some-more-help-from-seniors/ Share on other sites More sharing options...
Barand Posted April 18, 2022 Share Posted April 18, 2022 (edited) 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 April 18, 2022 by Barand typo Quote Link to comment https://forums.phpfreaks.com/topic/314711-want-some-more-help-from-seniors/#findComment-1595461 Share on other sites More sharing options...
ginerjm Posted April 18, 2022 Share Posted April 18, 2022 (edited) 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 April 18, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314711-want-some-more-help-from-seniors/#findComment-1595464 Share on other sites More sharing options...
Ehsan_collboy Posted April 18, 2022 Author Share Posted April 18, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314711-want-some-more-help-from-seniors/#findComment-1595480 Share on other sites More sharing options...
ginerjm Posted April 18, 2022 Share Posted April 18, 2022 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"; Quote Link to comment https://forums.phpfreaks.com/topic/314711-want-some-more-help-from-seniors/#findComment-1595516 Share on other sites More sharing options...
mac_gyver Posted April 18, 2022 Share Posted April 18, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314711-want-some-more-help-from-seniors/#findComment-1595517 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.