I am trying to find a number in a string from my database to replace it. Here is my funciton:
function findUserID($friendsString, $userToFind){
echo $friendsString . " " . $userToFind;
$pos = strpos($friendsString,$userToFind);
if($pos){
return true;
}
return false;
}
it returns "25,29 25". How can it not find 25 in that string?
Because string positions start at 0, not 1. Read the warning and solutions here: http://www.php.net/manual/en/function.strpos.php
I understand that it starts at 0. it should still work. i had it working before. i must've changed something.
here is my code for passing to the function:
$query = mysql_query("SELECT friends, pendingFriendRequest
FROM members
WHERE username = '$userID'");
if (mysql_affected_rows() > 0) {
$row = mysql_fetch_array($query) or die(mysql_error());
$friends = $row['friends'];
$pendingFriends = $row['pendingFriendRequest'];
}
// If the user exists in the waiting list, then
//continue (we must confirm that they are first by checking the DB
$test = findUserID($pendingFriends, "25");
if($test){
// If they are, then continue to add them
$test = confirmFriend($pendingFriends, "25");
if($test) {
echo "<br />You have confirmed " . findUserName(24) . "as a friend.";
} else {
echo "<br />Error: Please try again, or contact us.";
}
} else {
echo "<br />Error: You either have this person as a friend already, or there was an internal error.";
}
>I understand that it starts at 0. it should still work.
No, it will not work.
>$test = findUserID($pendingFriends, "25");
If the position of the found string is 0, then $test equals 0
>if($test){
0 evaluates to boolean false, so this test fails, making it appear that the string was not found. Try
if($test === false)
>Oh, so if it finds it or not, it automatically returns the value?
Let me try to explain again. The function strpos() returns two types of values. It returns an integer value if the string is found. It returns the boolean value FALSE if the string is not found. It never returns the boolean value TRUE. If the string is found in position zero, it returns the integer value zero. Zero is not a boolean value, but in php and many other languages, zero is evaluated as boolean FALSE.
>i can't overide it with "return true/false"?
I'm not sure what you mean. You have to treat the returned value in such as way that integer zero will not be evaluated as FALSE. Use the "===" operator for this. Or in the case of replacing the code you have, use the "!==" operator
if($test !== false)
If strpos() did not find the string, $test will equal false and the expression above will evaluate false. If it returns any integer, the expression will evaluate as true.
You're welcome. It appears to me that the reason you've run into this problem stems from the fact that you are storing multiple values in a single column.
>i have two columns in my db: pending and friends.
This creates all sorts of problems and violates the most basic normalization rules. Instead, think about moving that relation into another table.
North America
Europe, Middle East and Africa
Asia Pacific