Please ignore the following. It was a false alarm based on a type.
I have recently gone from using php version 5-something to php version 8-something (thanks to a change in hosting platforms), and I am running into some unusual problems.
Every now and then, my php code fails to work as expected.
In today’s problem, simple php functions like strpos and substr_count do not work when used on data that I have retrieved from a database – and yet if I bypass the database and hardcode the data into my php code, then the php code will work fine.
The following statements work fine:
$searchTerm="sample";
$txt="This is sample text";
$pos=strpos($txt,$searchTerm);
$pos returns the appropriate value in the above case.
HOWEVER…
If I get the exact same $txt value from a database, the php code fails:
$sql = "SELECT * FROM dbase";
$result = mysqli_query($link, $sql);
while($row = mysqli_fetch_assoc($result)) {
$txt=$row['txt'];
}
$searchTerm="sample";
$pos=strpos($txt,$searchTerm);
In this case, the $pos value comes back as blank, or null, even though I have triple-checked that the proper $txt value has been returned by Mysql. (I can even print out and echo the txt field returned by Mysql – but, again, I cannot manipulate it with the usual php commands. Only when I hardcode the data for the $txt string will the php operations work.)
FURTHER NOTES:
The above code also fails if I try to use substr_count with the strings. Again, if I hardcode the txt field, the php operation will work, but if I use the $txt data supplied from my database, the php operation will fail and return the null set. This is strange because the data supplied from the database for $txt is visually identical to the data that I use when I hardcode $txt by explicitly setting it equal to “This is sample text.”
Any ideas would be appreciated. It’s as if there is some further operation to which I need to subject the Mysql $txt variable prior to manipulating it with php.