How do I fetch multiple table columns by php query?
I need to fetch two table columns from mysql & then replace part of contents of one column with contents of other. This is how I do but it shows nothing.
$query = "SELECT id, msg FROM msg2_qualities";
$result = mysql_query($query);
$outArray = array();
if ($result) {
while ($row = mysql_fetch_assoc($result))
{
$row2 = str_replace('testWord','$row[0]',$row[1]);
$outArray[] = $row2;开发者_如何转开发
}
}
echo json_encode($outArray);
EDIT I tested the code by echo & $row[0], $row[1] have no value. But If I run query to fetch single column from table, then it works fine like
$query = "SELECT msg FROM msg2_qualities"; OR
$query = "SELECT id FROM msg2_qualities";
Try removing the quotes around $row[0]
, and refer to the fields by name rather than index:
$row2 = str_replace('testWord', $row['id'], $row['msg']);
you are using mysql_fetch_assoc
so code should be like this:-
while ($row = mysql_fetch_assoc($result))
{
$row2 = str_replace('testWord',$row['id'],$row['msg1']);
$outArray[] = $row2;
}
Thanks.
精彩评论