sql - How Can I truncate Multiple Tables in MySql? - php

$tblarray = array("abc1","abc2","abc3");
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE table_type ='base table' and TABLE_CATALOG='mydb' order by TABLE_NAME";
$res = mssql_query($sql);
while($rowTable = mssql_fetch_array($res))
{
if(in_array($rowTable['TABLE_NAME'],$tblarray))
{
$sqlDel = "SELECT DISTINCT concat('TRUNCATE TABLE',TABLE_NAME,';')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'abc%' AND TABLE_SCHEMA = 'mydb'";
$res_sqlDel = #mssql_query($sqlDel);
}
}
First I get all the tables name from database then truncate specific tables. But this code not working properly.

select Table_name from information_schema.tables where table_schema='YOUR_DB_NAME';
Will return all the table names
1) Iterate over the result set.
2) Create another sql query like
$query= "truncate table ".$resultSetRow['Table_name'];
Execute it in the loop. Basically it will iterate over result set and truncate all the tables one by one.

Related

How to find the names of all tables created in the last SQL statement using PHP?

I have some code like the following:
$import = "CREATE TABLE foo..."; // an SQL import containing some CREATE TABLE, ALTER TABLE, etc.
$result = $mysqli->query($import) or die ($mysqli->error);
How do I find the names of all the tables that were created in the last SQL query (i.e. the $import statement)? I was wondering if there was a special way in MySQL to do this, rather than just searching the statement for CREATE TABLE, which is kind-of a dirty hack.
use the information_schema.TABLES
$time = time();
//create tables here
...
select table_name, create_time
from information_schema.TABLES
where create_time > $time
where table_schema = 'andomar'
order by CREATE_TIME desc

Retrieve database table comments in CodeIgniter

I have created a database table in PHPMyAdmin with table comments. I plan to use the table comments to store table version. I want to retrieve the table comments in CodeIgniter database mysql driver.
I tried to execute the query via the query method:
SELECT table_comment FROM INFORMATION_SCHEMA.TABLES WHERE table_schema='{$table}' AND table_name='{$database}';
but the it returns nothing. Is there any workaround or proper way to do this?
Thank you!
Try this
$query = "SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_SCHEMA = '{$database}' AND TABLE_NAME = '{$table}'";
$result = $this->db->query($query);
print_r($result);
And your query is not executing because you have a typo within your query
TABLE_SCHEMA = '{$table}'
needs to be
TABLE_SCHEMA = '{$database}'
and
TABLE_NAME = '{$database}'
needs to be
TABLE_NAME = '{$table}'

Select all data from a table and insert it to another table

I wanted to select all the data from one of my table and insert it to another table. I have this code but it wasn't working..This is what the phpMyadmin says "MySQL returned an empty result set (i.e. zero rows). (Query took 0.0010 seconds.)"
Here is my code:
if(isset($_POST['select_table']))
{
$select_table = mysql_real_escape_string($_POST['select_table']);
$query_select = "INSERT INTO pdf_table
SELECT * FROM $select_table";
$select_query = mysql_query($query_select,$connectDatabase);
}
Please ensure that both the tables have equal number of columns.
It is a good practice to use following way for inserting records with select query :-
INSERT INTO pdf_table (column_1, column_2) SELECT column_1, column_2 FROM $select_table
your sql query is like
INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1;
The approach you are using INSERT INTO pdf_table SELECT * FROM $select_table will give you only single row inserted from 1st table to another table.
As per your requirement you want all the records from 1st table should get insert in your pdf_table
So i will like to suggest try the following approach
May be this is what you looking for
if(isset($_POST['select_table']))
{
$select_table = mysql_real_escape_string($_POST['select_table']);
$select_query = mysql_query( "select * from $select_table ",$connectDatabase);
while ($row = mysql_fetch_array($select_query))
{
mysql_query( "insert into pdf_table values($row['column1'],$row['column2'],...,$row['columns']) ",$connectDatabase);
}
}
try this one :
if(isset($_POST['select_table']))
{
$select_table = mysql_real_escape_string($_POST['select_table']);
$query_select = " Create Table pdf_table ( SELECT * FROM $select_table); ";
$select_query = mysql_query($query_select,$connectDatabase);
}
but, this query will create new table "pdf_table".

