garethhall Posted August 3, 2009 Share Posted August 3, 2009 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? Quote Link to comment Share on other sites More sharing options...
smerny Posted August 3, 2009 Share Posted August 3, 2009 $rw['memName'] ? Quote Link to comment Share on other sites More sharing options...
garethhall Posted August 3, 2009 Author Share Posted August 3, 2009 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? Quote Link to comment Share on other sites More sharing options...
JonathanV Posted August 3, 2009 Share Posted August 3, 2009 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 Quote Link to comment Share on other sites More sharing options...
garethhall Posted August 3, 2009 Author Share Posted August 3, 2009 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? Quote Link to comment Share on other sites More sharing options...
JonathanV Posted August 3, 2009 Share Posted August 3, 2009 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 ? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 3, 2009 Share Posted August 3, 2009 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 Quote Link to comment Share on other sites More sharing options...
JonathanV Posted August 3, 2009 Share Posted August 3, 2009 There, there, the same solution twice, you're a lucky man garethhall! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.