how to delete particular number of rows in a table using php - php

if i select these particular rows using
select * from wp_posts where title like 'abc%'
it displays the rows
now i want to delete these rows using php
Can i do this or iam wrong
thanks in advance

It would be more interesting if you inform your code a little more complete. But basically the SQL command is as follows:
delete from wp_posts where title like 'abc%'

If there is sensitive data like activation keys or something, you need to use binary function of mysql to check whether abc is matching with ABC or not.
Use this one:
delete from wp_posts where title like binary('ABC%')

Related

sql statement syntax for search

I would like to write an sql statement for search. Here is sample database structure.
For eg, I want to display all records with topic '13'. How can i write sql query for searching 13 from the above structure? Any suggestions?
Can i able to use WHERE Topic LIKE '%13%'? Anything wrong with this?
Try this one:
SELECT * FROM `TABLE_NAME` WHERE `Topic` LIKE "%13%";
It's better and faster to save it in a third table of many-to-many relationship.
If you want to save as per your example (single table), try to save data as eg ",10,13,15,"
always have coma before and after, thus the following sql will exclude 213 and 132 etc
select * from table_name where Topic like '%,13,%'
select * from table where find_in_set("13",topic);
or if topic is not used as a set, you could do ...
select * from table where concat(",",topic) like "%,13,%";
The 2nd isn't real elegant but I've had to do that a couple times.
Because the data isn't really normalized, I used concat to add a comma to the topic field so I could make sure the "like" comparison would pass with a comma before and after the value. I suppose we would also have to remove any unwanted spaces as well for this example, so ultimately it would end up like:
select * from TABLE where concat(",",replace(topic," ","")) like "%,13,%";
Ultimately, we have to know what to expect in the topic column to come up with a query that would always work. In the past, I've had situations where I would add values to a string field (i.e. topic) with a delimiter before and after each value like:
(1)(2)(3)(14)(15)(255)(283)
If you did something like this for the topic field, the query is simple ...
select * from table where topic like "%(13)%";

Mysql query using LIKE operator return no result

I am using php my admin to test my query and when I test a simple query like:
SELECT * FROM song WHERE title LIKE '%anytitle%'
It returns an empty result. With any title it gives me the same thing.
If i try the same thing on my other table users
SELECT * FROM users WHERE email LIKE '%gmail%'
It works and return all record containing gmail.
The table are built the same, utf8_unicode_ci and VARCHAR.
What could be the problem?
edit:
I destroyed my table and I have rebuild it.I have executed the query with just one row in the table and it worked.
I've repopulated the table and now it does not work.
I have 5029 records in the table.
Would it be a setting somewhere?
Re edit:
When I try like
SELECT * FROM song WHERE title LIKE '%d%'
It return every record where there is a d inside the title name. But if I try with more than one letter it does not work, I have no result.
I've updated mysql, I've unistalled it and installed version 5.6 instead and still the same issue.

fulltext search method didn't work

i tried to build a search engine in my php script using full text feature,but it didn't get any results.
steps i did:
i altered a 2 fileds to full text
ALTER TABLE stack_ask ADD FULLTEXT(q_title,q_body)
then i added some data
INSERT INTO articles (q_title,q_body) VALUES('MySQL Tutorial','DBMS stands for DataBase ...');
then i made a variable to recieve a post data from input
$q_title=$db->real_escape_string($_POST['search_word']);
then the query:
SELECT * FROM stack_ask WHERE MATCH (q_title,q_body) AGAINST ('$q_title');
when i tried to search something like database it didn't give any result and output the following error:
call to amember function fetch_assoc on a non-object
what's the problem?
Hi Mohamed amin,
Make sure that q_title,q_body has indexing or not. Read this article
may it will be useful for you.
http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html
after multiple trials,i solved the problem by the following:
1-i checked the index type of 2 columns using the following sql:
show index from stack_ask
and they were full text
2-i checked the table type ,which was MYISAM
2-the correct sql query should be like the following:
SELECT *, MATCH(q_title,q_body) AGAINST ('$q_title') FROM stack_ask ORDER BY id DESC
not:
"SELECT id, MATCH(q_title,q_body) AGAINST ('$q_title') FROM stack_ask ORDER BY id DESC

php search a mysql string results

guys just a quick question. I have a string in a mysql row resembling this
'google.co.nz, stackoverflow.com, facebook.com, grubber.co.nz'
and so on... I would like to search all the rows in the table and check how many times 'facebook.com' shows up in all the rows that are like the ones above so to clarify
my rows look like this
-- id -- user -- likes
1 bob facebook.com, google.co.nz, grubber.co.nz, stackoverflow.com
and i would like to check how many times facebook.com shows up in the whole table (in every row)
Assuming every user can only like the same page once, this should work:
SELECT COUNT(*)
FROM table
WHERE likes REGEXP '[[:<:]]facebook.com[[:>:]]'
PS: Normalize your table, you will run into serious trouble with a layout like this in the very near future!
You could do it in a lazy fashion, issuing a LIKE SQL query :
SELECT count(*) FROM my_table
WHERE my_table.likes LIKE '%facebook.com%'
This is really (REALLY) not cpu friendly. Especially with large tables
Otherwise, you could use MySQL fulltext indexes feature.
You may find more details in this article
you could use the "LIKE" statement.
It goes like this (if you want to know which user likes facebook.com)
SELECT * FROM yourtable
WHERE likes LIKE '%facebook.com%'
Anyway, please consider storing the likes in a 1:n table construct and not as a string
By that you have a a table storing all like-able sites, a user table and a user-likes table storing the assignment.
Other option. Get the row data in a variable and then use the function preg_match_all (http://www.php.net/manual/es/function.preg-match-all.php).
This function returns the number of occurrences of the regular expression passed in the first parameter.
Try this
select count(likes) from yourTable where likes like '%facebook.com%'
You can use the like operator to match the pattern:
SELECT COUNT(id) AS occurences WHERE likes LIKE "%facebook.com%"

PHP MYSQL Query Array Field

Ok. I'm using MySQL and PHP. I have a table called "Pictures" and a field in that table called "Tagged." This field is an array of UserIds which I imploded and stored as a string.
example: 114,159,14,334
Lets say I want to return all the rows in "Pictures" where a particular UserID, say 14, exists somewhere in the "Tagged" field's array. How can this be done?
select * from pictures where FIND_IN_SET(14,Tagged);
Start it with a , and end with a , like this: ,114,159,14,334, and then do a SELECT FROM Pictures WHERE tagged='%,14,%'
Just an idea, I really don't know... I've never done this before. Traditional blog tagging may be similar enough that you could look up some tutorials to learn from them.

Categories