Is there a logical (and possible) way to do something like this?
UPDATE $table SET LIKE %_checkbox = '' WHERE id='$id'
I have fields like allowed_checkbox and types_checkbox and they are sent to the database script dynamically. Can you use a wildcard when referring to the column name?
You've got a bit of a Frankenstein syntax there. The server will need to know the table and column names before compiling the SQL - so you can't do what you're after directly.
Does your php code have no prior knowledge of the database schema?
The key word you used is dynamically - you could find matching column names using a query against the MySQL INFORMATION_SCHEMA.COLUMNS table. You could do this per-update, which would be expensive, or once at application start up extract the schema for all tables you need.
No. You would have to generate the SQL string and then execute it separately. If you're trying to do something like this then you've probably got a bad schema design.
Related
I am new with PHP development and just wondering if theres a existing function on PHP than duplicate the copy command on phpmyadmin, i know that the query sequence is below, but this is like a long query/code since the table has alot of columns. i mean if phpmyadmin has this feature maybe its calling a build in function?
SELECT * FROM table where id = X
INSERT INTO table (XXX)VALUES(XXX)
Where the information is based from the SELECT query
Note: The id is primary and auto increment.
Here is the copy command on phpmyadmin
i mean if phpmyadmin has this feature maybe its calling a build in function?
There is no built-in functionality in MySQL to duplicate a row other than an INSERT statement of the form: INSERT INTO tableName ( columns-specification ) SELECT columns-specification FROM tableName WHERE primaryKeyColumns = primaryKeyValue.
The problem is you need to know the names of the columns beforehand, you also need to exclude auto_increment columns, as well as primary-key columns, and know how to come up with "smart defaults" for non-auto_increment primary key columns, especially composite keys. You'll also need to consider if any triggers should be executed too - and how to handle any constraints and indexes that may be designed to prevent duplicate values that a "copy" operation might introduce.
You can still do it in PHP, or even pure-MySQL (inside a sproc, using Dynamic SQL) but you'll need to query information_schema to get metadata about your database - which may be more trouble than it's worth.
I have problem for dropping database table when table name contains only numbers. So, my implementation works correctly for cases where database table contains letter+numbers.
This is my implementation:
$this->dbforge->drop_table($table_name);
I've tried to use back-tick(`) and my query looks like:
DROP TABLE `234`
Also i tried using single quote(') and this also didn't work.
I know that its bad practice to use names for tables and columns that contains only numbers as their name, but i want to know why i can dynamically create this type of table, but cant drop them.
Any suggestion would be appreciated.
I tested this and works just fine. Let me know if it is something you have tried.
Also tested this and also works. What version of CodeIgniter are you using?
I'm writing a piece of software that generates a pdf report out of a raw user-defined SQL query execution.
The pdf contains a simple table with rows containing SQL result rows. I'd like to add a table header with column names retrieved along with the SQL results.
The column headers in SQL may have various structures, e.g:
a) select * from users;
b) select name, surname, email from users;
c) select name as UserName, surname as UserSurname, email as UserEmail from users;
So far I fetch SQL results as association array, take the keys of the first row and treat them as column names.
It works only if there is at least 1 result in result set, so it's a heavy flaw in this approach.
I could generate pdf with "No results" label.
I could run a regex on a SQL query for named columns and execute describe table x, but this is plain ridiculous.
I also have even more ridiculous ideas, but that's not the way.
Is there anybody having any idea for solving this?
I use Doctrine on MySQL for this, but simple PDO approach would be just as good as Doctrine's one.
EDIT
Right after posting this question it came to my mind I could generate a view out of my SQL query, then run SHOW COLUMNS FROM randomViewName; and drop the view immediately afterwards.
It's hacky and needs some db security work (I can handle that), but it's a working candidate.
What do you think?
It may not be perfect solution, but I go along with the approach mentioned in the question.
I create a MySQL view. This allows me to have an db object which is queryable and have exact column names as I want.
describe nameOfViewYouJustCreated;
gives me exactly what I need.
The view is dropped afterwards.
I have imported a SQL database from an Excel sheet, so it's a little bit messy. For example there's a product field with VARCHAR values such as product8. I would like to grep through these data using some regex, capture the id in this example, and alter column data types. As of now I would start preg_matching the long and hard PHP way, and I'm curious how a database normalization is done right using SQL commands. Thanks for your support in advance.
you can select case, to pull the ids
select right(product,length(product)-7) as productID from table
this will pull the numbers, then you can do whatever
I have a MySQL field called user_tags, and I want to insert a string with commas like this:
string1
string1,string2
string1,string2,string3,......
How do I update the field dynamically using a query?
Please read about Mysql set DataType http://dev.mysql.com/tech-resources/articles/mysql-set-datatype.html
But this is the concept:
UPDATE set_test SET myset = CONCAT(myset,",Travel")
WHERE rowid = 3;
No you don't, that violates first normal form which says "Every row-and-column intersection contains exactly one value from the applicable domain". Also, it's not the right way of doing it; it's not how SQL databases are designed to work.
MySQL's sets don't solve it either (they also violate 1NF) because the set permitted values are effectively fixed (can only be changed by ALTER TABLE).
What you really want, is another table associating tags with users. It's dead easy.
Once you redesign your table this way, you can add a new tag with a simple INSERT.