tags:

views:

89

answers:

2

Hi, I'm just wondering what kind of mysql command i could execute in php that would select all items from a certain table where columna is not equal to x and columnb is not equal to x

so like

select something from table where columna does not equal x and columnb does not equal x

If anyone could help that would be great!

Thanks

+4  A: 

The key is the sql query, which you will set up as a string:

$sqlquery = "SELECT field1, field2 FROM table WHERE NOT columnA = 'x' AND NOT columbB = 'y'";

Note that there are a lot of ways to specify NOT. Another one that works just as well is:

$sqlquery = "SELECT field1, field2 FROM table WHERE columnA != 'x' AND columbB != 'y'";

Here is a full example of how to use it:

$link = mysql_connect($dbHost,$dbUser,$dbPass) or die("Unable to connect to database");
mysql_select_db("$dbName") or die("Unable to select database $dbName");
$sqlquery = "SELECT field1, field2 FROM table WHERE NOT columnA = 'x' AND NOT columbB = 'y'";
$result=mysql_query($sqlquery);

while ($row = mysql_fetch_assoc($result) {
//do stuff
}

You can do whatever you would like within the above while loop. Access each field of the table as an element of the $row array which means that $row['field1'] will give you the value for field1 on the current row, and $row['field2'] will give you the value for field2.

JGB146
Thanks, great detail :)
Belgin Fish
Glad to help. I started off with the block of code as an example...then I edited like 10 times as I kept saving only to think "wait, I could add one more thing to make it that much clearer"
JGB146
+2  A: 
SELECT * FROM table WHERE columnA != 'x' AND columnB != 'x'
BoltClock