Question

Hi I am performing an SQL query (that only returns one row) and I am printing a particular column on screen just for test purposes. The problem I am having is, the column that I am printing contains a string of 12 characters (abcdef123456), yet sometimes the printed variable is empty. So when I do var_dump($test);

sometimes I get:

string(12) "abcdef123456"

but some other times I get

string(12) ""

I dont understand why it knows there are 12 characters yet its still empty, and sometimes it says 12 characters yet its full. Due to this, I cant perform other functions as they rely on the string.

EDIT: here is the query

$query="SELECT * FROM members WHERE name='$member'";
$sqlResult=mysql_query($query);
$row = mysql_fetch_assoc($sqlResult);
$test = $row["membercode"];
var_dump($test);
Was it helpful?

Solution 2

You most-likely have html tags in your string that are being rendered by the browser.

$foo = "<p><img><div><table>";
var_dump($foo); // string(20) ""

OTHER TIPS

Where do you see that it says string(12) ""? Remember to ALWAYS look in the raw output; the source code. Not how the browser renders it.

For instance, if the string is <span></span>, that'll show up as nothing.

  1. Try adding header('Content-Type: text/plain') to render your page as text instead of HTML. Or use "View Source" to view the HTML source instead of the rendered page. There may be hidden HTML tags in the string.

  2. Try var_dump(bin2hex($test)). There may be invisible control characters such as "\0" that you can't see. For example:

    $ php <<< '<?php var_dump("\0\0\0\0"); ?>'
    string(4) ""
    $ php <<< '<?php var_dump(bin2hex("\0\0\0\0")); ?>'
    string(8) "00000000"
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top