Jump to content

Function help


garethhall

Recommended Posts

Hello I am wanting to reduce my amount of scripting by using some functions so say I create a page called functions.php ans I put the following in there


<?php
function theMem($id){
$sql = "SELECT * FROM members WHERE memID = '$id'";
$rs = mysql_query($sql);
$rw = mysql_fetch_assoc($rs);
}
?>

 

Now I create the profile.php page and lets say I want to get memName, memEmail, memPhone out of the DB How would I get those values?

Link to comment
Share on other sites

Maybe I was not to clear on what I need sure I can go echo $rw['memName'] in the function but my table has 30 fields let say I need only one value say memName the that's great but on another page I need 3 values say memDate, memStatus, memGender. Then this present a problem. I also don't want to pass the function the field I want as that prevents me from using certain fields as variables.  Does that make sense?

Link to comment
Share on other sites

If I understand you correctly what you're trying to do is select mutliple fields in your table ?!

 

The correct way to go over this would be altering your querystring to this : ( example ofc )

 

$sql = "SELECT members.memName, members.memDate, members.memStatus, members.memGender FROM members WHERE memID = '$id'";

 

All the info you could need about the SELECT syntax can be found in these docs : http://dev.mysql.com/doc/refman/6.0/en/select.html

 

I hope this gets you on your way  :shy:

Link to comment
Share on other sites

Sorry but still that is not it, I am already doing a select All if you look at my sql. Ok I am going to try and explain again.

I have a website with many pages. Most of the pages call data from the same table but different fields. So let say we have 3 pages

1 accountOverview.php

2 profile.php

3 publicProfile.php

 

so lets look at accountOverview.php so here I would like data like firstName, lastName, status.

 

Now for profile.php  I need firstName, lastName, emailAddress, dateOfBrith, address

 

and lastly publicProfile.php we need occupation, interests, blogAddress, webAddress ans so.

 

so typically I would write sql on everyone of those pages  that would look some like this

<?php
$sql = "select * from members where memID = $id";
$rs = mysql_query($sql, $client)
$rw = mysql_fetch_assoc($rs);
// Lastly we would echo the value we need IE
echo $rw['firstName'];
?>

Now this works fine but here is the issue I have to do this on a lot of pages not just the 3 mention above so I want to make my code more useable across the board since I and pulling data from the same table all the time. So I want to use a function or a class to do the work and call the same function over and over for as many time I need it with out having to write sql then query then fetch and so on.  SO can I have a function help me out here? Hmmmmm I am not sure but go some like $firstName = $this->$memFirstName from the function?

Link to comment
Share on other sites

Just put your code in a seperate php file then called functions.php and in the beginning of each file just do a require_once(functions.php) call.

 

You can just use the variables from within that php file then like you would do within functions.php.

 

small example :

 

<?php
// functions.php

$sql = "select * from members where memID = $id";

$rs = mysql_query($sql, $client)
$rw = mysql_fetch_assoc($rs);

?>

 

<?php
// any page where you need something ...

require_once(functions.php);

echo $rw['firstName'];
echo $rw['lastName'];
// ... and so on ...

?>

 

Is this what you're trying to do ?

Link to comment
Share on other sites

1) Your function needs to be available to the code that wants to use it.  You do this by using require_once() on the file.

<?php
// this is accounts.php
require_once( 'functions.php' );

$mem = theMem( 1 );
print_r( $mem );

2) Your function needs to return a value.
[code]function theMem($id){
$sql = "SELECT * FROM members WHERE memID = '$id'";
$rs = mysql_query($sql);
$rw = mysql_fetch_assoc($rs);
        return $rw;
}

 

 

Also, if you use mysql_fetch_object() instead of mysql_fetch_assoc, you'll have less typing to do.

$r = mysql_fetch_assoc();
echo $r['id'];

$r = mysql_fetch_object();
echo $r->id; <-- easier to type IMO

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.