0

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"; 
XCeptable
  • 1,247
  • 6
  • 25
  • 49
  • 2
    Your main problem is logic. A lack of logic, to be certain. You are asking how to fetch multiple columns but your real problem is far away from that. To see multiple columns you have to echo them out. Just echo and nothing else. No arrays, no replaces, but just echo. When you echo them out and ensure you have your columns, you'll see that your problem in somewhere else. A string syntax probably – Your Common Sense Nov 30 '10 at 09:05
  • The syntax error being $row[0] in single quotes... – Adam Holmes Nov 30 '10 at 09:06
  • Yes, that is indeed plausible ;) – mingos Nov 30 '10 at 09:08
  • So you are searching for 'testWord' in the msg column and replacing it with the ID? remove the single quotes around $row[0]. But please note that $row2 will contain only the msg column! – ifaour Nov 30 '10 at 09:10
  • @Col. Shrapnel, yes, I should but somehow I missed though I was continuously thinking to echo. If I select two columns as it then it selects no value. Nothing in echo $row[0] & nothing in $row[1]. Though query works fine if I select either one of these columns. – XCeptable Nov 30 '10 at 09:24
  • @ifaour, Problem actually is no value fetched this way, I early tried with removing quotes. You wrote $row2 will contain only msg column, the way I understood is you mean replace wouldn't happen. No, it happens as I checked that before with using a test word. It work then I start work to use a table value to replace with. – XCeptable Nov 30 '10 at 09:31
  • good. now see how to run queries properly. I posted it here: http://stackoverflow.com/questions/2666104/how-to-reslove-mysql-fetch-assoc-problems - using this method you'll be always informed of any error occurred in query execution. And also check number of rows returned, using mysql_num_rows() - there may be no rows in the table. – Your Common Sense Nov 30 '10 at 09:33
  • @ Col. Shrapnel, OK, thank you for the link but my problem here lies somewhere else. i.e php query is not fetching multiple columns from table. I checked each row has value & each row's values are fetched when I select single column but no value in case of two i.e. $row[0] & $row[1] prints nothing but $row is fine for both id & msg if I select them singly. You kindly see if there is some error in my query for multiple because there is problem. – XCeptable Nov 30 '10 at 09:42
  • 1
    @XCeptable: Try referring to the columns by name rather than index, i.e. $row['id'] and $row['msg'] - you are using `mysql_fetch_assoc`. – cspolton Nov 30 '10 at 09:45
  • If you were using `mysql_fetch_row` you would be able to use $row[0] and $row[1], but you are not. – cspolton Nov 30 '10 at 09:48
  • 1
    @Spolto, thank you very much, that was problem ..... – XCeptable Nov 30 '10 at 09:48
  • shit, i missed such a silly issue. However I never have one in my own scripts, because I always code with error_reporting(E_ALL); and always being informed of all missed variables. – Your Common Sense Nov 30 '10 at 10:21

2 Answers2

2

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']);
cspolton
  • 4,495
  • 4
  • 26
  • 34
1

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.

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • your suggestion was very close but was not clear enough though I tried it too. ThanX for the help – XCeptable Nov 30 '10 at 09:50
  • means you don't found any solution yet or is it not working, I just shown what are you doing wrong. I am not going to written whole logic here. Thanks – Shakti Singh Nov 30 '10 at 10:01
  • no, you took it wrong, solution is this one $row['id'] and $row['msg']. I thank to you as your suggestion was very near to solution. – XCeptable Nov 30 '10 at 10:15