MySQL fectch array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cheatasp
Forum Newbie
Posts: 3
Joined: Sun Mar 22, 2009 10:22 pm
Location: Phnom Penh

MySQL fectch array

Post by cheatasp »

Hi All,

I would like to create a function that can return mysql_fetch_array($result)

here my code:

Code: Select all

 
function FetchRecord($sql) {
    $result = mysql_query($sql);
    return mysql_fetch_array($result);
}
 
after I call function as below:

Code: Select all

 
$sql="SELECT * FROM tbl_banner_page";
while ($row = FetchRecord($sql)) {
    echo $row[1];
}
 
it does not work
Can anyone help me?
Last edited by cheatasp on Sun Mar 22, 2009 10:43 pm, edited 3 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: MySQL fectch array

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
cheatasp
Forum Newbie
Posts: 3
Joined: Sun Mar 22, 2009 10:22 pm
Location: Phnom Penh

Re: MySQL fectch array

Post by cheatasp »

Hi, I have changed already, you can reply my question or not?


Thanks
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: MySQL fectch array

Post by Benjamin »

Yeah, that won't work because you are executing the query in a continous loop.
cheatasp
Forum Newbie
Posts: 3
Joined: Sun Mar 22, 2009 10:22 pm
Location: Phnom Penh

Re: MySQL fectch array

Post by cheatasp »

So any solution? Because I don't want to write that again and again


Thanks
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: MySQL fectch array

Post by Benjamin »

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: MySQL fectch array

Post by John Cartwright »

It's not very intuitive to create a query class with simple functions since things tend to become rather complex quickly. However, for the sake of a simple query function, you could do something along the lines of:

Code: Select all

 
function query($sql) {
   $result = mysql_query($sql) or die(mysql_error());
   
   $return = array();
   if (mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_assoc($sql)) {
         $return[] = $row;
      }
   }
 
   return $return;
}
 
$sql="SELECT * FROM tbl_banner_page";
$rows = query($sql);
 
 
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: MySQL fectch array

Post by php_east »

here is one other guy who hates to write it over and over again.
i used his class and modified it to my purpose, added on several nice extra features
and have found it a happy expericence.

# Name: database.class.php
# File Description: MySQL Class to allow easy and clean access to common mysql commands
# Author: ricocheting
# Web: http://www.ricocheting.com/scripts/
# Update: 2/2/2009
# Version: 2.1
# Copyright 2003 ricocheting.com
Post Reply