mysql: maybe string or bytes error when insert - mysql

INSERT INTO fb_public_figure_posts VALUES ('153080620724_10158531267690725',
'https://www.facebook.com/DonaldTrump/videos/10158531267690725/',
'2017-01-23T20:36:07+0000',
'2017-01-24T00:22:49+0000',
'Donald J. Trump',
153080620724,
'b'My family and I will never forget Friday, January 20th, 2017. Thank you!'',
'https://www.facebook.com/DonaldTrump/videos/10158531267690725/',
123471,
8263,
153080620724)
when running this sql sentence, it errors:
1064(42000), "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
'My family and I will never forget Friday, January 20th, 2017. Thank you!'','http' at line 1"
could you please help me for that

The line with the 'My family...'-sentence is a bit weird. This looks like a copy-paste-error. If this field has the type of VARCHAR you have to move the sentance in the simple apostrophe
'My family and I will never forget Friday, January 20th, 2017. Thank you!'
Your line is interpreted as
'b' : VARCHAR field
My family and I will never forget Friday, January 20th, 2017. Thank you! : some SQL-Syntax
'' : VARCHAR field
which can't obviously work.
A working INSERT would look like this:
INSERT INTO fb_public_figure_posts
VALUES (
'153080620724_10158531267690725',
'https://www.facebook.com/DonaldTrump/videos/10158531267690725/',
'2017-01-23T20:36:07+0000',
'2017-01-24T00:22:49+0000',
'Donald J. Trump',
153080620724,
'My family and I will never forget Friday, January 20th, 2017. Thank you!',
'https://www.facebook.com/DonaldTrump/videos/10158531267690725/',
123471,
8263,
153080620724
);

The problem is that you have a single quotation in the string you are trying to insert into your database. The single quotation is missing up your INSERT statement. Before putting the text into your INSERT statement, you need to escape any single quotation.
Refer to this previous post "How to escape apostrophe (') in MySql?"

Your sql is not right.'b'My family and I will never forget Friday, January 20th, 2017. Thank you!'', 'b'M...the ' is unnecessary.and ...Thank you!'' the '' is unnecessary.Hope that helps.

it's give error because sql syntax not proper it you insert data with some special character like " ' " so it's need to slashes before special character for example if you add
b'My family and I will never forget Friday, January 20th, 2017. Thank
you!'
string into database than b'My give error so you need do add slashes before ' like
b\'My family and I will never forget Friday, January 20th, 2017. Thank
you!\'
Mysql function mysql_real_escape_string automatically handle this problem so you just change your string using mysql_real_escape_string function
if you use mysqli than it's mysqli_real_escape_string function
Full Example code
$link = mysqli_connect('localhost', '***', '***');
$r = "b'My family and I will never forget Friday, January 20th, 2017. Thank you!'";
//echo $r;
$r = "b'My family and I will never forget Friday, January 20th, 2017. Thank you!'";
echo mysqli_real_escape_string($link,$r);
Sorry for my bad english

Related

Mysql convert string to time

Currently, my start_time column was string type.
I want to convert 8:00 AM to 8:00:00 using MySQL.
I have tried like this but it didn't work SELECT STR_TO_DATE('8:00 AM', '%h:%i %p')
Since you are using Laravel, I recommend to use Carbon.
Carbon is an inherited php class from DateTime what makes you able to format times in any way you want and like.
Related to your question:
SELECT STR_TO_DATE("08:00 AM", "%h:%i %p"); // output: 08:00:00
Works fine, so there is something else that causes your problem, but you are giving us not enough information to help you further.
What db are you using?
What version?
what outpout do you get?
etc.

MySQL – replace part of strings ending with '/'

I have very little experience with SQL. I am stuck with seemingly very simple task – I've copied a row with contract numbers, which includes a two-digit year number (EG 1/14, 31/15, 221/17) into a new "year" row.
Now I need to replace the digits left from the forward slash and slash itself with '20' string to get pretty full year (EG 2014, 2015, 2017). The row is VARCHAR.
I was trying this:
UPDATE contracts SET year = REPLACE(year, '%/', '20')
or this:
UPDATE contracts SET year = REPLACE(year, '*/', '20')
Which did not throw error, but did not update anything (0 rows affected…).
I was trying even regexp, but this throws error, so obviously the syntax must be wrong:
UPDATE contracts SET year = REPLACE(year, '.+/\', '20')
Any help would be appreciated.
MySQL does not support regular expressions with replace(). Based on your sample data, you can do:
UPDATE contracts
SET year = CONCAT('20', SUBSTRING_INDEX(year, '/', -1))

How to convert string to time in sql query

I have data in table "2:00 PM-3:00 PM"
Here, I want to print start time based on AM PM.
For example print start time(2:00PM) as number 14:00.
Can you please suggest sql query for this.
Simply you can do it using date_format()
$date = new \DateTime('2:00PM');
echo date_format($date, 'H:i');
// Output: 14:00
But if you're getting this information from the DB you can do it on query-level but the way of doing it depends on which database you're using.
For example with mysql you can do it using DATE_FORMAT().

Save a message with ' and " in mysql DB

I have a column message_txt varchar(4096) in a table.
I want to stock in this field a message like this :
"Hi,
My name is Aron and I'm from Canada.
My favourite Dj are "Alpha²" and "Ran-D"."
So when I do my query it says I got an syntax error, so I think it comes from the ' and the ". So my question is how can I stock/save them ?
My query :
mysql_query('
INSERT INTO MESSAGE (id_message,message_txt,auteur,date_creation,date_last_modif,statut)
VALUES
(NULL,"'.$_POST['message_sujet'].'","'.$_SESSION['Pseudo'].'",now(),now(),"Normal")
') or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
where $_POST['message_sujet'] is the message above.
Since you're using PHP and the deprecated mysql_* extension too, you're best off with mysql_real_escape_string:
mysql_query('
INSERT INTO MESSAGE (id_message,message_txt,auteur,date_creation,date_last_modif,statut)
VALUES
(NULL,"'
. mysql_real_escape_string($_POST['message_sujet'])
. '","'
. mysql_real_escape_string($_SESSION['Pseudo'])
. '",now(),now(),"Normal")') or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
Note
You should consider using PDO or mysqli with parameterized prepared statements and binding of your input values to the parameters instead. Then there's no worry about escaping values.
If you're writing SQL statements manually then you can escape your single quote either with a backslash or by doubling it:
'"Hi, My name is Aron and I\'m from Canada. My favourite Dj are "Alpha²" and "Ran-D"."'
'"Hi, My name is Aron and I''m from Canada. My favourite Dj are "Alpha²" and "Ran-D"."'
Both will work.

Mysql queries giving error after upgrading

why this simple query not working. I know it was OK before I upgraded my mysql version. i know there are some syntax changes in new version that I done. but this is simple query no join, but not working.
SELECT * FROM ship WHERE sensitive='Y' and entry_date between $startdate and $enddate
Please help me.
EDIT:
Upgraded from mysql4 to mysql5 and error is 'syntax error'
I think you upgraded from mysql 4 to 5 . sensitive is new reserved keyword in mysql5. You need to enclose it in backticks(`) while using it in query.
Mysql 5 allows reserved words to be used is query but with backticks.
Try:
SELECT * FROM ship WHERE `sensitive`='Y' and entry_date between $startdate and $enddate
You have "space" character in the '$start date'. That might be one reason. So, if your $startdate and $enddate are timestamps:
SELECT * FROM ship WHERE `sensitive`='Y' and entry_date between $startdate and $enddate
if they are not timestamps, but date in iso format, but them in single quotes.