PHP script to copy mysql column to another table [duplicate] - php

This question already has answers here:
php and mysql copy record from one table to another
(4 answers)
Closed 3 years ago.
Is there any easy way to copy one a column in a table to another table without changing the order ?
I am a beginner here. I would be much thankful to you if you could supply in depth answer.

I think this query can help you out
INSERT INTO second_table SELECT * FROM first_table WHERE id IN(1,2,3,4...n)

Related

MySQL Show Databases With Where Condition [duplicate]

This question already has answers here:
"Show databases" with condition
(4 answers)
Closed 5 years ago.
Is there a way to show a list of databases by condition?
Show databases WHERE table_name ='?' AND column_name = '?';
BEST BET:
Just itterate through all the databases, but if your trying to find a specific table why not just do this SHOW TABLES LIKE 'my_table_numer_%'; That will select a table with a begging part followed by a wild card text.

How can I fetch with PDO in order to have names of the columns in the first array position? [duplicate]

This question already has answers here:
php pdo: get the columns name of a table
(14 answers)
Closed 7 years ago.
How would you fetch a mysql query with PDO (Or after PDO) in order to have this result? I mean the "titles of the columns" in the first position of the array and the values in the next positions? Can somebody helping me with an example?
[
["ColumnTitle","Column2Title"],
["RecordValue1","RecordValue2"],
["RecordValue1","RecordValue2"],
["RecordValue1","RecordValue2"],
["RecordValue1","RecordValue2"]
]
The mysql query returns:
ColumnTitle | Column2Title
----------------------------
RecordValue1 RecordValue2
RecordValue1 RecordValue2
RecordValue1 RecordValue2
RecordValue1 RecordValue2
I don't think this question was duplicate, this was a specific case.. :(
PDO doesn't offer such a format out of the box (because no developer would like it this way), but you can make it with minor tweaking
$data = $pdo->query('SELECT * FROM users')->fetchAll(PDO::FETCH_ASSOC);
array_unshift($data, array_keys($data));
But in your place I wouldn't add column names to the data array at all.

fetching tables name from database by query in php file [duplicate]

This question already has answers here:
Select data from "show tables" MySQL query
(13 answers)
Closed 9 years ago.
Blockquote
sir i want to fetch all the table's name and information about all table as total no. of field etc with the help of single query in php file .i am using phpmyadmin as mysql please tell me how can i do it.
thanks
select * from information_schema.TABLES
You can also check :
select * from information_schema.COLUMNS

Getting all effected rown in MySQL UPDATE [duplicate]

This question already has answers here:
UPDATE/DELETE in mysql and get the list of affected row ids?
(3 answers)
Closed 9 years ago.
Title pretty much says it. I am using the deprecated mysql_ functions. I've got a query like:
UPDATE `table`
SET `row` = 'newtext'
WHERE `row` = 'oldtext'
Is there an easy way to get all effected row's primary key? Some glorious combination of mysql_insert_id and mysql_affected_rows? How can I do this without looping and doing each update one row at a time?
As a solution to your problem please refer the following sample code snippet
UPDATE `table` SET `row` = 'newtext' WHERE `row` = 'oldtext'
select id from table where row='oldtext'

How do I reference a row immediately after it has been added to a table? [duplicate]

This question already has answers here:
CodeIgniter activerecord, retrieve last insert id?
(11 answers)
Closed 8 years ago.
This is PHP / CodeIgniter / MySQL
The only way I can think of is to do the insert ($this->db->insert(...)), and then immediatly after, run another query to find the record again.
I'm hoping there is something (which seems a bit more efficient to me) that returns the primary key (or something) of the newly added record.
For codeigniter
$this->db->insert_id()
Have a look at here http://ellislab.com/codeigniter/user_guide/database/helpers.html and the first function is $this->db->insert_id();
This also works with activerecord inserts.
or you can do
$last_insert_id = $this->db->call_function('insert_id');

Categories