views:

209

answers:

3

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.

+4  A: 

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.

middaparka
unfortunately that does not print out the data at all... nothing gets output.
John Baker
Perhaps because there's no data in there? Can you check what the same SELECT query returns via the MySQL shell client or phpMyAdmin?
middaparka
It's most definitely returning data. There is data in that field for certain, and it will let me handle it if I access each individual letter.
John Baker
Where is the data? I can't see it in your print_r dump. The column is definitely empty. Can you post more complete information?
Open Source
print_r dump was Array ( [index] => 1 [name] => php include [language] => php [text] => ) I know it looks like text has nothing inside of it, however it's not true. I'm trying to install a different version of php and see if it's some strange bug. We'll see
John Baker
Tis a deeply odd one - there's not some weird MySQL client/server version mismatch?
middaparka
I don't know, I'm pretty spent for today, I'm going to take up the problem again tomorrow and update the question with whatever progress I made. Thanks for the help
John Baker
+1  A: 

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.

Chris Gutierrez
A: 

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.

John Baker