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}'
Related
$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.
Hi everyone I am new in web development and I am suffering from a problem to get date and time when mysql database table last updated because I have to show it on my web page. I am getting the last updated date correctly but not correct time please help me.
<?php
$sql = "SHOW TABLE STATUS FROM MydatabaseName LIKE 'TableName'";
$tableStatus = mysql_query($sql);
while ($array = mysql_fetch_array($tableStatus)) {
$updatetime = $array['Update_time'];
$datetime = new DateTime($updatetime);
echo $updatetime ;
}
?>
If this could help you
SELECT UPDATE_TIME
FROM information_schema.tables
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'tabname'
How can I tell when a MySQL table was last updated?
since you have tagged a question in mysql.
Did you try this?
see if this is helpful.
select columns from table order by date_time column desc limit 1:
If you have a relatively recent version of MySQL, you can query information_schema.tables
select substr(update_time,1,10) as date_updated,
substr(update_time,12) as time_updated
from information_schema.tables
where
table_schema = 'name_of_your_database' and
table_name = 'your_table_name';
Note that this may not work for user-defined tables if your MySQL engine is InnoDB. It works as advertised on a MyISAM installation.
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
I want to use PHP to return a list of my tables containing a specific word.
I found mysql_tablename but apparently this function has been deprecated. How would I go about doing this?
$q = mysql_query("SHOW TABLES LIKE 'pattern'");
while ($row = #mysql_fetch_row($q))
{
echo $row[0]."<br>";
}
You can Issue a query against information_schema.tables, should be something like
select * from information_schema.tables where table_name like '%keyword%';
Query the MySQL server for it:
SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name'
[AND table_name LIKE 'wild']
Source
For tables containing a specific word, use the LIKE keyword to match it in the WHERE clause.
I am writing a plugin for wordpress. When the plugin is initialised I need to find out if the users table contains the columns I am trying to insert to ensure I am not overwriting anything. Can someone provide me with the syntax that does this; I think it looks something like this:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('columnA','ColumnB')
AND TABLE_SCHEMA='YourDatabase';
Thanks
If you're just trying to find out if the column exists, I'd do
SELECT 1
FROM information_schema.COLUMNS
WHERE COLUMN_NAME = 'column_name'
AND TABLE_NAME = 'table_name'
AND TABLE_SCHEMA = 'database_name'
LIMIT 1
pseudocode:
DBQuery("SHOW COLUMNS FROM ".$table);
while (DBGetRow())
{
$columns[]=$access["Field"];
}