I am trying to drop a table in a database with the following query statement:
mysql_query('DROP TABLE IF EXISTS "dbName.tableName"') or die(mysql_error());
But I keep getting an error. Does anyone know if specifying the dbName.tableName is invalid?
mysql_query('DROP TABLE IF EXISTS `dbName`.`tableName`') or die(mysql_error());
You should use backticks instead of double quotes like this:
mysql_query('DROP TABLE IF EXISTS `dbName`.`tableName`');
You can't use double quotes to quote db/table names, instead you either leave them unquoted or use backticks. But to answer your question, yes it is perfectly valid to specify the database name.
DROP TABLE `dbName`.`tableName`
Related
I have a table which name is data with column name First Name
I am trying to insert data in table but unable to do it as column name is with blank space.
INSERT INTO data(First Name) VALUES('Sample');
How can i do it, please explain.
You need to use backticks to encapsulate the field names
INSERT INTO data(`First Name`) VALUES('Sample');
Remove space from you column name First Name should be FirstName.
INSERT INTO data(FirstName) VALUES('Sample');
Or your column contain space then you can use like
INSERT INTO data([First Name]) VALUES('Sample');
I have this code that does not work, and im not sure why...
if(isset($_GET['id'], $_SESSION['username'])){
$id = $_GET['id'];
$user = $_SESSION['username'];
$query = $handler->query("INSERT INTO photolikes('User', 'Photo')
SELECT '$user', '$id'
WHERE NOT EXISTS (SELECT Id FROM photolikes WHERE User='$user' AND Photo=$id)");
}else{
}
Is just supposed to insert user and photo into a table if there is no such in there before... thanks for any help!
The SELECT is missing the FROM clause which is required when a WHERE clause is used.
That's the problem.
There's a couple of ways to fix it.
For a quick fix, you can add FROM DUAL before the WHERE.
If you don't like your MySQL queries looking like they are Oracle queries, you can use an inline view as a rowsource.
In place of FROM DUAL you could use FROM (SELECT 1) i.
That's the less-Oracle-more-MySQL-like way of fixing it. That's how I would do it.
You could also reference any table or view that you are guaranteed returns exactly one row. (It can't be zero rows, and it can't be two rows.
A couple other notes:
In MySQL, identifiers (for example column names) can be escaped with backtick characters, but not single quotes. Identifiers only need to be escaped if they contain characters that aren't allowed (in unescaped identifiers) or if the identifier conflicts with a reserved word.
INSERT INTO photolikes(`User`, `Photo`)
^ ^ ^ ^
Also, the code appears to be vulnerable to SQL Injection. Potentially unsafe values that are incorporated into the text of a SQL statement should be properly escaped. But an even better pattern is to use prepared statements with bind placeholders.
INSERT INTO photolikes(`User`, `Photo`)
SELECT '$user', '$id'
FROM <someTable>
^^^^ you miss the FROM
WHERE NOT EXISTS (SELECT Id
FROM photolikes -- Here you didnt forget.
WHERE User='$user' AND Photo=$id)")
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
I have a table with a primary column "MatchId", followed by many other columns.
Unfortunately, I can't seem to get my insert/update query right: Even if I only want to insert an MatchId (Not auto-increment by the way), I get the error Unknown column in 'field list'...
Here is my query:
INSERT INTO `stats` (`MatchId`) VALUES (`123456`);
How do I insert something in this table without getting this error?
You have the wrong types of quotes around the value. Backticks are used around table and column names. To quote a string, use single or double quotes:
INSERT INTO `stats` (`MatchId`) VALUES ('123456');
If it's an integer, you don't need to quote it at all:
INSERT INTO `stats` (`MatchId`) VALUES (123456);
Putting a value in backticks forces it to be treated as a column name, even though it has the syntax of a number. Backticks are the way that MySQL allows you to use column names that have unusual syntax.
Test it in phpmyadmin the unrecognised field is "123456". Change your SQL and wrap the value in single quotes
This is properly going to be really obvious but been driving me insane for far to long.
I have a bunch of tables in a MySql database, example of these are
//Table Names 000001_table1, 000001_table2, 000002_table1, 000002_table2, 000003_table1, 000003_tabel2
In php i can programmatically create these tables
$db->query("CREATE TABLE {$tablenum}_table1 (ID INT PRIMARY KEY AUTO_INCREMENT, COL1 TEXT, COL2 TEXT, COL3 TEXT)")
However when i come to read these tables I'm getting issues
Using the following query
$db->query("SELECT COUNT(*) from {$tablenum}_table1")
I get the error
Table 'database.000001' doesn't exist[]
If i put backspaces around it, i get the error
Can't find file: '.\database\000001#000d#000a_table1.frm' (errno: 22 - Invalid argument)[]
If i try constructing the table name before the query i get the same issue.
Any ideas
Thanks
Use backticks around your tablename:
$db->query("SELECT COUNT(*) from `" . $tablenum . "_table1`")
Hope it helps.
looks like your {$tablenum}_table1 converts it to
{$tablenum}._table1
(an extra dot is inserted and he thinks it is 00001.table1 which means:
table1 FROM database 00001
Try to use the variable tablename like this:
$db->query("SELECT COUNT(*) from ${tablenum}_table1")
INSERT INTO `test` (`x`, `y`) WHERE `id` = `$id`
VALUES (`$x`, `$y`)
Whats wrong with this query? I run this in a mysql_query() function in a php file.
You can't use a Where clause with an insert. You are either inserting a row or you're not.
If you're trying to update information from the database, use UPDATE instead of INSERT INTO in the query you're running.
You can't use a where clause on an insert. I think you might be wanting an update statement:
update test set
x = $x,
y = $y
where id = $id
When you're inserting a new value in the database, you usually don't have an ID value until after the insert (assuming you're using auto-generated IDs).
You need to remove the "WHERE id=$id" - it has no meaning in an INSERT statement.
So, either
UPDATE test SET x='x', y='$y' WHERE id ='$id';
OR
INSERT INTO test ('x', 'y') VALUES ('$x', '$y');
as stated in other posts - you cannot do an INSERT with a WHERE.
Also note that you must use single quotes (') rather than backticks (`) for values. Backticks are used when referencing column names / field names.
This:
`field` = '$value'
rather than this:
`field` = `$value`
unless you really do want to reference another field. This is sometimes what you want when copying values or matching JOINs and such. But because you have a variable there, I'm assuming you want to use single quotes rather than backticks.