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.
Related
I have made a localhost server with wamp, and i want to be able to print out the entire database. I thought of the hacky solution of just documentwriting the entire database with all the values then just print the page. However, I have no idea how to show the database content on the html page while preserving the tables and structure of the database. I tried this:
<?php
$conn = mysqli_connect('localhost','root','','varer');
$data = $conn->query("SELECT * FROM lagerbeholdning_hth");
while ( $tables = $result->fetch_array())
{
echo $tmp[0]."<br>";
}
?>
but it didn't work. I also tried this while loop instead:
while ( $tables = mysqli_fetch_array($data))
{
echo $tables["VARENUMMER"] . "<br>";
}
which posts all the correct values, but not properly formatted. Is there any way to do this, in another way perhaps? or a way to format the outputs properly so they are displayed according to database structure? or just plain print out the database from SQL.
You can use var_dump() to print a structured representation of an array.
I recommend you use fetch_assoc(), which returns an associative array, so you'll see the column names.
$result = $conn->query("SELECT * FROM lagerbeholdning_hth");
while ($row = $result->fetch_assoc()) {
var_dump($row);
}
If you want to code the display of the entire database contents, you will have a lot of work to do. Displaying just the tables for a single database, you could write a select statement on the information_schema.TABLES to get the table names:
SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name';
Once you have the table name, you can then get the column names using the following query:
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
AND table_schema = 'db_name'
Once you have the table name and column information, you can then use this information to get the tables data and display it in an html table.
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
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}'
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 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"];
}