开发者

How do I fetch multiple table columns by php query?

开发者 https://www.devze.com 2023-01-27 19:12 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号