Jump to content

[SOLVED] create a function for SELECTing fields


tgavin

Recommended Posts

I'm trying to create a function that will easily let me select user fields. Hopefully you can see what I'm trying to accomplish with the code.

 

<?php
// get_sub('field names,serarated by, commas','subscriber's id');
function get_sub($value,$id) {

global $row_subs;

// separate the entered values
$field = explode(',',$value);
$fields = join(',', $field);

select_db();
$sql = mysql_query("SELECT $fields FROM subscribers WHERE id = $id") or die(mysql_error());

// pretty sure this is the problem...
while($row = mysql_fetch_assoc($sql)) {
	$row_subs[$fields] = $row[$fields];
}
}

get_sub('fname,lname',$id);
$fname = $row_subs['fname'];
$lname = $row_subs['lname'];
?>

 

 

Link to comment
Share on other sites

Well, why would you explode the values with a comma and then concatenate them with a comma? Besides you are using the wong command - you shoudl have used implode() not join(). In any case you are taking an unnecessary step.

 

Also your while loop is unnecessary. All you are doing is creating a duplicate array.

 

Also I always find it advisable to create the query as a string so you can print it to the page if there is a problem:

 

<?php
// get_sub('field names,serarated by, commas','subscriber's id');
function get_sub($fields,$id) {

select_db();
$query = "SELECT $fields FROM subscribers WHERE id = $id";
$result = mysql_query($query) or die(mysql_error()."<br>Query: $query");

return $result;
}

$user_values = get_sub('fname,lname',$id);
$fname = $user_values['fname'];
$lname = $user_values['lname'];
?>

Link to comment
Share on other sites

Well, why would you explode the values with a comma and then concatenate them with a comma? Besides you are using the wong command - you shoudl have used implode() not join(). In any case you are taking an unnecessary step.

 

Also your while loop is unnecessary. All you are doing is creating a duplicate array.

 

Also I always find it advisable to create the query as a string so you can print it to the page if there is a problem:

 

<?php
// get_sub('field names,serarated by, commas','subscriber's id');
function get_sub($fields,$id) {

select_db();
$query = "SELECT $fields FROM subscribers WHERE id = $id";
$result = mysql_query($query) or die(mysql_error()."<br>Query: $query");

return $result;
}

$user_values = get_sub('fname,lname',$id);
$fname = $user_values['fname'];
$lname = $user_values['lname'];
?>

Thanks for your reply. That's not exactly what I'm looking for. I want a solution so that anywhere in the application, if I want the user's first name, all I will need to do is call the function at the top of the script, then echo $row_sub['fname']; whenever I want the first name. What you provided is close, but adds an extra step that I'd like to bypass.

 

<?php
// top of script
get_sub('fname,lname',$id);
// do stuff
First name: $row_subs['fname'];
Last name: $row_subs['lname'];
// and so on
?>

Link to comment
Share on other sites

Sorry to jump on the "you shouldn't need to do this" bandwagon, but here I am... = D

 

When a user logs in and is authorized, you should have MySQL load everything in that user's profile into an array, and carry that array around in the script (you can utilize sessions here as well). That way, everytime you need the user's first name, you don't have to ask MySQL for it. It's in $userinfo['fname'] or whatever. This is quite simple...

 

PhREEEk

Link to comment
Share on other sites

Which is the 'extra step' you want to remove?

$user_values = get_sub('fname,lname',$id);
$fname = $user_values['fname'];
$lname = $user_values['lname'];

I know it's not much, but I'd rather it not be there and just do it the way I provided. I have a few other functions that are similar and that's how they're called. I just want to be consistent with the rest of my app. :)

 

Sorry to jump on the "you shouldn't need to do this" bandwagon, but here I am... = D

This is for a mailing list program. The function her is for retrieving subscriber information, not the admin. :)
Link to comment
Share on other sites

All I did was change some variable names. What you ask for is what the code I provided should do for you. I just thought $user_values was a much more appropriate name than $row_subs. But, that's a simple emough change:

 

<?php
// get_sub('field names,serarated by, commas','subscriber's id');
function get_sub($fields,$id) {

select_db();
$query = "SELECT $fields FROM subscribers WHERE id = $id";
$result = mysql_query($query) or die(mysql_error()."<br>Query: $query");

return $result;
}

// top of script
$row_subs = get_sub('fname,lname',$id);

First name: $row_subs['fname'];
Last name: $row_subs['lname'];

?>

 

Or if you prefer to use global variables when it is not necessary (which actually creates many extra steps anyway):

 

<?php
// get_sub('field names,serarated by, commas','subscriber's id');
function get_sub($fields,$id) {

global $row_subs;
select_db();
$query = "SELECT $fields FROM subscribers WHERE id = $id";
$result = mysql_query($query) or die(mysql_error()."<br>Query: $query");

$row_subs = $result;
return;
}

// top of script
$row_subs = '';
get_sub('fname,lname',$id);
First name: $row_subs['fname'];
Last name: $row_subs['lname'];

?>

Link to comment
Share on other sites

I was over complicating things.

 

<?php
function get_sub($fields,$id) {

global $row_sub;

select_db();
$sql = mysql_query("SELECT $fields FROM subscribers WHERE id = $id") or die(mysql_error());
$row_sub = mysql_fetch_assoc($sql);
}

get_sub('fname,lname',$id);
var_dump($row_sub['fname']);
var_dump($row_sub['lname']);
?>

 

Thanks for all of your help. I appreciate it.

You're correct about creating a class. Unfortunately, I have no idea how to do that (yet)!

 

Edit: The extra step I was referring to was $row_subs = get_sub('fname,lname',$id); Like I said, not a big deal. I just wanted to be consistent with the rest of my app.

 

Thanks again!

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.