Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging date format in mysql_fetch returned array
    primarykey
    data
    text
    <p>I have a query like this</p> <pre><code>select fname,joined_date from employees where id=1 </code></pre> <p>currently the date format I'm using to display this returned employee details is Y-m-d. But now I need to convert the default mysql format Y-m-d to d/m/Y(in all date information display fields).</p> <p>For me it's <strong>very very difficult</strong> to go through all the files to do the date format conversion. </p> <p>So I thought of doing some thing like this in my database class.I have a function like this in my database class</p> <pre><code>function fetch($res){ $row = mysql_fetch_assoc($res); foreach($row as $key=&gt;$value){ if(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/',$value)) $row[$key] = date('d/m/Y',strtotime($value)); } return $row; }//end function </code></pre> <p>and i'm using this function like this</p> <pre><code>$row = $db-&gt;fetch($res); </code></pre> <p>or</p> <pre><code>while($row = $db-&gt;fetch($res)){...} </code></pre> <p>I'm getting the expected output,but with an error message</p> <blockquote> <p>invalid argument for foreach</p> </blockquote> <p>it looks like the fetch function code executed (total_num_rows + 1) times</p> <p>If I use for loop instead of foreach, getting undefined offset error</p> <p>currently I'm using some thing like this to escape</p> <pre><code>if(is_array($row)){...} </code></pre> <p>when i look for the type $res, it showed resource(1st iteration),resource(2nd)</p> <p>for $row array</p> <p>array(1st iteration),boolean(2nd)</p> <p>Can anybody tell me why it's happening? Or is there a better way to do this?</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload