MySQL combining two where operators WHERE row LIKE '%$search%' AND WHERE NOT row='$id'
SELECT * FROM table_name WHERE column_name LIKE '%$search%' AND WHERE NOT column_name='$id';
I want to select all the results from my database that are like my search term, but exclude the row that is equal to my php variable $id.
Is it possible to do this in a MySQL query?
mysql select search where like
add a comment |
SELECT * FROM table_name WHERE column_name LIKE '%$search%' AND WHERE NOT column_name='$id';
I want to select all the results from my database that are like my search term, but exclude the row that is equal to my php variable $id.
Is it possible to do this in a MySQL query?
mysql select search where like
A literal string'string'
does not translate variables to their value, so this would not work unless you use"double quoted strings"
. But don't do that either, use prepared statements by either usingPDO
orMySQLi
.
– Xorifelse
Nov 24 '18 at 11:50
my sql variable is using mysqli $sql = "SELECT * FROM table_name WHERE column_name LIKE '%$search%' AND WHERE NOT column_name='$id'";
– Hurly
Nov 24 '18 at 11:54
Exactly, you're not using prepared statements. Your database is wide open to an attack if you don't handle your data correctly.
– Xorifelse
Nov 24 '18 at 20:36
add a comment |
SELECT * FROM table_name WHERE column_name LIKE '%$search%' AND WHERE NOT column_name='$id';
I want to select all the results from my database that are like my search term, but exclude the row that is equal to my php variable $id.
Is it possible to do this in a MySQL query?
mysql select search where like
SELECT * FROM table_name WHERE column_name LIKE '%$search%' AND WHERE NOT column_name='$id';
I want to select all the results from my database that are like my search term, but exclude the row that is equal to my php variable $id.
Is it possible to do this in a MySQL query?
mysql select search where like
mysql select search where like
edited Nov 24 '18 at 11:57
Hurly
asked Nov 24 '18 at 11:45
HurlyHurly
82
82
A literal string'string'
does not translate variables to their value, so this would not work unless you use"double quoted strings"
. But don't do that either, use prepared statements by either usingPDO
orMySQLi
.
– Xorifelse
Nov 24 '18 at 11:50
my sql variable is using mysqli $sql = "SELECT * FROM table_name WHERE column_name LIKE '%$search%' AND WHERE NOT column_name='$id'";
– Hurly
Nov 24 '18 at 11:54
Exactly, you're not using prepared statements. Your database is wide open to an attack if you don't handle your data correctly.
– Xorifelse
Nov 24 '18 at 20:36
add a comment |
A literal string'string'
does not translate variables to their value, so this would not work unless you use"double quoted strings"
. But don't do that either, use prepared statements by either usingPDO
orMySQLi
.
– Xorifelse
Nov 24 '18 at 11:50
my sql variable is using mysqli $sql = "SELECT * FROM table_name WHERE column_name LIKE '%$search%' AND WHERE NOT column_name='$id'";
– Hurly
Nov 24 '18 at 11:54
Exactly, you're not using prepared statements. Your database is wide open to an attack if you don't handle your data correctly.
– Xorifelse
Nov 24 '18 at 20:36
A literal string
'string'
does not translate variables to their value, so this would not work unless you use "double quoted strings"
. But don't do that either, use prepared statements by either using PDO
or MySQLi
.– Xorifelse
Nov 24 '18 at 11:50
A literal string
'string'
does not translate variables to their value, so this would not work unless you use "double quoted strings"
. But don't do that either, use prepared statements by either using PDO
or MySQLi
.– Xorifelse
Nov 24 '18 at 11:50
my sql variable is using mysqli $sql = "SELECT * FROM table_name WHERE column_name LIKE '%$search%' AND WHERE NOT column_name='$id'";
– Hurly
Nov 24 '18 at 11:54
my sql variable is using mysqli $sql = "SELECT * FROM table_name WHERE column_name LIKE '%$search%' AND WHERE NOT column_name='$id'";
– Hurly
Nov 24 '18 at 11:54
Exactly, you're not using prepared statements. Your database is wide open to an attack if you don't handle your data correctly.
– Xorifelse
Nov 24 '18 at 20:36
Exactly, you're not using prepared statements. Your database is wide open to an attack if you don't handle your data correctly.
– Xorifelse
Nov 24 '18 at 20:36
add a comment |
1 Answer
1
active
oldest
votes
Put the all the conditions inside a single WHERE
block and use conditional operators like AND/OR/NOT
to combine them
SELECT * FROM table
WHERE `row` LIKE '%$search%' AND
`row` <> '$id';
Also, please learn to use Prepared Statements
$sql = "SELECT * FROM userprofile WHERE userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%' NOT userUid <> '$id'";
This is still not working
– Hurly
Nov 24 '18 at 12:02
removenot
beforeuserUid
. useand useruid <> '$id'
– Madhur Bhaiya
Nov 24 '18 at 12:03
The code has no errors then, but it is still not excluding the rows in theuserUid
column that are equal to php variable $id.
– Hurly
Nov 24 '18 at 12:06
1
@Hurly please use proper parentheses around conditions. put all theor
conditions inside one pair of brackets andand useruid <>
outside that
– Madhur Bhaiya
Nov 24 '18 at 12:08
1
@Hurly do this:$sql = "SELECT * FROM userprofile WHERE (userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%') AND userUid <> '$id'"
– Madhur Bhaiya
Nov 24 '18 at 12:20
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457784%2fmysql-combining-two-where-operators-where-row-like-search-and-where-not-row%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Put the all the conditions inside a single WHERE
block and use conditional operators like AND/OR/NOT
to combine them
SELECT * FROM table
WHERE `row` LIKE '%$search%' AND
`row` <> '$id';
Also, please learn to use Prepared Statements
$sql = "SELECT * FROM userprofile WHERE userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%' NOT userUid <> '$id'";
This is still not working
– Hurly
Nov 24 '18 at 12:02
removenot
beforeuserUid
. useand useruid <> '$id'
– Madhur Bhaiya
Nov 24 '18 at 12:03
The code has no errors then, but it is still not excluding the rows in theuserUid
column that are equal to php variable $id.
– Hurly
Nov 24 '18 at 12:06
1
@Hurly please use proper parentheses around conditions. put all theor
conditions inside one pair of brackets andand useruid <>
outside that
– Madhur Bhaiya
Nov 24 '18 at 12:08
1
@Hurly do this:$sql = "SELECT * FROM userprofile WHERE (userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%') AND userUid <> '$id'"
– Madhur Bhaiya
Nov 24 '18 at 12:20
|
show 4 more comments
Put the all the conditions inside a single WHERE
block and use conditional operators like AND/OR/NOT
to combine them
SELECT * FROM table
WHERE `row` LIKE '%$search%' AND
`row` <> '$id';
Also, please learn to use Prepared Statements
$sql = "SELECT * FROM userprofile WHERE userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%' NOT userUid <> '$id'";
This is still not working
– Hurly
Nov 24 '18 at 12:02
removenot
beforeuserUid
. useand useruid <> '$id'
– Madhur Bhaiya
Nov 24 '18 at 12:03
The code has no errors then, but it is still not excluding the rows in theuserUid
column that are equal to php variable $id.
– Hurly
Nov 24 '18 at 12:06
1
@Hurly please use proper parentheses around conditions. put all theor
conditions inside one pair of brackets andand useruid <>
outside that
– Madhur Bhaiya
Nov 24 '18 at 12:08
1
@Hurly do this:$sql = "SELECT * FROM userprofile WHERE (userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%') AND userUid <> '$id'"
– Madhur Bhaiya
Nov 24 '18 at 12:20
|
show 4 more comments
Put the all the conditions inside a single WHERE
block and use conditional operators like AND/OR/NOT
to combine them
SELECT * FROM table
WHERE `row` LIKE '%$search%' AND
`row` <> '$id';
Also, please learn to use Prepared Statements
Put the all the conditions inside a single WHERE
block and use conditional operators like AND/OR/NOT
to combine them
SELECT * FROM table
WHERE `row` LIKE '%$search%' AND
`row` <> '$id';
Also, please learn to use Prepared Statements
edited Nov 24 '18 at 11:52
answered Nov 24 '18 at 11:46
Madhur BhaiyaMadhur Bhaiya
19.6k62236
19.6k62236
$sql = "SELECT * FROM userprofile WHERE userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%' NOT userUid <> '$id'";
This is still not working
– Hurly
Nov 24 '18 at 12:02
removenot
beforeuserUid
. useand useruid <> '$id'
– Madhur Bhaiya
Nov 24 '18 at 12:03
The code has no errors then, but it is still not excluding the rows in theuserUid
column that are equal to php variable $id.
– Hurly
Nov 24 '18 at 12:06
1
@Hurly please use proper parentheses around conditions. put all theor
conditions inside one pair of brackets andand useruid <>
outside that
– Madhur Bhaiya
Nov 24 '18 at 12:08
1
@Hurly do this:$sql = "SELECT * FROM userprofile WHERE (userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%') AND userUid <> '$id'"
– Madhur Bhaiya
Nov 24 '18 at 12:20
|
show 4 more comments
$sql = "SELECT * FROM userprofile WHERE userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%' NOT userUid <> '$id'";
This is still not working
– Hurly
Nov 24 '18 at 12:02
removenot
beforeuserUid
. useand useruid <> '$id'
– Madhur Bhaiya
Nov 24 '18 at 12:03
The code has no errors then, but it is still not excluding the rows in theuserUid
column that are equal to php variable $id.
– Hurly
Nov 24 '18 at 12:06
1
@Hurly please use proper parentheses around conditions. put all theor
conditions inside one pair of brackets andand useruid <>
outside that
– Madhur Bhaiya
Nov 24 '18 at 12:08
1
@Hurly do this:$sql = "SELECT * FROM userprofile WHERE (userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%') AND userUid <> '$id'"
– Madhur Bhaiya
Nov 24 '18 at 12:20
$sql = "SELECT * FROM userprofile WHERE userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%' NOT userUid <> '$id'";
This is still not working– Hurly
Nov 24 '18 at 12:02
$sql = "SELECT * FROM userprofile WHERE userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%' NOT userUid <> '$id'";
This is still not working– Hurly
Nov 24 '18 at 12:02
remove
not
before userUid
. use and useruid <> '$id'
– Madhur Bhaiya
Nov 24 '18 at 12:03
remove
not
before userUid
. use and useruid <> '$id'
– Madhur Bhaiya
Nov 24 '18 at 12:03
The code has no errors then, but it is still not excluding the rows in the
userUid
column that are equal to php variable $id.– Hurly
Nov 24 '18 at 12:06
The code has no errors then, but it is still not excluding the rows in the
userUid
column that are equal to php variable $id.– Hurly
Nov 24 '18 at 12:06
1
1
@Hurly please use proper parentheses around conditions. put all the
or
conditions inside one pair of brackets and and useruid <>
outside that– Madhur Bhaiya
Nov 24 '18 at 12:08
@Hurly please use proper parentheses around conditions. put all the
or
conditions inside one pair of brackets and and useruid <>
outside that– Madhur Bhaiya
Nov 24 '18 at 12:08
1
1
@Hurly do this:
$sql = "SELECT * FROM userprofile WHERE (userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%') AND userUid <> '$id'"
– Madhur Bhaiya
Nov 24 '18 at 12:20
@Hurly do this:
$sql = "SELECT * FROM userprofile WHERE (userUid LIKE '%$search%' OR profileFirstName LIKE '%$search%' OR profileLastName LIKE '%$search%' OR profileStatus LIKE '%$search%' OR profileGender LIKE '%$search%') AND userUid <> '$id'"
– Madhur Bhaiya
Nov 24 '18 at 12:20
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457784%2fmysql-combining-two-where-operators-where-row-like-search-and-where-not-row%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
A literal string
'string'
does not translate variables to their value, so this would not work unless you use"double quoted strings"
. But don't do that either, use prepared statements by either usingPDO
orMySQLi
.– Xorifelse
Nov 24 '18 at 11:50
my sql variable is using mysqli $sql = "SELECT * FROM table_name WHERE column_name LIKE '%$search%' AND WHERE NOT column_name='$id'";
– Hurly
Nov 24 '18 at 11:54
Exactly, you're not using prepared statements. Your database is wide open to an attack if you don't handle your data correctly.
– Xorifelse
Nov 24 '18 at 20:36