douzhui8531 2012-05-07 23:16
浏览 27

在Joomla DB中对introtext的MySQL查询有时会返回NULL吗?

I've got a PHP script that searches my Joomla DB for the introtext field in #_content and returns the results as a json_encoded string.

Not sure why, but some of the introtext feilds are NULL in the JSON string (e.g. {"introtext":null},{"introtext":null} ) when they clearly aren't/should not be. Some return the text just fine.

Here's my script . . .

<?php
    /* Connect to database */
    $DB_HostName = "**";
    $DB_Name = "**";
    $DB_User = "**";

    @mysql_connect($DB_HostName,$DB_User) or die ('Could not connect');
    @mysql_select_db($DB_Name) or die ('Could not find database');

    $result = mysql_query ("SELECT introtext FROM **_content") or die (mysql_error());

    $records = array();
    while ($row = mysql_fetch_assoc($result)) {
        $records[] = $row;
    }

    echo json_encode($records, JSON_HEX_TAG);

    mysql_close();
?>

The DB introtext field contains HTML tagged text. The query I've written above succeeds just fine when entered directly in the phpMyAdmin tool for the DB. The introtext field includes some blank entries (e.g. <p></p>) but the vast majority have HTML tagged text included. The query returns results for all introtext fields in the *_content table.

UPDATE

I've discovered my issue is with the json_encode function. This function only works with UTF-8 encoded data, and not everything in my introtext is correctly encoded in this format. If not properly encoded, it will return NULL. The solution is to run my array through a function in my PHP script that will encode things properly before returning the json_encoded data.

See this link for some options in the comments: http://www.php.net/manual/en/function.json-encode.php

Now I just need to strip all the HTML tags (working with strip_tags to try and get this done) so I can have just the article text to work with. Any suggestions on how best to do this is appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?