Youko Posted June 12, 2007 Share Posted June 12, 2007 First of, sorry that I keep pestering you guys. Ok, so I'm in a dead end once again. The solution is propably so simple, that I will be hitting my head to the mousepad afterwards, haha. Ok, so here it goes. I have a value set to a variable in php, so how can I make it check a table in a MySQL table from, let's say columm ID that if it finds that some value in the colum ID matches the value set to the variable, it outputs the data entered to the next colum from the same row it found the id from? The table in question is formed like this: id img_source ----------------------------------- 1 | http://site.com/blah1.jpg 2 | http://site.com/blah2.jpg 3 | http://site.com/blah3.jpg 4 | http://site.com/blah4.jpg So if the variable has a value of 3, it checks the database table if it has such a value in the id columm and then outputs the image link next to it. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/ Share on other sites More sharing options...
suma237 Posted June 12, 2007 Share Posted June 12, 2007 Hi, Check this <?php $var=3; $query="SELECT * FROM table-name WHERE id=$var";//this will display the imagesource for id=3 or $query="SELECT * FROM table-name WHERE id >$var";//this will display the imagesource greater than id=3 $res=mysql_query($query); $row=mysql_fetch_array($res); echo $row['img_source']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-272960 Share on other sites More sharing options...
Youko Posted June 12, 2007 Author Share Posted June 12, 2007 Edit: Oh now I know what's going on! The variable can have values in theese forms. Single number or multiple numbers separated by a , Now, ho do I get php to recognize all the numbers there? (this might get messy) Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273173 Share on other sites More sharing options...
trq Posted June 12, 2007 Share Posted June 12, 2007 Syntax wise, this... $user_class = "$post_info[user_class]"; Should be.... $user_class = $post_info['user_class']; Next, add some basic error handling to your query. $res=mysql_query($query) or die(mysql_error()."<br />".$query); Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273176 Share on other sites More sharing options...
Youko Posted June 12, 2007 Author Share Posted June 12, 2007 Oh you'r reply was fast, thank you. I just happened to edit my previous post at the same time, hehe. If you would be kind enough to look at my previous post for new details. Edit: Indeed that was the issues. It doesn't understand the value the variable has. MySQL error follows. Thanks for the help with the error checking. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3,5,6,7' at line 1 SELECT * FROM e107_userclass_classes WHERE userclass_id=2,3,5,6,7 Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273177 Share on other sites More sharing options...
trq Posted June 12, 2007 Share Posted June 12, 2007 You'll want to use the IN sql function. eg; SELECT * FROM e107_userclass_classes WHERE userclass_id IN(2,3,5,6,7); Post your code if you want a better example. Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273191 Share on other sites More sharing options...
Youko Posted June 12, 2007 Author Share Posted June 12, 2007 Ok, I'll post the code: // ignore these global $post_info, $user; // this is where i check the users class and this part of the code works, since // I can output the results with "return $user_class" if(check_class($post_info['user_class'], TRUE) || check_class($user['user_class'], TRUE)) { $user_class = "$post_info[user_class]"; } $query="SELECT * FROM e107_userclass_classes WHERE userclass_id=$user_class"; $res=mysql_query($query) or die(mysql_error()."<br />".$query); $row=mysql_fetch_array($res); return $row['userclass_img']; return $user_class; --------------- The solution you offered wouldn't work, since the variable is not constant. I'd want the script to check, let's say a value of 1,8,3 then perhaps store all those numbers to variables or some other way. So, that it checks if all those numbers (separetly) are found in the id columm and then outputs the next column from every id the numbers match to. Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273200 Share on other sites More sharing options...
trq Posted June 12, 2007 Share Posted June 12, 2007 [quote]$query="SELECT * FROM e107_userclass_classes WHERE userclass_id IN($user_class); And you still havent fixed your poor syntax. $user_class = "$post_info[user_class]"; Should be... $user_class = $post_info['user_class']; Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273204 Share on other sites More sharing options...
Youko Posted June 12, 2007 Author Share Posted June 12, 2007 Thanks for the correction. (Altho that part of the code worked just fine... odd..) Edit: Actually that gives me a parse error, so that change does nothing good. Edit2: Oh nvm, fixed it. But still I need help with the aforementioned issue. I know now way on how to do that and it might even be impossible. If only the system I am working on wasn't designed like this... Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273209 Share on other sites More sharing options...
trq Posted June 12, 2007 Share Posted June 12, 2007 Sorry, I'm not sure what your issue is exactly. You'll need to try and describe what you want a littel clearer. Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273212 Share on other sites More sharing options...
Youko Posted June 12, 2007 Author Share Posted June 12, 2007 Ok I'll try. I'l give a bigger picture, which most information ain't really relevant to the script itself, but to get a bigger picture. I have a CMS system and every user have their own user classes (Admins, MOds, etc) Theese userclasses have an unique id number from 1 to upwards. Now, the system is designed so that it stores all the userclass' the user has in the users table with teh format of, ex 2,5,3,6 Then the userclasses themselves are separated in another table as userclass_id userclass_img | userclass_name -------------------------------------------------------------------- 1 | http://blah.com/pic.jpg | Admin 2 | http://blah.com/pic.jpg | Moderator 3 | http://blah.com/pic.jpg | Whatever Ok. So, I have gotten the user's (that has posted something) userclasses to a variable, but since they are in a format with all the classes in the same columm separated with , so I can't compare that value in the variable with userclass_id's Now what I want to do is get every single number separated from, let's say 2,5,3 (the data in variable) to new variables or find an alternative way to do this. The goal I'm aiming to is that for every single userclass_id the userclass is formed of, the id's userclass_img columm get's shown on the site (return $blahblah). I hope that helped to understand better what I'm doing. Otherwise you need to ask some specific questions, since I don't know if I can explain this better. Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273224 Share on other sites More sharing options...
trq Posted June 12, 2007 Share Posted June 12, 2007 The goal I'm aiming to is that for every single userclass_id the userclass is formed of, the id's userclass_img columm get's shown on the site (return $blahblah). Say you have allready otained a users userclass_id's (eg 2,4,5, and it is now held in the $userclasses variable. A simple query such as.... SELECT userclass_img FROM nameoftable WHERE userclass_id IN($userclasses); Will get you what you describe. Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273229 Share on other sites More sharing options...
Youko Posted June 12, 2007 Author Share Posted June 12, 2007 I know, but since the variable has a value value like 2,3,,3 so it can't match it to the userclass_id I want the script to separate every number to individual variables Oviously it won't find a id 2,4,6 from the userclass_id columm, since they are only single number's there Every number that the variable has reperesents one userclass so if the user has more than one, they become separated with coma. Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273236 Share on other sites More sharing options...
trq Posted June 12, 2007 Share Posted June 12, 2007 Have you actually ran the query? Do you know what the IN() sql function does? This.... SELECT foo FROM bar WHERE id IN(1,2,3); Would select each record of foo where the id's equal 1, 2 and 3. Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273243 Share on other sites More sharing options...
Youko Posted June 12, 2007 Author Share Posted June 12, 2007 My deepest apologies, I misread. Yay! It works! *hugs* Thanks so much! Sorry that I tried your patience... All is well now.... atleast for now. Edit: ACtually there is still one issue, but we are getting there! It only checks the first number of the $usrclass and outputs the data acording to that, but id wan't it to go thru all the numbers in the variable. Is that possible? I mean that if a user has a userclass 1,2 only the picture from the userclass_img for class 1 gets output. Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273245 Share on other sites More sharing options...
trq Posted June 12, 2007 Share Posted June 12, 2007 Do you know how to loop through a result? eg; <?php $sql = "SELECT userclass_img FROM nameoftable WHERE userclass_id IN($userclasses)"; if ($result = mysql_query($result)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { // loop through the result. echo $row['userclass_img']."<br />"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273261 Share on other sites More sharing options...
Youko Posted June 12, 2007 Author Share Posted June 12, 2007 Ok, now my code looks like this: global $post_info, $user; if(check_class($post_info['user_class'], TRUE) || check_class($user['user_class'], TRUE)) { $user_class = $post_info['user_class']; } $query = "SELECT userclass_img FROM e107_userclass_classes WHERE userclass_id IN($user_class)"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { $return $row['userclass_img']; } } } It still only show's the output for the first number in the variable, not for all, since they are separated with coma. Or atleast I think that's the reason. Btw, Thorpe, you had an error in your syntax. Not: if ($result = mysql_query($result)) { But: if ($result = mysql_query($query)) { Edit: Fixed some things... Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273355 Share on other sites More sharing options...
Youko Posted June 12, 2007 Author Share Posted June 12, 2007 Since I can't edit my previous post anymore, I'll make a new one. It seems that if I replace return with echo, I get the whole list, but it just bloats it randomly to the top of the page and not where it is supposed to be so return has to be used. I wonder why return doesn't show them all. Quote Link to comment https://forums.phpfreaks.com/topic/55221-need-help-once-again/#findComment-273385 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.