php/mysql get all tables and columns in a database

i want to check a database and get all table names first and then
show some kind of report from data inside each table
$query = $db3->query("SELECT `table_name` from INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'db3' ") or die("$db3->error");
while ( $table = $query->fetch_assoc() )
{
//echo $table['table_name'].'<br />';
$t = trim($table['table_name']);
//i get all table names now i want to select all columns from that table name :
$select = $db3->query("SELECT * FROM ".$t." ") or die($db3->error);
$row = $select->fetch_assoc();
// checking to see if query worked fine
echo gettype($row);
//it returns NULL
}
am i doing it the right way ?
what should i do ?
For security reasons you should have a whitelist of databases/tables you want to generate reports from. Querying for all tables assumes that all future tables will need to be part of this system.
You can query for the columns in each table using show columns from tableName and iterate the results.
You can use this query in mysql
mysql> SELECT table_name, table_type, engine
-> FROM information_schema.tables
-> WHERE table_schema = 'db5'
-> ORDER BY table_name DESC;
SELECT table_name, column_name FROM Information_schema.columns WHERE table_name like '%example%' ORDER BY table_name DESC
Or you can try this
SELECT table_name, column_name FROM Information_schema.columns WHERE column_name like '%example%' ORDER BY table_name DESC

How to SELECT DEFAULT value of a field

I can't seem to find or write a sqlquery that SELECTS the DEFAULT VALUE
(and I don't think I can generate it in phpmyadmin for me to copy)
I tried to SELECT it as if it was a record but to no avail...
$defaultValue_find = mysql_query(
"SELECT $group FROM grouptable WHERE $group='DEFAULT'")
or die("Query failed:".mysql_error());
$defaultValue_fetch = mysql_fetch_row($defaultValue_find);
$defaultValue = $defaultValue_fetch[0];
echo $defaultValue;
"SELECT $group FROM grouptable WHERE $group=DEFAULT( $group ) "
Or I think better:
"SELECT DEFAULT( $group ) FROM grouptable LIMIT 1 "
Update - correction
As #Jeff Caron pointed, the above will only work if there is at least 1 row in grouptable. If you want the result even if the grouptable has no rows, you can use this:
"SELECT DEFAULT( $group )
FROM (SELECT 1) AS dummy
LEFT JOIN grouptable
ON True
LIMIT 1 ;"
Get the default values of all fields in mytable in the associative array $res:
// MySQL v.5.7+
$res = [];
$sql = "SHOW FULL COLUMNS FROM `mytable`";
foreach ($PDO->query( $sql, PDO::FETCH_ASSOC ) as $row) {
$res[$row['Field']] = $row['Default'] ;
}
print_r($res);
You can get the default column of any table, and in fact lots of interesting information about it, by looking at the INFORMATION_SCHEMA.COLUMNS tables. As the documentation states...
INFORMATION_SCHEMA provides access to database metadata, information about the MySQL server such as the name of a database or table, the data type of a column, or access privileges. (Source: MySQL 8.0 Reference Manual / INFORMATION_SCHEMA Tables / Introduction.)
So, to get the column default, just SELECT COLUMN_DEFAULT, like...
SELECT COLUMN_DEFAULT
FROM information_schema.columns
WHERE TABLE_SCHEMA = 'YourSchema'
AND TABLE_NAME = 'YourTable' AND
COLUMN_NAME = 'YourField';
You can then just wrap this into a subquery, SELECT * FROM YourTable WHERE YourField = (queryabove). This lets you make a much more customizable, default-based list in your MySQL query.

Categories