I've found that a hyphen in a value in a MySQL DELETE statement from PHP tends to make the thing just... not work. It doesn't fail, but it also doesn't delete the proper rows. Why exactly is this? INSERT works fine, it's just DELETE.
Edit: The particular query I'm running in this case (with the IP removed) is:
DELETE FROM ratings WHERE ip=IP_ADDRESS AND video = '-yfOsVrJLGt'
This runs successfully when I remove the hyphen.
I've also added that this occurs in PHP, and is seemingly limited to such.
Table structure, as per request:
There are currently no records in it.
Your video column length is varchar(10), but passing value contains 11
video = '-yfOsVrJLGs'
check your video length.varchar(10) but -yfOsVrJLGs contain 11 character.So try to give correct input
DELETE FROM ratings WHERE ip=IP_ADDRESS AND video = 'yfOsVrJLGs'
Very simple, but easily to make the mistake.
Your length of the column is varchar(10) and but needs to be varchar(11) as there are 11 characters. video = '-yfOsVrJLGs'
Related
I have a new question cause i didnt find it anywhere.
I have a db which contains 4 columns. I did my bot to insert array to a column.Now i have to fill another columns.
My filled column contains site links. Exmp: www.dizipub.com/person-of-interest-1-sezon-2-bolum-izle
I need to take "person-of-ınterest" part and write it to another column as kind of a "Person of Interest". And also "1-sezon-2-bolum" as "Sezon 1 - Bölüm 1".
I couldnt find it to do with php not sql. I need to make it with bot. Can someone help me about it please.
database
There is a column named bolumlink where i put the links. As i told i need to take some words from these links. For instance:
dizi column needs to be filled with "Pretty Little Liars" in first 9 row.
It can be done by SQL Update with Like which allows you to select rows with pattern based search using wild-cards:
% matches any number of characters, even zero characters.
_ matches exactly one character.
update your_table set dizi = 'Pretty Little Liars' where bolumlink like '%pretty-little-liars%'
NOTE:
Updating your database using like without limit or conditions with unique columns can be dangerous. This code might affect the whole table if empty string is passed.
The table (images_list is the name of the table) I have to update has over 500 rows with a certain link which I have to replace to a url connected to a local folder.
For example a field will contain www.google.com/img/test-more-text.gif and this has to be replaced to /image/test-more-text.gif. The prefix link is exactly the same for each row, the only variable part is the image name (test-more-text.gif for example is the only variable part in the example given above)
I've looked up multiple tutorials but the only things I can find replace the complete field whereas I need to keep the suffix so to speak.
This image obviously has a different name aswell so I can't simply do
UPDATE images_list
SET image_link = '/image/test-more-text.gif'
WHERE image_link = 'www.google.com/img/test-more-text.gif'
I know how to lookup text with the LIKE statement but I've never had to update something like this before.
If anyone knows how to do this that would safe me a ton of work
Use the REPLACE function:
UPDATE images_list
SET image_link = REPLACE(image_link, 'www.google.com/img/', '/image/');
WHERE image_link LIKE 'www.google.com/img/%'
In my user table I have column called SeedNumber which is declared as INT(11). In MySQL database INT is always represented by 4 bytes, and the number 11 in parenthesis does not mean that INT value will be restricted on the 11 digits. This blog explains it well.
Here is MySQL declaration for the field SeedNumber:
By using field SeedNmb, I want to fetch user. The following SQL query is used:
SELECT * FROM user WHERE SeedNmb=99999617
In my table, the value 99999617 is current maximum value. This query returns me a user without any problems, like it is demonstrated on the image below:
Recently a new user registered with SeedNumber equal 12561361. When I search the new user using the email column in the query, I get the following result:
select * from user where email like ("dennis#ca....bin.com");
Here is result:
But when I search for a user with a query:
SELECT * FROM user WHERE SeedNmb=12561361
I do not get any result, and that is a problem. The image below demonstrates my problem:
It seems like a where clause is somehow not working and user is not returned as result.
Value 12 561 361 is not greater than 99 999 617 which is current maximum value in the SeedNumber table.
Any help would be appreciated.
Have you already tried putting the number between quotes?
SELECT * FROM `user` WHERE `SeedNmb` = '12561361';
Try it in MySQL command line..
I guess something is wrong with your IDE.
I was having this same issue and fixed it by recreating an index. I noticed that if I used "like" instead of "=" the query would work. The explain plan showed that an index was used only when using "=" and not when using "like" so I deleted and recreated that index. The query started working again.
I feel like there is probably a very simple answer for this, but I've spent about 20 minutes searching and can't find anything.
Basically, I am using PHP to query a table and output the results as a list, using the primary key column (COL_1) of the table to create a link for each record that will bring the user to a detail page for that record. It works fine when the data in COL_1 is a straight-forward string such as "TEST". The edit link will then be detail.php?COL_1=TEST The detail page works by querying the database using the data passed by the link. So in this case it would do a select on the table where COL_1 = 'TEST' and return the correct record.
However, when new line characters are stored in COL_1 things get a bit complicated. For instance, if 'TEST\r\nTEST' is stored in COL_1, when the original query of the entire table is done, $row['COL_1'] for that line will give me 'TESTTEST', which gets passed to the detail page as detail.php?COL_1=TESTTEST, the detail page does a select on the table where COL_1 = 'TESTTEST', and it returns nothing.
However, if I manually link to detail.php?COL_1=TEST\r\nTEST the detail page will query on 'TEST\r\nTEST' and return the correct record.
So basically what I need is a way to do a query and have $row['COL_1'] return 'TEST\r\nTEST' instead of 'TESTTEST'. Does this make sense? How would I go about doing this?
As for why the table is set up like this, don't ask me. I didn't design it. I'd never design keys that can include line breaks like this. But I do have to interact with this table. Bah.
You should encode values that are passed in the URL:
echo urlencode("TEST\r\nTEST");
However, why would TEST\r\nTEST be a primary key? That's crazy. Maybe you need to rethink how you are doing things. Primary keys as integers work nicely.
How can I retrieve a text field from mysql db table, but not the entire text, just the few 40 or so characters.
Can this be done in sql or do I need to do it using php?
basically what I am trying to do is show the first x characters and then let the user click on that to view the full content.
SELECT LEFT(field, 40) AS excerpt FROM table(s) WHERE ...
See the LEFT() function.
As a rule of thumb, you should never do in PHP what MySQL can do for you. Think of it this way: You don't want to transmit anything more than strictly necessary from the DB to the requesting applications.
EDIT If you're going to use the entire data on the same page (i.e., with no intermediate request) more often than not, there's no reason not to fetch the full text at once. (See comments and Veger's answer.)
SELECT LEFT(MY_COLUMN, 40) FROM MY_TABLE
Function in the MySQL reference manual:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_left
try this... SELECT LEFT(field name, 40) FROM table name WHERE condition for first 40 and
SELECT RIGHT(field name, 40) FROM table name WHERE condition for last 40
You could do this in SQL as the others shown already.
But, if you also want to show the full text if the user clicks on it, you also need to full text and it seems a waste to let the database send you the short and the full text. So you could grab the full text and write some code to show the short text and the full text when the user clicks on it.
$result = mysql_query('SELECT text FROM table');
$row = mysql_fetch_row($result);
echo '<div onclick="alert(\''.$row[0].'\');">'.substr($row[0], 0, 40).'</div>';
Ofcourse you could do something nicer when you click on it (instead of alert()). Also you could do some PHP checking now to see if the original is shorter than 40 characters and handle situations like this.
Check this one as well,
mysql_query('SELECT LEFT('your text/fieldname', 40) FROM tablename');