Use PHP variable in a SELECT LIKE query
I'm trying to execute a mysqli query using the following code:
$sql = "SELECT * FROM `table` WHERE `description` LIKE ('AB CD %')";
$result= mysqli_query($con, $sql) or die(mysqli_error());
and this query gives me 6 results. It will only looks for items like "AB CD EF..." and not items like "AB CDEF...", which is exactly what I want.
But if I give to the LIKE value a variable like this:
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE ('$var%')";
$result= mysqli_query($con, $sql) or die(mysqli_error());
it gives me zero results.
I have tried also several LIKE formats such ...('".$var."%')";
or CONCAT($var, '%')
, but nothing.
How can get the same results as the first query usin a variable like in the sencond query?
The variable is get by a query which will select all description
items and then, inside a while loop, it will look for the first Capital letters of each item:
$name = $row['description'];
$expr = '/[A-Z]*/';
preg_match_all($expr, $name, $res);
$var = implode(' ', $res[0]);
Each row has values like "AB CD EF something else in not capital letters"
Thank you.
php mysqli
|
show 12 more comments
I'm trying to execute a mysqli query using the following code:
$sql = "SELECT * FROM `table` WHERE `description` LIKE ('AB CD %')";
$result= mysqli_query($con, $sql) or die(mysqli_error());
and this query gives me 6 results. It will only looks for items like "AB CD EF..." and not items like "AB CDEF...", which is exactly what I want.
But if I give to the LIKE value a variable like this:
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE ('$var%')";
$result= mysqli_query($con, $sql) or die(mysqli_error());
it gives me zero results.
I have tried also several LIKE formats such ...('".$var."%')";
or CONCAT($var, '%')
, but nothing.
How can get the same results as the first query usin a variable like in the sencond query?
The variable is get by a query which will select all description
items and then, inside a while loop, it will look for the first Capital letters of each item:
$name = $row['description'];
$expr = '/[A-Z]*/';
preg_match_all($expr, $name, $res);
$var = implode(' ', $res[0]);
Each row has values like "AB CD EF something else in not capital letters"
Thank you.
php mysqli
how about$var = 'AB CD %';
?
– Always Sunny
Nov 24 '18 at 16:28
Yes, tried, also.
– Goncalorsr
Nov 24 '18 at 16:31
Maybe is important to refer how I get the variable. I will edit the post
– Goncalorsr
Nov 24 '18 at 16:31
Yes that is very much important. I guess you need to escape the string also.
– Always Sunny
Nov 24 '18 at 16:32
2
How about using prepared statements...
– dn Fer
Nov 24 '18 at 16:35
|
show 12 more comments
I'm trying to execute a mysqli query using the following code:
$sql = "SELECT * FROM `table` WHERE `description` LIKE ('AB CD %')";
$result= mysqli_query($con, $sql) or die(mysqli_error());
and this query gives me 6 results. It will only looks for items like "AB CD EF..." and not items like "AB CDEF...", which is exactly what I want.
But if I give to the LIKE value a variable like this:
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE ('$var%')";
$result= mysqli_query($con, $sql) or die(mysqli_error());
it gives me zero results.
I have tried also several LIKE formats such ...('".$var."%')";
or CONCAT($var, '%')
, but nothing.
How can get the same results as the first query usin a variable like in the sencond query?
The variable is get by a query which will select all description
items and then, inside a while loop, it will look for the first Capital letters of each item:
$name = $row['description'];
$expr = '/[A-Z]*/';
preg_match_all($expr, $name, $res);
$var = implode(' ', $res[0]);
Each row has values like "AB CD EF something else in not capital letters"
Thank you.
php mysqli
I'm trying to execute a mysqli query using the following code:
$sql = "SELECT * FROM `table` WHERE `description` LIKE ('AB CD %')";
$result= mysqli_query($con, $sql) or die(mysqli_error());
and this query gives me 6 results. It will only looks for items like "AB CD EF..." and not items like "AB CDEF...", which is exactly what I want.
But if I give to the LIKE value a variable like this:
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE ('$var%')";
$result= mysqli_query($con, $sql) or die(mysqli_error());
it gives me zero results.
I have tried also several LIKE formats such ...('".$var."%')";
or CONCAT($var, '%')
, but nothing.
How can get the same results as the first query usin a variable like in the sencond query?
The variable is get by a query which will select all description
items and then, inside a while loop, it will look for the first Capital letters of each item:
$name = $row['description'];
$expr = '/[A-Z]*/';
preg_match_all($expr, $name, $res);
$var = implode(' ', $res[0]);
Each row has values like "AB CD EF something else in not capital letters"
Thank you.
php mysqli
php mysqli
edited Nov 24 '18 at 16:39
Goncalorsr
asked Nov 24 '18 at 16:26
GoncalorsrGoncalorsr
134
134
how about$var = 'AB CD %';
?
– Always Sunny
Nov 24 '18 at 16:28
Yes, tried, also.
– Goncalorsr
Nov 24 '18 at 16:31
Maybe is important to refer how I get the variable. I will edit the post
– Goncalorsr
Nov 24 '18 at 16:31
Yes that is very much important. I guess you need to escape the string also.
– Always Sunny
Nov 24 '18 at 16:32
2
How about using prepared statements...
– dn Fer
Nov 24 '18 at 16:35
|
show 12 more comments
how about$var = 'AB CD %';
?
– Always Sunny
Nov 24 '18 at 16:28
Yes, tried, also.
– Goncalorsr
Nov 24 '18 at 16:31
Maybe is important to refer how I get the variable. I will edit the post
– Goncalorsr
Nov 24 '18 at 16:31
Yes that is very much important. I guess you need to escape the string also.
– Always Sunny
Nov 24 '18 at 16:32
2
How about using prepared statements...
– dn Fer
Nov 24 '18 at 16:35
how about
$var = 'AB CD %';
?– Always Sunny
Nov 24 '18 at 16:28
how about
$var = 'AB CD %';
?– Always Sunny
Nov 24 '18 at 16:28
Yes, tried, also.
– Goncalorsr
Nov 24 '18 at 16:31
Yes, tried, also.
– Goncalorsr
Nov 24 '18 at 16:31
Maybe is important to refer how I get the variable. I will edit the post
– Goncalorsr
Nov 24 '18 at 16:31
Maybe is important to refer how I get the variable. I will edit the post
– Goncalorsr
Nov 24 '18 at 16:31
Yes that is very much important. I guess you need to escape the string also.
– Always Sunny
Nov 24 '18 at 16:32
Yes that is very much important. I guess you need to escape the string also.
– Always Sunny
Nov 24 '18 at 16:32
2
2
How about using prepared statements...
– dn Fer
Nov 24 '18 at 16:35
How about using prepared statements...
– dn Fer
Nov 24 '18 at 16:35
|
show 12 more comments
3 Answers
3
active
oldest
votes
How about this after wrapping the variable with curly braces {}
? Also I just removed the extra parenthesis ()
after LIKE :)
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE '{$var}%'";
Yes I tried this, also.
– Goncalorsr
Nov 24 '18 at 16:39
add a comment |
Well, user3783243 found the problem.
When I implode the array to get the $var in a string, for some reason I cannot explain, the array elements (a lot of them) that had no value were included. The solution was to explode the $var, pass it through the array_filter function, to delete the empty elements and implode it again.
Thank you all.
add a comment |
Use :
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE '".$var."%'";
But this is a bad practice as well as highly vulnerable, use prepared statement instead : http://php.net/manual/en/book.pdo.php
That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.
– user3783243
Nov 24 '18 at 18:06
add a comment |
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%2f53460128%2fuse-php-variable-in-a-select-like-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
How about this after wrapping the variable with curly braces {}
? Also I just removed the extra parenthesis ()
after LIKE :)
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE '{$var}%'";
Yes I tried this, also.
– Goncalorsr
Nov 24 '18 at 16:39
add a comment |
How about this after wrapping the variable with curly braces {}
? Also I just removed the extra parenthesis ()
after LIKE :)
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE '{$var}%'";
Yes I tried this, also.
– Goncalorsr
Nov 24 '18 at 16:39
add a comment |
How about this after wrapping the variable with curly braces {}
? Also I just removed the extra parenthesis ()
after LIKE :)
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE '{$var}%'";
How about this after wrapping the variable with curly braces {}
? Also I just removed the extra parenthesis ()
after LIKE :)
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE '{$var}%'";
answered Nov 24 '18 at 16:31
Always SunnyAlways Sunny
16.4k32847
16.4k32847
Yes I tried this, also.
– Goncalorsr
Nov 24 '18 at 16:39
add a comment |
Yes I tried this, also.
– Goncalorsr
Nov 24 '18 at 16:39
Yes I tried this, also.
– Goncalorsr
Nov 24 '18 at 16:39
Yes I tried this, also.
– Goncalorsr
Nov 24 '18 at 16:39
add a comment |
Well, user3783243 found the problem.
When I implode the array to get the $var in a string, for some reason I cannot explain, the array elements (a lot of them) that had no value were included. The solution was to explode the $var, pass it through the array_filter function, to delete the empty elements and implode it again.
Thank you all.
add a comment |
Well, user3783243 found the problem.
When I implode the array to get the $var in a string, for some reason I cannot explain, the array elements (a lot of them) that had no value were included. The solution was to explode the $var, pass it through the array_filter function, to delete the empty elements and implode it again.
Thank you all.
add a comment |
Well, user3783243 found the problem.
When I implode the array to get the $var in a string, for some reason I cannot explain, the array elements (a lot of them) that had no value were included. The solution was to explode the $var, pass it through the array_filter function, to delete the empty elements and implode it again.
Thank you all.
Well, user3783243 found the problem.
When I implode the array to get the $var in a string, for some reason I cannot explain, the array elements (a lot of them) that had no value were included. The solution was to explode the $var, pass it through the array_filter function, to delete the empty elements and implode it again.
Thank you all.
answered Nov 24 '18 at 18:22
GoncalorsrGoncalorsr
134
134
add a comment |
add a comment |
Use :
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE '".$var."%'";
But this is a bad practice as well as highly vulnerable, use prepared statement instead : http://php.net/manual/en/book.pdo.php
That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.
– user3783243
Nov 24 '18 at 18:06
add a comment |
Use :
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE '".$var."%'";
But this is a bad practice as well as highly vulnerable, use prepared statement instead : http://php.net/manual/en/book.pdo.php
That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.
– user3783243
Nov 24 '18 at 18:06
add a comment |
Use :
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE '".$var."%'";
But this is a bad practice as well as highly vulnerable, use prepared statement instead : http://php.net/manual/en/book.pdo.php
Use :
$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE '".$var."%'";
But this is a bad practice as well as highly vulnerable, use prepared statement instead : http://php.net/manual/en/book.pdo.php
answered Nov 24 '18 at 17:57
LabLab
1,00489
1,00489
That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.
– user3783243
Nov 24 '18 at 18:06
add a comment |
That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.
– user3783243
Nov 24 '18 at 18:06
That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.
– user3783243
Nov 24 '18 at 18:06
That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.
– user3783243
Nov 24 '18 at 18:06
add a comment |
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%2f53460128%2fuse-php-variable-in-a-select-like-query%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
how about
$var = 'AB CD %';
?– Always Sunny
Nov 24 '18 at 16:28
Yes, tried, also.
– Goncalorsr
Nov 24 '18 at 16:31
Maybe is important to refer how I get the variable. I will edit the post
– Goncalorsr
Nov 24 '18 at 16:31
Yes that is very much important. I guess you need to escape the string also.
– Always Sunny
Nov 24 '18 at 16:32
2
How about using prepared statements...
– dn Fer
Nov 24 '18 at 16:35