# Returning array from function - my own sql wrapper

**URL:** <https://forum.kirupa.com/t/returning-array-from-function-my-own-sql-wrapper/186462>\
**Category:** programming\
**Created:** [April 21, 2006, 11:58am UTC](https://forum.kirupa.com/t/returning-array-from-function-my-own-sql-wrapper/186462 "2006-04-21T11:58:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zaid\_W1red](https://avatars.discourse-cdn.com/v4/letter/z/cab0a1/32.png) [@Zaid\_W1red](https://forum.kirupa.com/u/Zaid_W1red)\
**Post date:** [April 21, 2006, 11:58am UTC](https://forum.kirupa.com/t/returning-array-from-function-my-own-sql-wrapper/186462/1 "2006-04-21T11:58:26Z")

</div>

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:

```php

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?

---

<div class="post-metadata">

**Author:** ![skOOb](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@skOOb](https://forum.kirupa.com/u/skOOb)\
**Post date:** [April 21, 2006, 2:50pm UTC](https://forum.kirupa.com/t/returning-array-from-function-my-own-sql-wrapper/186462/2 "2006-04-21T14:50:22Z")

</div>

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

```php
$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.

```php
$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. 🍺

---

<div class="post-metadata">

**Author:** ![955](https://avatars.discourse-cdn.com/v4/letter/9/4bbf92/32.png) [@955](https://forum.kirupa.com/u/955)\
**Post date:** [April 21, 2006, 3:36pm UTC](https://forum.kirupa.com/t/returning-array-from-function-my-own-sql-wrapper/186462/3 "2006-04-21T15:36:39Z")

</div>

> [@skOOb](#):
>
> well, if you have a LIMIT that is over 1, then your
> 
> ```php
> $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.
> 
> ```php
> $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. 🍺

You could do this instead:

```php

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

```
