I have a table category which has two filed id (auto increment) and catname (string)
In some particular entry it has entered my user like
id | catname
1 | \\
2 | abcd
3 | \\
4 | >> asd
5 | \\
6 | \\
7 | \\
I want to delete this \\ entry but it is not found by select query
How do I delete this \\ entry ?
And I also use 'php' so any way to delete \\ entry by php or by phpmyadmin panel please help me.
You can use this query:
DELETE FROM `cat` WHERE `catname` = '\\\\';
Fiddle: http://www.sqlfiddle.com/#!2/ef33d/1
you can also check the values at the time of post by using "trim" or "str_replace"
trim will remove all of your whitespaces and and other characters at the beginning of the string
or you can use str_replace
The str_replace() function replace all occurrences of the search string with the replacement string
"mixed str_replace ( mixed $search , mixed $replace , mixed $subject <, int &$count > )"
This function works by the following rules:
If the string to be searched is an array, it returns an array
If the string to be searched is an array, find and replace is performed with every array element
If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as replace
If find is an array and replace is a string, the replace string will be used for every find value
Examples:
<?php
echo str_replace("world","Peter","Hello world!");
?>
output: Hello Peter!
<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "Replacements: $i";
?>
output: Array
(
<0> => blue
<1> => pink
<2> => green
<3> => yellow
)
Replacements: 1
or you can use regular expression "preg_replace" for that you have to learn regular-expresions for details on regular expression follow the link
http://www.php.net/manual/en/ref.pcre.php
1st way to delete from database is:
DELETE TABLE `TABLE_NAME` WHERE `CATNAME` = '\\';
This deletes all rows which have '\' entry.
DELETE TABLE `TABLE_NAME` WHERE `CATNAME` LIKE '\\';
Related
Here is the query to get all columns of a single row
$STH = $DBH->db->prepare("SELECT * FROM Settings");
$STH->execute();
$STH->setFetchMode(PDO::FETCH_ASSOC);
$str = '';
while ($row = $STH->fetch())
{
foreach($row as $key => $value)
{
$str .= '<div>'.$key.' => '.$value.'</div>';
}
}
and this one is to get extended information of the column (including comment)
$STH = $DBH->db->prepare("SHOW FULL COLUMNS FROM Settings");
$STH->execute();
$STH->setFetchMode(PDO::FETCH_ASSOC);
\* the same php code *\
can I combine these two SQL queries to get the following data simultaniously
`FieldName`, `FieldValue`, `CommentValueOfColumn`
'
'
'
example of Settings table
Columns -> | PrintImage | SettMinus | HasChat . . .
--------------------------------------------
Values -> 0 1 1 . . .
and there is additional data Comment of the column.
So I want to select data:
PrintImage, 0 , Comment of PrintImage;
SettMinus, 1 , Comment of SettMinus;
HasChat, 1 , Comment of HasChat;
.....................................
and so on.
This will give you the column name, values of the columns and the comments. However, you can use php to get the values which is
VALUEARRAY[ORDINAL_POSITION]
You also need to copy/paste the rest of the column names after concat_ws.
Again, I cannot do everything in mysql so it needs help from PHPcodes to get the exact value from the array.
SELECT s.COLUMN_NAME,
s.ORDINAL_POSITION,
t.valuearray,
s.COLUMN_COMMENT
from INFORMATION_SCHEMA.columns s
cross join (select concat_ws(",", PrintImage,
SettMinus,HasChat) as valuearray from settings) t
where s.TABLE_NAME='Settings';
sample result :
COLUMN_NAME ORDINAL_POSITION valuearray COLUMN_COMMENT
PrintImage 1 0,1,1 commenttest
SettMinus 2 0,1,1 comments too
HasChat 3 0,1,1 3rd comment
I have Table as Production with field name as production_code
Production_code have value like this,
Id production_code
1 P101,P102,P103,P105
2 P103,P106,P102
3 P104
4 P102,P105,P111
------ I have value on PHP page like $p_code='P102,P109';
Now I want to fetch rows from Table Production_code where any code is exist in production_code of variable $p_code
please help me .. what mysql query should i use
You can use function FIND_IN_SET, like:
SELECT * FROM yourTable
WHERE FIND_IN_SET('P102', production_code) OR FIND_IN_SET('P109', production_code)
Solution.
<?php
$p_code='P102,P109'; //example
$condition = '';
foreach(explode(',',$p_code) as $r){//explode convert $p_code string into array. then apply foreach for compare every element of array From database saved value
$condition .= 'FIND_IN_SET("'.$r.'", production_code) OR ';
}
$condition = rtrim($condition,' OR '); //this remove last occurence of OR from condition
echo $sql = 'SELECT * FROM yourTable
WHERE '.$condition;
?>
I use my SQL as backend for my project, and I need to get all record from database where some part of inputted string is available in database string like:
table = seller
id company_name seller_name
1 companyname1 seller1
2 companyname2 seller2
3 companyname3 seller3
4 companyname4 seller4
Given string is 1105 companyname1 is outstanding
So i need to get id = 1 for a given string if it is possible with laravel or MySQL then please help me.
You can construct a query using like:
where $YourString like concat('%', companyname, '%')
Note: There are situations where one company name might be sufficiently like another ("company1" and "company10"). If this is an issue, regular expressions might help.
First, you have to convert that string to an array.
$str = "1105 companyname1 is outstanding";
$str = explode(' ' , $str);
$results = Seller::whereIn('company_name' , $str)->get();
Note:
As you are converting random strings to an array, there will be a mistake where the user input some more spaces.
Let say the user input a string like $str = "1105 companyname1 is outstanding"; this will create some more elements. Like
array(
0 => '1104',
1 => '',
2 => 'companyname1',
3 => 'is',
4 => 'outstanding'
)
So, to avoid that, I have to recommend you to split out some more spaces. You can do by
$str = preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);
I'm working now on some dictionary and i'm downloading source from multiple dictionaries into mysql database.
I have two tables:
Words
ID and Word
ie. VALUES:
123, "Hakunamatata"
332, "Boom"
Source
ID, WordID and Numerical ie. VALUES:
1, 123, 7676552
2, 332, 651365
Now, i would like to update data in Source this way:
find ID in Words WHERE Word = "example"
put datum (WordID, "Number") into Source
BUT
If there is no such Word in Words - create it.
I need to do about 100000 of queries of this type, but it does not need to be very fast ;)
I've tried to do something like this:
REPLACE INTOsjp-dict.Words
LEFT JOINsjp-dict.Source-Wiktionary
ONWords.ID =Source-Wiktionary.WordID
SET
Word= IF(WordIS NULL, "Apulia",Word),
WordID= IF(Source-Wiktionary.WordID IS NULL ANDWord= "Apulia",Words.ID,WordID),
WikiWordID= IF(Word= "Apulia", 123, WikiWordID)
But it does not work...
Done with two sql commands:
$sql = 'INSERT IGNORE INTO Words (Word) VALUES '. $sql_words;
$sql2 = 'INSERT IGNORE INTO "Source-Wiktionary" (WordID, WikiWordID) VALUES '. $sql_values;
Where WordID is definied by
SELECT ID FROM Words WHERE Word = "'. strtolower($cma[2]). '" LIMIT 1
Here I want to search like
I have a string say $var = "1,3,5,7"
And
in my table say table student there is a column say column_abc
And this column contain values like
column_abc
2,3,45,6
1,3,4,5,8
3,4,6,9
1,5,10,13,34
I want to search $var against that column column_abc
can anybody help me
In result I want rows which contain any number present in $var
Am I right that you want to search in the table students for the value of column_abc? In that case you can use the following PHP code:
$result = mysql_query("SELECT * FROM `student` WHERE `column_abc` = '" . mysql_real_escape_string($var) . "'");
Hope this helped.
This solution may work for you ...
where column_abc regexp '(^|,)$var($|,)'
If $var is equal to 2, then it will find it twice in the below scenario:
2,3,45,6,12
1,2,3,4,5,8
3,4,6,9
1,5,10,13,34
EDIT: Just re-read your question, and you were looking for an entire string of numbers, and not just one ....
declare #search int(10)
declare #Search2 int(10)
Set #Search = '1,2,3,4'
Set #Search2 = #Search+'%'
select * from student where _abc like #Search2