Matthew Stopa
Matthew Stopa

Reputation: 3865

PHP: How can I turn a mysql TEXT or BLOB type into a string?

I have a frustrating experience in PHP because I keep getting my MEDIUMTEXT field returned as a non-string string. When I check the variable's type it says it's a string, but it's actually an array, basically a character array so I do:

while ($row = mysql_fetch_assoc($records)) {
  print_r($row["myText"]);
}

Which instead of printing out the actual text stored there like "here is a line", it just prints 1. This is confusing because I ask it what it's datatype it and it says string... However if I do

$row["myText"][0]

It will return "h". I'm totally lost on how I can change this value into a string, I mean I would assume php has some function for it, but I can't find it. I also have no idea why it says it's not an array.

Any ideas?

edit by request

mysql query

select * from snippets

a print_r: text is the name of the MEDIUMTEXT column in mysql. The others aren't related

Array ( [index] => 1 [name] => a name [language] => english [text] => ) 1

edit2:

All the code that produced this error:

$username = "root";
$password = "";
$hostname = "localhost"; 
$dbname = "TestSnippets";

$dbh = mysql_connect($hostname, $username, $password) 
$selected = mysql_select_db("snippets",$dbh); 

$records = mysql_query("select * from snippets");

while ($row = mysql_fetch_assoc($records)) {
   echo print_r($row);
}

This produces the error every time. All other fields come in perfectly fine. Not the MEDIUMTEXT field however.

edit3:

I tried installing a new webserver (Apache) and a new version of PHP, but it still has the same issue. Interestingly enough when I call php-cgi.exe it returns the data with that variable being output properly. I have zero clue what it could be, the only other this is I could install a different version of mysql. I'll try that tomorrow and report back.

edit4: I solved this myself keeping in mind what I was told here. See my answer to my own question below.

Upvotes: 1

Views: 9036

Answers (3)

Matthew Stopa
Matthew Stopa

Reputation: 3865

Wow okay so I solved the problem, the problem was that I was incredibly dumb.

I hadn't noticed in the my mysql queries that my test string was actually

<? myString" ?>

It didn't even dawn on me that php would end up parsing out that statement rather than outputting it. That's why it showed up when I ran php-cgi.exe but not in the browser itself, since the display is a little different.

Wow, just mind blowing. Still, thanks everyone for your help, without you telling me the answer I'd have gone down one wrong path after another.

Mystery solved.

Upvotes: 0

John Parker
John Parker

Reputation: 54425

You should simply use...

while ($row = mysql_fetch_assoc($records)) {
  echo $row["text"];
}

...as long as 'text' is the appropriate field in the table you're using. The fact that you're using a medium text is irrelevant as far as PHP (a language that isn't typed) is concerned.

If this doesn't work, then it sounds like the problems are elsewhere.

Upvotes: 3

Chris Gutierrez
Chris Gutierrez

Reputation: 4755

In php you can access strings like arrays. So you can get characters from a string by their index.

so when you call $row["myText"][0] on a value that is "here is the line". You get the first the character at index 0 which is "h".

if you call $row["myText"][6] you should get "i" for the beginning of is.

Upvotes: 2

Related Questions