简体   繁体   English

使用PHP从MySQL将文本字段作为字符串获取问题

[英]Problem getting text field as string from MySQL with PHP

I'm having this problem that's driving me nuts. 我遇到这个问题令我发疯。

I've got a MySQL database in which there is a table that contains a text field. 我有一个MySQL数据库,其中有一个包含文本字段的表。 I am querying the table in PHP and trying to put the content of the text field for each row into a var. 我正在用PHP查询表格,并尝试将每一行的文本字段的内容放入var中。

I am doing something like this: 我正在做这样的事情:

for ($i=0;$i<$nbrows;$i++){
  $id = $data[$i]['ID'];
  $description = $data[$i]['DESCRIPTION'];
  $mystring .= '<div>'.$id.': '.$description.'</div>';
}

DESCRIPTION is my text field. Description是我的文本字段。

I'll pass on the details. 我将详细介绍。 The $data array is built from mysql_fetch_array($result). $ data数组是从mysql_fetch_array($ result)构建的。 I also tried using objects instead, as I use mysql_fetch_object for all my other routines, but there is no change. 我还尝试使用对象,因为我将mysql_fetch_object用于所有其他例程,但没有任何变化。

Anyway, the problem is this: if I do "echo $description;" 无论如何,问题是这样的:如果我执行“ echo $ description”; then it works. 然后就可以了。 I am getting my text field data as expected. 我正在按预期方式获取文本字段数据。 The problem is that I don't want to output it directly, but add it to a concatenated string, and that is not working. 问题是我不想直接输出它,而是将其添加到串联的字符串中,这是行不通的。 What happens in that case is it seems to be taking $description for some kind of array or object. 在这种情况下发生的事情似乎是在为某种数组或对象使用$ description。 To make the above example work, I have the replace the string with: 为了使上面的示例起作用,我将字符串替换为:

$mystring .= '<div>'.$id.': '.$description[0].'</div>';

So in the concatenated string code, if I treat $description as an array, it works, but obviously I am getting only one letter. 因此,在串联的字符串代码中,如果我将$ description视为一个数组,它可以工作,但是显然我只收到一个字母。 (it doesn't actually seem to be an array because I can't implode it). (它实际上似乎不是数组,因为我无法将其内爆)。

I tried a million things but I just can't make this work unless I use echo, but that is not what I am trying to do. 我尝试了上百万次,但是除非使用echo,否则我将无法完成这项工作,但这不是我要尝试的工作。

There is no issue with fields that aren't text. 非文本字段没有问题。

Thanks for any ideas! 感谢您的任何想法!

There is nothing visually wrong with the code you pasted, maybe if you could also add the fetching function as well, we might be able to help you further. 您粘贴的代码在视觉上没有任何问题,也许如果您还可以添加获取功能,我们也许可以为您提供进一步的帮助。

Maybe you could post a var_dump of your $data array? 也许您可以发布$data数组的var_dump

您是否尝试过$mystring .= "<div> $id : $description </div>";

Ack, well, you know, hours spent on this and then it becomes obvious after I decide to post for help. 阿克,好吧,您知道,花了几个小时,然后在我决定发帖寻求帮助后,这一点就显而易见了。 This is just because of text encoding/escaping and nothing else. 这仅仅是因为文本编码/转义,而没有其他。 I just didn't see well enough where the problem was actually happening. 我只是对问题实际发生的地方不太了解。

Thanks for taking the time to read and respond! 感谢您抽出宝贵的时间阅读和回复!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM