Jump to content

How to use == in a Query


JSHINER

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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