使用 PHP 从 MySQL 获取文本字段作为字符串的问题 [英] Problem getting text field as string from MySQL with PHP

查看:47
本文介绍了使用 PHP 从 MySQL 获取文本字段作为字符串的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个让我发疯的问题.

我有一个 MySQL 数据库,其中有一个包含文本字段的表.我正在用 PHP 查询表并尝试将每一行的文本字段的内容放入一个 var 中.

我正在做这样的事情:

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

DESCRIPTION 是我的文本字段.

我会传递细节.$data 数组是从 mysql_fetch_array($result) 构建的.我也尝试过使用对象,因为我将 mysql_fetch_object 用于所有其他例程,但没有任何变化.

无论如何,问题是:如果我执行echo $description;"那么它的工作原理.我正在按预期获取我的文本字段数据.问题是我不想直接输出它,而是将它添加到连接的字符串中,这是行不通的.在这种情况下发生的事情似乎是为某种数组或对象获取 $description .为了使上面的示例工作,我将字符串替换为:

$mystring .= '

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

所以在连接的字符串代码中,如果我将 $description 视为一个数组,它可以工作,但显然我只得到一个字母.(它实际上似乎不是一个数组,因为我无法内爆它).

我尝试了一百万件事,但除非我使用 echo,否则我无法完成这项工作,但这不是我想要做的.

非文本字段没有问题.

感谢您的任何想法!

解决方案

Ack,好吧,你知道,花了几个小时在这个上,然后在我决定发帖寻求帮助后,它变得显而易见.这只是因为文本编码/转义,没有别的.我只是没有看清楚问题实际发生在哪里.

感谢您花时间阅读和回复!

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. I am querying the table in PHP and trying to put the content of the text field for each row into a 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.

I'll pass on the details. The $data array is built from 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.

Anyway, the problem is this: if I do "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. 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. (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.

There is no issue with fields that aren't text.

Thanks for any ideas!

解决方案

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!

这篇关于使用 PHP 从 MySQL 获取文本字段作为字符串的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