I'm trying to insert a variable into mysql table, but it does not do what i want (no error, it simply does not work)
$string = 'this is an example';
mysql_query("insert into tablename (columnname) values ????");
Anyone can help me how to insert $string into table tablename? I tried replacing ???? with ($string), (".$string."), $string, but all of them does not insert the $string variable into the database.
Thanks in advance!
You can use \" (an escaped quotation mark) to include quotes inside your query, like so:
$string = 'this is an example';
mysql_query("insert into tablename (columnname) values (\"$string\")");
you forgot the quote (') and you should use double-quotes a your string delimiter.
$string = "test abc";
mysql_query("insert into t (col) values ('$string')");
Related
I've field in DB table which has string values as "principaux de l\'asthme : facteurs psychologiques, é-; blabla é tiques à-; blabla".
I want to replace string "é-;" with "é" and "à-;" with "à".
Can I have query for it?
Check With This:
REPLACE(Column_name,'K','SA')
Column_name : Name of your Column
'K' - The characters which you want to replace
'SA' - The Characters with which you want to replace
If you have to repeat this for multiple strings use cascaded Replace function such as
REPLACE(REPLACE(REPLACE(Column_Name, '3', 'test') , '2', 'test') , '1', 'test')
I have to change s-Gravenland into 's-Gravenland in my database
I use find and replace,
UPDATE Voornamen_2 SET Buurt = REPLACE(Buurt, 's-Gravenland', ''s-Gravenland');
This doesn't work obviously, how do I insert the extra ' ?
Use double single quotes to represent a single quote in a string:
UPDATE Voornamen_2
SET Buurt = REPLACE(Buurt, 's-Gravenland', '''s-Gravenland');
Use an extra single quote char to accept it as part of data.
UPDATE Voornamen_2
SET Buurt = REPLACE( Buurt, 's-Gravenland', '''s-Gravenland' );
Or, you can also escape it to include with the data.
UPDATE Voornamen_2
SET Buurt = REPLACE( Buurt, 's-Gravenland', '\'s-Gravenland' );
Below query should work:
UPDATE Voornamen_2
SET Buurt = REPLACE(Buurt, "s-Gravenland", "'s-Gravenland");
I am trying to insert data into a MySQL database:
$response = $client->fql->query(
query => '
SELECT name, email, birthday, username, first_name, last_name, pic
FROM user
WHERE uid = me()
',
);
print join "\n Name:", sort map { $_->{name} } #$response;
$dbh->do("
INSERT INTO Users(SNo,Name,Email,Birthday,UserName,FirstName,LastName)
VALUES(1,
sort map { $_->{name} } #$response,
'imm\#gmail.com',
'1987/12/10',
'imm',
'imm',
'Dee')
");
$dbh->disconnect();
used the mysql query in one line.
This above print statement is printing the name correctly but why the above sql insert statement is not working?
I connect the db and after that i am receiving the value and printing in the browser is working.
Why does the mysql statement not accept the value?
When inserting the database is not working?
You should have a look at the official doc
and specially this :
# INSERT some data into 'foo'. We are using $dbh->quote() for
# quoting the name.
$dbh->do("INSERT INTO foo VALUES (1, " . $dbh->quote("Tim") . ")");
# Same thing, but using placeholders
$dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 2, "Jochen");
$query = "INSERT INTO $stats_table_name (name, name_ID, anz_aufruf) VALUES ($plan_name, $plan_nr, $anz)";
echo "<br />".$query."<br />";
if (!mysql_query($query) && !$error) {
die (mysql_error());
}
mysql-error tells me:
INSERT INTO 'p_stats' ('name', 'name_ID', 'anz_aufruf') VALUES ('Laptop 1', '1', '95')
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 ''p_stats' ('name', 'name_ID', 'anz_aufruf') VALUES ('Laptop 1', '1', '95')' at line 1
Where is the code wrong here?
the error is the wrapping of single quotes around table name.
Table names as well as column names are identifiers. They should be wrap if a name is a reserved keyword with backtick (optional if not). Single quotes are for string literal.
MySQL - when to use single quotes, double quotes, and backticks?
Use this.
$query = "INSERT INTO $stats_table_name (name, name_ID, anz_aufruf)
VALUES ('".$plan_name."', '".$plan_nr."', '".$anz."')";
$query = "INSERT INTO $stats_table_name (name, name_ID, anz_aufruf)
VALUES ('".$plan_name."', '".$plan_nr."', '".$anz."')";
I'm trying to do a friend search app.
So to do that I'm using regular expressions like:
SELECT ...
WHERE firstname REGEXP $firstname
And the $firstname variable is a string - for example:
(ch|k)ris
But for some reason it does not work. maybe the ( | ) doesn't work in MySQL?
What can I use instead of that operator to do the same thing?
in PHP modify your line to this:
$query = "SELECT ... FROM users WHERE id NOT IN($alreadyfriend) AND firstname REGEXP '$firstname'"; //notice the single quotes