euel Posted January 12, 2012 Share Posted January 12, 2012 Hi! gud pm guys! I have a simple problem which i can't seem to workout so i kinda need your expert help! I'm trying to compare a variable to a string in my db, the variable and the entry on my db has a format of (car - branch), I made this simple function to do that but it doesn't seem to compare properly.. function get_car_code($branch) { global $connection; $query = "SELECT code "; $query .= "FROM car_code "; $query .= "WHERE branch =" . '"$branch"'; $query .= " LIMIT 1"; $result_set = mysql_query($query, $connection); confirm_query($result_set); return $result_set; } when its called back it doesn't return anything.. I'm thinking its the white spaces (or I'm wrong) but i needed them so i cant removed them.. Any suggestions? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/254863-string-comparison/ Share on other sites More sharing options...
euel Posted January 12, 2012 Author Share Posted January 12, 2012 Opppss! should be.. $query = "SELECT * "; any ideas y its not working? Link to comment https://forums.phpfreaks.com/topic/254863-string-comparison/#findComment-1306821 Share on other sites More sharing options...
harristweed Posted January 12, 2012 Share Posted January 12, 2012 try changing this line. to this: $query .= "WHERE branch = '$branch' "; Link to comment https://forums.phpfreaks.com/topic/254863-string-comparison/#findComment-1306848 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2012 Share Posted January 12, 2012 if that doesn't fix it we could use a look at your confirm_query() function. Link to comment https://forums.phpfreaks.com/topic/254863-string-comparison/#findComment-1306853 Share on other sites More sharing options...
euel Posted January 12, 2012 Author Share Posted January 12, 2012 Thanks harristweed ! Ill try that as early as i can tomorrow! @Muddy_Funster Come to think of it.. it may probably the answer.. really don't know cuz i cant check it right now, gonna reply as soon i can test everything out! Thanks for the replies guys! Link to comment https://forums.phpfreaks.com/topic/254863-string-comparison/#findComment-1306885 Share on other sites More sharing options...
euel Posted January 12, 2012 Author Share Posted January 12, 2012 Holy Mother of Christ! it seems i solved my stupid problem after a short sleep.. i was editing the wrong php file while looking for changes on the wrong php page.... soo lame.. Anyways just for the record, i tried your code harrisweed and it works perfectly! thanks! Can i ask another help? its kinda related to this..or will i put it in another topic? Anyways here it is: I have a user table with a field name of branch where it contains a string like this - > car1 - branch1, car2 - branch2, car3 - branch3... I need to make it an array so i can separate each (car - branch) so i did this code: function display_user_branches($user_id, $branch = NULL) { $branches = get_all_branch($user_id); $all_branches = mysql_fetch_array($branches); $split_branches = preg_split("/,/", $all_branches['brand'], -1, PREG_SPLIT_NO_EMPTY); foreach($split_branches as $value) { echo "<option value='{$value}' "; if($value == $branch) { echo " selected"; } echo ">$value</option>"; } } and is being called by this: echo "<td>"; echo "<select name='branch' >"; display_user_branches($reserved['users_id'], $reserved['branch']); echo "</select>"; echo "</td>"; everything works fine, it displays all car - branch in each option except for the comparison to make it selected. Can't seem to figure out what's wrong.. Again $value and $branch has a format of Car - Branch like my first solved problem.. Maybe i messed up some simple thingy.. Any advice? Thanks in advance!!!! Link to comment https://forums.phpfreaks.com/topic/254863-string-comparison/#findComment-1306950 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2012 Share Posted January 12, 2012 At forst glance it looks like a white space problem. try: function display_user_branches($user_id, $branch = NULL) { $branches = get_all_branch($user_id); $all_branches = mysql_fetch_array($branches); $split_branches = preg_split("/,/", $all_branches['brand'], -1, PREG_SPLIT_NO_EMPTY); foreach($split_branches as $value) { echo "<option value='{$value}' "; if(trim($value) == trim($branch)) { echo " selected=\"selected\""; } echo ">$value</option>"; } } I just changed your selected statement too. selected on it's own isn't standards complient. Link to comment https://forums.phpfreaks.com/topic/254863-string-comparison/#findComment-1306955 Share on other sites More sharing options...
euel Posted January 13, 2012 Author Share Posted January 13, 2012 Right on the spot Muddy_Funster! Thanks!! That's why i don't like putting white space in my db but my boss insist it should be like that, oh well.. Thanks again everyone!! Is there a button for 2ble solved? lol! Link to comment https://forums.phpfreaks.com/topic/254863-string-comparison/#findComment-1307117 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.