stub function question (simulating data retrieved from a database)

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
PHP noob here (background in C++/C/Java though)

I want to return dummy data from a stub function, this stub function  
should replicate data from a database. Any idea how I can write this  
stub function?

i.e. something like this:

function foo (){


I want this function to return data that looks like the result of a  
database query - i.e. 0 or more rows, with column headers.

PS: The format will be fixed i.e. I know exactly the format of the data  
I am expecting (i.e. number of coulmns and column names)

Re: stub function question (simulating data retrieved from a database)

The structure of the result set depends on the database API used. For
example, mysql_fetch_array returns an associative array with both
numeric and string keys.

Here's an example that works with mysql_fetch_array:


function getDataStub()
  $rows = array();
  $rows[] = array('val1-1', 'val1-2', 'val1-3');
  $rows[] = array('val2-1', 'val2-2', 'val2-3');
  $rows[] = array('val3-1', 'val3-2', 'val3-3');

  $cols = array('col1', 'col2', 'col3');
  $items = array();
  foreach ($rows as $row) {
    $item = array();
    for ($i = 0; $i < count($cols); $i++) {
      $item[$i] = $row[$i];
      $item[$cols[$i]] = $row[$i];
    $items[] = $item;
  return $items;

function getDataMysql()
  $items = array();
  $res = mysql_query("select * from t") or die(mysql_error());
  while ($row = mysql_fetch_array($res)) {
    $items[] = $row;
  return $items;

function getData()
  return isset($GLOBALS['stubsEnabled']) ? getDataStub() :


Bit Byte wrote:
Quoted text here. Click to load it

Site Timeline