Page 1 of 1

MySQL fectch array

Posted: Sun Mar 22, 2009 10:26 pm
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?

Re: MySQL fectch array

Posted: Sun Mar 22, 2009 10:31 pm
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]

Re: MySQL fectch array

Posted: Sun Mar 22, 2009 10:45 pm
by cheatasp
Hi, I have changed already, you can reply my question or not?


Thanks

Re: MySQL fectch array

Posted: Sun Mar 22, 2009 10:48 pm
by Benjamin
Yeah, that won't work because you are executing the query in a continous loop.

Re: MySQL fectch array

Posted: Sun Mar 22, 2009 11:05 pm
by cheatasp
So any solution? Because I don't want to write that again and again


Thanks

Re: MySQL fectch array

Posted: Sun Mar 22, 2009 11:06 pm
by Benjamin

Re: MySQL fectch array

Posted: Sun Mar 22, 2009 11:19 pm
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);
 
 

Re: MySQL fectch array

Posted: Mon Mar 23, 2009 6:23 am
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