Returning array from function - my own sql wrapper

Hey Guys,

I kinda playin around my own SQL wrapper (I know there are plenty out there this is just for my own learning).

Ive made a select function below:


function selectSQL($table, $where,$limit){
$sql = "SELECT * FROM ".$table." WHERE ".$where." LIMIT ".$limit;
$query = mysql_query($sql);
if (!$query){
echo "error<br>";
echo $sql;
exit;
}
$result = mysql_fetch_array($query);
$rows = mysql_num_rows($query);
return $result;
}

I was hoping that by returning $result I would be able to echo out values from the query using $result[‘title’]; etc but it does do anything.

Ive checked the function is working by getting the function to echo the results but his isnt really how i want it to work.

Can anyone suggest what im dooing wrong?

well, if you have a LIMIT that is over 1, then your

$result = mysql_fetch_array($query);

won’t work properly. You will have to loop through all the results adding them to an array and then returning it. e.g.

$i=0;
while($row = mysql_fetch_array($query)) {
  $results[$i] = $row;
  $i++;
}

It should at least store the first instance in $result though.

hth some. :beer:

You could do this instead:


while($row = mysql_fetch_array($query)) {
  $results[] = $row;
}