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);
4
  • Plz, show the code. And do you check, whether the database connection was successful? Maybe, that's the cause? Commented Nov 27, 2012 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. Commented Nov 27, 2012 at 22:18
  • I have added the code containing the query Commented Nov 27, 2012 at 22:26
  • Please also add the hex-dump of the string(s) in question. Commented Nov 28, 2012 at 12:36

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The result from the query is "abcdef123456". So there is no tags. Also, sometimes it shows the string and sometimes it doesnt
In that case, we'll need to see the code producing your output.
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) ""

1 Comment

The result from the query is "abcdef123456". So there is no tags. Also, sometimes it shows the string and sometimes it doesnt
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"
    

4 Comments

I tried bin2hex but it just returns all 0's sometimes, and sometimes it prints the correct string but in hex
@Matt9Atkins All 0's means you have a string with null characters in it, which is exactly what's tripping you up, no?
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
@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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.