我有一个php脚本从sql数据库获取数据然后将其添加到数组并在json中对其进行编码然后打印它,但我想在将其添加到数组之前编辑其中一个项目,我该怎么做在将$row['name']
添加到数组之前进行编辑?
很抱歉,这真的令人困惑!
$con = mysql_connect("localhost","username","password");
mysql_select_db("database", $con);
$result = mysql_query("SELECT name,email,phone FROM table;");
$rows = array();
while($row = mysql_fetch_array($result))
{
$rows[] = $row;
}
$jTableResult = array();
$jTableResult['aaData'] = $rows;
print json_encode($jTableResult);
答案 0 :(得分:0)
你可以这样添加:
while($row = mysql_fetch_array($result))
{
if($row['name'])
{
// do here what you want.
}
$rows[] = $row;
}
可能会帮助你
答案 1 :(得分:0)
while($row = mysql_fetch_array($result))
{
$row['name'] = YourFunction($row['name']); //change it however you want
$rows[] = $row;
}
答案 2 :(得分:0)
根据您要修改$row['name']
的方式,您可能会发现MySQL的String functions很有用。
我假设您要在查询中进行此修改。
// Add a string to the end of name
$result = mysql_query("SELECT CONCAT(name, 'string') name, email, phone FROM table");
// Or try replacing a part of the string
$result = mysql_query("SELECT REPLACE(name, 'old', 'new') name, email, phone FROM table");
$rows = array();
while ($row = mysql_fetch_array($result)) {
$rows[] = $row;
}