Simple PHP code functions failing for no apparent reason

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.

Is there some reason you deleted your post?

If you are updating old code, I recommend that you use the PDO database extension. The mysqli extension was never a very good choice, even if all you are doing is updating mysql_ based code.

Thanks for the response. I deleted the post because it turned out there was a typo with one of the variables.

I will look into using the PDO extension.

While on this subject, however, I have encountered some differences with how php works in the newer versions of php.

In version 5-something of PHP, I could do the following:

$txt="This is a test.";
$thisChar=$txt{3};

This would return the letter “s” from the txt string when in php version 5-somethning.

However, this same code breaks when I try to use it in version 8-something of PHP.

use [ ] square brackets instead of curly { } braces/brackets.

Interesting.

I’ve been working with php 5 for two decades and I’ve always used { } to specify locations inside a string and I’ve always used square brackets [ ] for arrays.

However, the square brackets do seem to work in this case in php 8. I guess the modern versions of php can tell the difference between an array and a non-array without the need for different kinds of brackets.

The only thing I know for sure is that curly brackets used to work to indicate character position in strings (when using php 5) and that this is no longer the case with the newer version of php.

Thanks for thn info!

Sponsor our Newsletter | Privacy Policy | Terms of Service