从mysql json数据中剪切文本

时间:2015-02-10 12:28:27

标签: php mysql json

我有一些列idnameplacewish

如何剪切希望

中的文字
 <?php            
    //connect to database
    include 'db.php';

    $link = mysql_connect("localhost",$db_user,$db_pass);
    $db = mysql_select_db($db_name, $link);
    mysql_query("set names utf8");

    $sth = mysql_query("SELECT * FROM wishes");
    $rows = array();
    while($r = mysql_fetch_assoc($sth)) {
        $rows[] = $r;
    }
    print json_encode($rows); ?>

1 个答案:

答案 0 :(得分:0)

我的解决方案,工作正常。

<?php

//connect to database
include 'db.php';

$link = mysql_connect("localhost",$db_user,$db_pass);
$db = mysql_select_db($db_name, $link);
mysql_query("set names utf8");

$sth = mysql_query("SELECT id, name, place, IF(SUBSTR(wish, 1, 120)=wish, wish, CONCAT(SUBSTR(wish, 1, 120), '...')) wish FROM wishes");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $rows[] = $r;

}

print json_encode($rows);
?>