Forum Moderators: coopster
This is a simplified version of the code I have to search for all users (and their emails) who have selected Yes in a certain column of my DB. This "certain column" is my source of trouble; I want it to be a variable determined by the $_SESSION['user'], (so that the results match the user logged in) but I'm not sure how to insert the $VARIABLE in my preexisting script. How do I do this (the bold lines are really all I need help with)?
Preexisting script:
_____________________________________________________
$sql = "SELECT cat_id, cat_title FROM table
WHERE $VARIABLE='y'";
$result = mysql_query( $sql,$conn );
while( $row=mysql_fetch_row($result) )
{
$cat_titles[] = $row[1];
}
mysql_free_result( $result );
?>
_____________________________________________________
Again, variable: $_SESSION['user']
Thanks, I appreciate all the pointers I've been given here.
$VARIABLE= $_SESSION['user'];... or you could just stick it right into the query ...
$sql = "SELECT cat_id, cat_title FROM table WHERE $VARIABLE='y'";
$sql = "SELECT cat_id, cat_title FROM table WHERE {$_SESSION['user']}='y'";Note the braces surrounding the superglobal variable since it is an array [php.net]
I tried and tweaked both methods, ensured I was connected, yet I get these errors:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/virtual/site110/fst/var/www/html/match.php on line 31
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/virtual/site110/fst/var/www/html/match.php on line 37
Warning: Invalid argument supplied for foreach() in /home/virtual/site110/fst/var/www/html/match.php on line 41
for this code:
__________________________________
$USERCOL = $_SESSION['user'];
$sql = "SELECT user, email FROM users WHERE $USERCOL = 'y'";
$result = mysql_query( $sql,$conn );
while( $row=mysql_fetch_assoc($result) )
{
$usercol[] = $row['usercol'];
// do stuff with other column
// data if we want
}
mysql_free_result( $result );
___________________________________
What does all this mean?
Post a bit more about what you are tying to do and show a dump of your table structure.
Cheers
Charlie
CREATE TABLE users (
id int(11) NOT NULL auto_increment,
login varchar(25) NOT NULL default '',
pw varchar(32) NOT NULL default '',
real_name varchar(32) NOT NULL default '',
extra_info varchar(100) NOT NULL default '',
email varchar(50) NOT NULL default '',
tmp_mail varchar(50) NOT NULL default '',
active enum('y','n') NOT NULL default 'n',
PRIMARY KEY (id),
UNIQUE KEY mail (email),
UNIQUE KEY user (login)
) TYPE=MyISAM;
It might also help if I tell you exactly what I'm trying to do: After, the column 'active', there will be another column for each real_name, login, or id (preferably id, but whichever works), and the column will be enum('y','n') NOT NULL default 'n'.
I'm trying to make a page that automatically searches and prints to the screen the 'y' values in the logged-in user's column. In order to do this, I am querying the column of the user by using $_SESSION['user'] to match the logged-in user with the relevant column (because both the logged-in user and his or her column have the same name). If you guys know a better method to query this information for the relevant user, I would appreciate it.
Note: in my last post I wrote "user, email"; it should have been "real_name, email".
Thanks guys, I appreciate your help.
My guess would be that you want to add a field called 'last_visit '. You would then update the laat_visit time
whenever the user logs in and each time they load a page.
Now whenever any page is loaded by anyone you query the database to see who has a last_visit time of say less then 5 mins ago. You then just display a list of names from those records.
You can use this same schemce to ehance other aspects of your pages as well, for instance if you collect the last_visit time when the user logs in you can make a little line saying 'Your last visit was on ....
You can also keep a time field when you update your pages and show a list of 'Articles since your last vist.
I hope this is relavent to what you are doing if not feel free to sticky me with more details of what you actually want to do.
$sql = "SELECT user, email FROM users WHERE $USERCOL = 'y'";
echo "\$sql == '$sql'<br>\n";
...so you can see if the query is properly formed.
BUT, as long as you dumped the table here, one problem I see is that there is no column "user" in the table and yet you've used it in your query. That's at least one reason why there is no result set, causing the first error, and everything tumbles down from there.
Even with that correction, I also noticed that your line
$usercol[] = $row['usercol'];
...will cause you problems because there is no 'usercol' in either your query or table.
I'd also suggest that you try including the mysql_error() function when making any MySQL queries, like:
if (!$result = mysql_query($sql,$conn))die(mysql_error());
...instead of simply:
$result = mysql_query( $sql,$conn );
That will give you a more meaningful message as to what's wrong.
I hope this helps.
Now what I get is this:
Warning: Invalid argument supplied for foreach() in /home/virtual/site110/fst/var/www/html/match.php on line 41
$sql == 'SELECT real_name, email FROM users WHERE inuwolf = 'y''
This comes from this line:
$sql = "SELECT real_name, email FROM users WHERE inuwolf = 'y'";
For now, I got rid of the variable and replaced it with inuwolf, an existing column, so it bothers me that it still doesn't work. Charlier, unfortunately I'm not looking for an "I'm Online" indicator; that would probably be a lot easier for me to find. I'm working on a service where users select each other from a list, and when two people select each other, both are notified. What you see here is my (attempted) mechanism to check when the user has a mutual selection with someone else.
I would be forever greatful if you guys kept helping me here, I'm so close to success I can feel it!
Now what you need to do is
1. Do a query for each member and then extract the member ids of his/her selections from the selections column.
2. Take each of the member ids from step 1 and do a query to get their selections.
3. Compare the selections for the member from step 2 to the original member id from step one. If you find the member id from step one in the list from step 2 you have a match.
I guess it's only peripheral now, but I could still use help with my problematic SQL query, because even though this is a new beginning, I'll probably face the same problem again.
Thanks
Edit: If anyone knows a good, simple many-to-many script, please post or mail it. I'm looking around, but most many-to-many tutorials are strictly conceptual--actual scripts are scarce thus far.
[MySQL] Extend Who's online system [webmasterworld.com]
Lots of information to absorb there but it may be something that interests you.
1. Do a query for each member and then extract the member ids of his/her selections from the selections column.2. Take each of the member ids from step 1 and do a query to get their selections.
3. Compare the selections for the member from step 2 to the original member id from step one. If you find the member id from step one in the list from step 2 you have a match.
This seems like such an essential peice of PHP, why can't I find a script for it anywhere. A lot of what I have found use three tables and are much more complex than necessary, as well as too complex for me to tweak for my own needs.
Anyone?