SELECT * FROM Table WHERE Column LIKE %?% - php

Based on information here MySQL query String contains
trying to create pdo query with ?
Experimented with following
SELECT * FROM Table WHERE Column LIKE %?%
SELECT * FROM Table WHERE Column LIKE ?%
SELECT * FROM Table WHERE Column LIKE %?
Nothing works. Get error
Syntax error or access violation: 1064 You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near '%...
Tried with SELECT * FROM Table WHERE Column LIKE ? but this is not for contains
Aim is to get query SELECT * FROM Table WHERE Column contains ?
What is is correct pdo contains statement for positional placeholders (?)?

try this by concatenating the value in the string via PHP,
$value = "valueHere";
$passThis = "%" . $value . "%";
// other pdo codes...
$stmt = $dbh->prepare("SELECT * FROM Table WHERE Column LIKE ?");
$stmt->bindParam(1, $passThis);
// other pdo codes...

after like add quotes. eg:- like '%?%'
i.e:
SELECT * FROM table_name WHERE column_name like '%field_name%';

I think wildcard stament should be within single quotes, like;
SELECT * FROM Table WHERE Column LIKE '%?%';
This returns any record which contains the string given anywhere within the particular column field
Column data which starts with 'ber', example
SELECT * FROM Table WHERE Column LIKE 'ber%';
Hope this helps

Either put the % characters before and after your parameter before you pass it into the query or
SELECT * FROM Table WHERE Column LIKE '%' + ? + '%'
SELECT * FROM Table WHERE Column LIKE ? + '%'
SELECT * FROM Table WHERE Column LIKE '%' + ?
Although this will fail if ? is null because the concatenate will yield null. you could use coalesce
SELECT * FROM Table WHERE Column LIKE '%' + Coalesce(?,'') + '%'
SELECT * FROM Table WHERE Column LIKE Coalesce(?,'') + '%'
SELECT * FROM Table WHERE Column LIKE '%' + Coalesce(?,'')

If you would like to use prepared statements then take a look at http://php.net/manual/de/pdo.prepared-statements.php.
SELECT * FROM REGISTRY where name LIKE '%?%'

Related

Using a variable name after the where clause in mysql and php

I'm trying to write a PHP string to create an SQL statement which uses a variable for a field name after the WHERE clause. The problem is that it doesn't use the variable name, I'm not good with PHP so not sure if it's my string concatenation or something else... any help would be appreciated! I'm using PHP 5.5 with mysql, as mysqli isn't available with my current host.
echo $sql_subject;
$sqlString2 = "SELECT * FROM tableName WHERE ". $sql_subject . " = '$set'";
However it keeps outputting:
S
SELECT * FROM tableName WHERE '' = '1'
I know the $sql_subject has a value because it's printing above the SQL output... I'd like it to say:
SELECT * FROM tableName WHERE S = '1'
Thanks

How to select data from columns that match a criteria?

Hi i have a table in mysql database which has columns like UI_12-Apr-2016,DA_12-Apr-2016.
----------------------------------------------------------------------
| DA_12-Apr-2016 | UI_13-Apr-2016 | UI_12-Apr-2016 | DA_13-Apr-2016 |
|---------------------------------------------------------------------
| |
----------------------------------------------------------------------
How do i fetch data from the table whose columns have 12-Apr-2016 in it. Is there any way to select data according to the criteria. I know i can run this query:-
SELECT UI_12-Apr-2016,DA_12-Apr-2016 from table;
but the date and the code before it can be anything. I want to create a dynamic query to fetch data from the columns that match the date criteria.
I would be highly grateful if anyone can please provide a solution to do it.
This may help:
select COLUMN_name
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='Table1' and column_name like "%12-Apr-2016%";
in the above query, we can search for column names from INFORMATION_SCHEMA.COLUMNS table(which holds data related to table specified).
Output of above query should:
UI_12-Apr-2016
DA_12-Apr-2016
The result of above query is a list of columns with the pattern given(%12-Apr-2016%). You may save this result in a list and use it to fetch data accordingly from the table.
There is no direct method or query to do the same.
maybe you can create a new table tbl_column with one column column_name include all your columns~
SELECT * FROM tbl_column WHERE column_name LIKE '%12-Apr-2016%';
then you can use the query result to generate a new dynamic sql~
Did you mean:
SELECT * FROM table WHERE column LIKE '%13-Apr-2016%';
?
Update: you should look into information_schema database:
SELECT COLUMN_NAME from `COLUMNS` where `TABLE_NAME` = 'table_name' AND `COLUMN_NAME` LIKE '%12-Apr-2016%';
Simple PHP example script could be:
$table_name = 'table_name';
$col_pattern = '12-Apr-2016';
$mysqli = new mysqli($config['host'], $config['user'], $config['password'], $config['dbname']);
$sql1 = "SELECT COLUMN_NAME from `COLUMNS` where `TABLE_NAME` = '{$table_name}' AND `COLUMN_NAME` LIKE '%{$col_pattern}%'";
$res1 = $mysqli->query($sql1);
$acol = array();
while ($r1 = $res1->fetch_assoc()) {
$acol[] = $r1['COLUMN_NAME'];
}
if (!empty($acol)) {
$sql2 = 'SELECT ' . implode(', ', $acol) . ' FROM ' . $table_name;
$res2 = $mysqli->query($sql2);
while ($r2 = $res2->fetch_assoc()) {
echo var_export($r2, 1) . PHP_EOL;
}
}
You must put together your SQL statement dynamically. E.g. the following statement uses the Oracle view ALL_TAB_COLUMNS to produce a select column_name from table_name statement for every column that contains 12-Apr-2016:
SELECT 'SELECT ' || COLUMN_NAME || ' FROM ' || TABLE_NAME || ';'
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'MY_TABLE'
AND COLUMN_NAME LIKE '%12-Apr-2016%'
This may not be exactly what you want, however, this illustrates the general idea.
It is not possible to dynamically come up with names of columns since MySQL does not have an eval() function. Since you are using PHP, it is preferable to construct the query from your application in order to achieve this.
If you really insist for a solution using MySQL, there is one using prepared statements that is not too hard to come up with as follows:
set #table='your_table_name';
select group_concat(column_name)
from information_schema.columns
where table_name='your_table_name'
and column_name like '%12_Apr_2016'
into #colnames;
set #construct= CONCAT('SELECT ', #colnames, ' FROM ', #table);
prepare query from #construct;
execute query;
SQL Fiddle for reference.
But overall, having columns named using dynamically generated DDL is not the best schema modeling practice.

