Jump to content

mysql fetch array in function

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Hi all,i'm wondering how to use mysql_fetch_array inside function..Suppose i create a function which i'm intending to get the values in a row.I got no problem without using function..Here is my code :


<?php

$member=$_SESSION['id'];

$sql="Select email,nama,alamat,kota,propinsi from users where member_id='$member'";

$exec=mysql_query($sql);

$result=mysql_fetch_array($exec);

?>

<form id="edit" name="edit" method="post" action="">

<label for="email">Email</label>

<input type="text" name="email" id="email" value="<?php echo $result['email'];?>"/>

<div class="spacer"></div>

</form>


Is it possible to use a function to fetch array?Thanks guys...

#2
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
function return($sql) {
        $exec = mysql_query($sql);
        $result = mysql_fetch_array($exec);
        while($row = mysql_fetch_array($exec) {
            echo $result['nama'];
            echo $result['email'];
        }
        return true;
}

$query = "SELECT `email`, `nama`, `almat`, `kota`, `propinsi` FROM `users` WHERE member_id = '$member'";

echo return($query);


NOTE: This function will only work if you are echoing the right results in the PHP function. Let's say you might of wanted to execute the sql query that return the results email and almat. But in this function it returns nama and email. You could change this with a few modifications.

This should work :)
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#3
mikelbring

mikelbring

    Programmer

  • Members
  • PipPipPipPip
  • 118 posts
I am not sure if printing a string from the function and then returning true is such a good idea. Id have to maybe return a array with the information and then print it.

Realize the Web Web services and design.


#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
and can you really create a function named return? return is a reserved word, I believe?

#5
mikelbring

mikelbring

    Programmer

  • Members
  • PipPipPipPip
  • 118 posts
I didnt even think about that, probably not. To lazy to go test it.

Realize the Web Web services and design.


#6
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
OK well if return is a reserved work, you can use whatever you want, that isn't a reserved word ;)
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#7
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Thanks a lot guys..I'll try it out..Anyway,where can i find the tools to mark threads as solved??

#8
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Not really, there is no need. Because people will still come here asking a similar question and they could get the answer. And if yourself need further help, we can still help you :)
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#9
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Ok brandon..Thanks a lot..May God bless you..

#10
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Your welcome mate, just helping a fellow member :)
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!