4

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);
M9A
  • 3,168
  • 14
  • 51
  • 79
  • Plz, show the code. And do you check, whether the database connection was successful? Maybe, that's the cause? – user4035 Nov 27 '12 at 22:10
  • What does `var_dump($test, bin2hex($test));` give? I ask because there are characters that are not displayed. `bin2hex` will show a hexdump of the string which makes everything visible. – hakre Nov 27 '12 at 22:18
  • I have added the code containing the query – M9A Nov 27 '12 at 22:26
  • Please also add the hex-dump of the string(s) in question. – hakre Nov 28 '12 at 12:36

3 Answers3

2

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.

kba
  • 19,333
  • 5
  • 62
  • 89
  • The result from the query is "abcdef123456". So there is no tags. Also, sometimes it shows the string and sometimes it doesnt – M9A Nov 27 '12 at 22:14
  • In that case, we'll need to see the code producing your output. – kba Nov 27 '12 at 22:19
1

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) ""
Mike B
  • 31,886
  • 13
  • 87
  • 111
  • The result from the query is "abcdef123456". So there is no tags. Also, sometimes it shows the string and sometimes it doesnt – M9A Nov 27 '12 at 22:15
1
  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"
    
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I tried bin2hex but it just returns all 0's sometimes, and sometimes it prints the correct string but in hex – M9A Nov 27 '12 at 22:16
  • @Matt9Atkins All 0's means you have a string with null characters in it, which is exactly what's tripping you up, no? – John Kugelman Nov 27 '12 at 22:18
  • i understand that but why does it sometimes return a string of the correct characters? surely if it works once then it should always work. Yet sometimes I get a string of all 0's – M9A Nov 27 '12 at 22:22
  • 1
    @Matt9Atkins Point being, sometimes your SQL query returns "abcdef123456" and sometimes it returns "\0\0\0\0\0\0\0\0\0\0\0\0". – John Kugelman Nov 27 '12 at 22:31