JSHINER Posted May 7, 2007 Share Posted May 7, 2007 I am trying the following: SELECT DISTINCT vendors.id AS id, vendors.name AS name, vendors.name, SUBSTRING_INDEX(vendors.name, ' ', -1) AS lastname, vendors.name, SUBSTRING_INDEX(vendors.name, ' ', 1) AS firstname_a, vendors.name, SUBSTRING_INDEX(vendors.name, ' ', 2) AS middle_a,"; if('middle_a'=='name') { $query .= " vendors.name, SUBSTRING_INDEX(vendors.name, ' ', 1) AS firstname,"; } else { $query .= " vendors.name, SUBSTRING_INDEX(vendors.name, ' ', 2) AS firstname,"; } Is the if('middle_a'=='name') correct? Because on ones that I know middle_a should be equal to name, it is not working. Quote Link to comment https://forums.phpfreaks.com/topic/50397-how-to-use-in-a-query/ Share on other sites More sharing options...
per1os Posted May 7, 2007 Share Posted May 7, 2007 You are combining PHP with MySQL. You need to fetch the actual row than perform an if on the variable. IE: <?php if('middle_a'=='name') { // incorrect because of course 'middle_a' is not equal to 'name' } // should be like this if ($row['middle_a'] == 'name') { // this way the variable is being set to equal name. Remember mysql and php are completely seperate. You have to make sure you retrieved the row first and put it into a variable for checking purposes. Quote Link to comment https://forums.phpfreaks.com/topic/50397-how-to-use-in-a-query/#findComment-247517 Share on other sites More sharing options...
JSHINER Posted May 7, 2007 Author Share Posted May 7, 2007 OK - However, middle_a is being created up top. name is stored in the database. How would I create a relationship between those two? Quote Link to comment https://forums.phpfreaks.com/topic/50397-how-to-use-in-a-query/#findComment-247525 Share on other sites More sharing options...
per1os Posted May 7, 2007 Share Posted May 7, 2007 Your logic is flawed. 'middle_a' is not a variable it is a literal. Think of it this way, if I were to say this: <?php if (1 == 2) { } Would that ever get ran? No, because obviously 1 is not equal to 2, but however if I were to do this: <?php $one = 2; if ($one == 2) { } That will be ran because the value stored in $one, which was defined inside PHP at the top is equal to 2. What you are thinking is that PHP is processing the mySQL right then and there and as such should be able to take what was defined in the sql query of "middle_a" and process it without it being a variable. I would look into the basics of programming again because your logic is flawed. I could be missing something from the code as it seems you only posted a portion of it, maybe post the whole thing, as it might help clear up some issues. Quote Link to comment https://forums.phpfreaks.com/topic/50397-how-to-use-in-a-query/#findComment-247530 Share on other sites More sharing options...
sasa Posted May 7, 2007 Share Posted May 7, 2007 try SELECT DISTINCT vendors.id AS id, vendors.name AS name, SUBSTRING_INDEX(vendors.name, ' ', -1) AS lastname, SUBSTRING_INDEX(vendors.name, ' ', 1) AS firstname_a, if(SUBSTRING_INDEX(SUBSTRING_INDEX(vendors.name, ' ', 2), ' ',-1)=SUBSTRING_INDEX(vendors.name, ' ', -1),'',SUBSTRING_INDEX(SUBSTRING_INDEX(vendors.name, ' ', 2), ' ',-1)) AS middle_a from vendors Quote Link to comment https://forums.phpfreaks.com/topic/50397-how-to-use-in-a-query/#findComment-247541 Share on other sites More sharing options...
JSHINER Posted May 7, 2007 Author Share Posted May 7, 2007 That last one gives me an unexpected = error. Ok my logic is . . . name is currently in the database, if the results that = middle_a are equal to name, I want X done. If they are not equal, I want Y done. This is how I get middle_a: vendors.name, SUBSTRING_INDEX(vendors.name, ' ', 2) AS middle_a,"; So what I am doing is lets say name = John Smith . . . if middle_a = John Smith - I want X to happen. Quote Link to comment https://forums.phpfreaks.com/topic/50397-how-to-use-in-a-query/#findComment-247553 Share on other sites More sharing options...
per1os Posted May 7, 2007 Share Posted May 7, 2007 Your logic is flawed in the sense of you are using php to check a literal vs a literal. Not a variable vs a literal. 'middle_a' does not equal 'name' It is like saying if JSHINER = FrosT which it is not, understand? Now having that said, you can use mysql's version of IF statements: http://dev.mysql.com/doc/refman/5.1/en/if-statement.html You need the mysql if syntax not PHP from what I am understanding. Quote Link to comment https://forums.phpfreaks.com/topic/50397-how-to-use-in-a-query/#findComment-247557 Share on other sites More sharing options...
sasa Posted May 7, 2007 Share Posted May 7, 2007 look http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html Quote Link to comment https://forums.phpfreaks.com/topic/50397-how-to-use-in-a-query/#findComment-247569 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.