How to ask a SELECT * from table where all columns that = $var

hi how to select columns that have the var value?
like this:
$var=$_GET['id'];
$select="SELECT * FROM table WHERE **COLUMNS THAT EQUAL TO VAR** = $var";
How to select those columns that have the $var value?
Clarification: SELECT only the columns that have the value $var in them.
Example:
$var="1";
Column 1, 7, 11 have on some row value 1 then pick those columns and show in which columns the value 1 is in
You have to mention your column name in your query
If you want to check exact match you can do as follows
$var=$_GET['id'];
$select="SELECT * FROM table WHERE column_name = '$var'";
If you want to check column that contain #var you can do as follows
$var=$_GET['id'];
$select="SELECT * FROM table WHERE column_name like '%$var%'";
You must look into INFORMATION_SCHEMA Database
Provide your database name => $db
Provide your column name => $column
Then :
$sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" . $db . "' AND COLUMN_NAME = '" . $column . "'";
The easy ways is to use php to get array from all columns:
SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'YOUR-DB-NAME' and TABLE_NAME='YOUR-TABLE-NAME'
And use 'for' to create 'where'
for example:
for($i=0;$colums[$i];$i++) $where.=" or {$colums[$i]} like '%string%'";
$SQL="Select * from table where 1=1 $where";
The query will look like this:
$var=$_GET['id'];
// for checking if both the column name and value are the same $var value
$select= "SELECT ".$var." FROM table_name WHERE ".$var."='".$var."'"
OR
$var = $_GET['id'];
// for checking if both the column name and value are different
$select= = "SELECT ".$var." FROM table_name WHERE ".$var."='".$otherValue."'"
Between this is SQLinjection vulnerable code. You should check the variables before using them in query. Also you should start using mysqli and prepared statements

need rows with specific column entries in MySQL

I need to get the rows which has a specific column value .
is this is correct for it ??
SELECT *
FROM
tourDB
WHERE
tour_type = insta_deals
in this i want to get all the rows having insta_deals in the column of 'tour_type'.
You are missing " "
SELECT *
FROM
tourDB
WHERE
tour_type = "insta_deals"
^^^ ^^^
SELECT * FROM tourDB WHERE tour_type LIKE 'insta_deals'
Without using ' the value insta_deals is understood by mysql as a column name.
So your need to use ' to specify to mysql that this value is actualy a string.
SELECT *
FROM
tourDB
WHERE
tour_type = 'insta_deals'

Can I remove an apostrophe from an SQL search (WHERE clause)?

I have a query in SQL (Mysql) using a where clause.
SELECT * FROM TABLE WHERE name = 'Bristols';
Now I know that there's a row in the table containing Bristol's with an apostrophe, but not one without an apostrophe. However I want to return the row anyway. The problem is that I can only feed the query a value without an apostrophe: Bristols - is there any way within the query to remove the apostrophe from the field the query is searching?
SELECT * FROM TABLE
WHERE replace(name, '''', '') = 'Bristols'
There are several ways to accomplish this:
See Fiddle
Regex:
SELECT *
FROM cities
WHERE name REGEXP 'Bristol\'?s';
Replace:
SELECT *
FROM cities
WHERE 'Bristols' = replace(name,'\'','');
Explicit Matching:
SELECT *
FROM cities
WHERE name IN('Bristols','Bristol''s');
You have Two possible outlooks:
First:
SELECT * FROM TABLE WHERE name LIKE '%Bristol%' // Gather data like: BrISTOLS, Bristols, Bristol, Bristol's,
Second:
SELECT * FROM TABLE WHERE replace(name,'''','') = 'Bristols'

Categories