Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Is there are reason you are not doing this with just an UPDATE query? Something like the following:
UPDATE 'table' SET 'column' = REPLACE('column', '25' 'replacementdata')
Copy link to clipboard
Copied
thanks for this, but first i have to check if the user exists first in the field.
Copy link to clipboard
Copied
So it's not just replacing everywhere it encounters 25? At any rate, even if I don't quite understand what you are trying to do, it still seems like the best way to do this is all in MySQL, not PHP.
Copy link to clipboard
Copied
what I am trying to do is confirm a friend. i have two columns in my db: pending and friends. i have to move the pending id of the friend to confirm to friends. first, i wanted to check if a user ID is actually in pending. then, remove it from the string, and add that ID to the friends.
Copy link to clipboard
Copied
Because string positions start at 0, not 1. Read the warning and solutions here: http://www.php.net/manual/en/function.strpos.php
Copy link to clipboard
Copied
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.";
}
Copy link to clipboard
Copied
>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)
Copy link to clipboard
Copied
ok. but i was returning values "true" or "false", right? and 0 evaluates to false? or does it just take it as the number 0?
Copy link to clipboard
Copied
>but i was returning values "true" or "false", right?
No. strpos() returns the position of the string if found, and FALSE if not found. If the string is found in position 0, then 0 is returned. Integer zero is evaluated as false. Hence the warning on the page I referred you to.
Copy link to clipboard
Copied
Oh, so if it finds it or not, it automatically returns the value? i can't overide it with "return true/false"?
Copy link to clipboard
Copied
>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.
Copy link to clipboard
Copied
I see - thanks!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
so maybe a friends table?
Copy link to clipboard
Copied
Yes. It could have columns for the user, friend and a boolean to indicate if friend is confirmed or pending.
Copy link to clipboard
Copied
I was thinking about that. it seems like you would have so many rows. wouldn't it be slower, rather than just searching a string?
Copy link to clipboard
Copied
No, it won't be slower. How many rows are you anticipating? Put an index on the column that will be used in the where clause.