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'];
?>

 

 

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'];
?>

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
?>

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

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. :)

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'];

?>

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.