List all tables within DB - php

I am trying to show a list of all tables within my database
I have the below code:
$result=mysql_query("SELECT TABLE_NAME FROM vogaldes_fuse.INFORMATION_SCHEMA.Tables ")or die('ERROR 315' );
$num_rows = mysql_num_rows($result);
echo "$num_rows";
However this does not show any results, instead i see ERROR 315
Also, I want to list the table names, how do I get these?
UPDATE
I have managed to get the correct number of tables using the below:
$result=mysql_query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE 'vogaldes_fuse';")or die('ERROR 315' );
The last bit of my question still stand, how do I turn each table name into a string that I can then use to list in a select dropdown?

Just use select TABLE_NAME from information_schema.tables Remove vogaldes_fuse. database name
To create dropdown of table use
$result = mysql_query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE 'vogaldes_fuse'") or die('ERROR 315');
if (mysql_num_rows($result) > 0) {
echo "<select name='table_name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['TABLE_NAME'] . "'>" . $row['TABLE_NAME'] . "</option>";
}
echo "</select>";
}
UPDATED
For select TABLE_SCHEMA except some table
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE 'vogaldes_fuse' AND TABLE_NAME NOT
IN (
'hr_employees', 'hr_roles'
)

Run This
USE 'your db name';
SHOW TABLES;
SELECT FOUND_ROWS();

Use This
$sql = "SHOW TABLES FROM 'your db name'";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "{$row[0]}</br>";
}
mysql_free_result($result);
Hope this help .

For MySQL:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='dbName'

Related

Getting the AUTO_INCREMENT value from php(not working)

I have a simple mysqli code to select the current AUTO_INCREMENT value in a table named bookings.
After the code executes, nothing happens.I do not get any output in the screen.
Here is the code.
if ($result = mysqli_query($conn, "SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = titan3d AND TABLE_NAME = bookings", MYSQLI_USE_RESULT)) {
if (!mysqli_query($conn, "SET #a:='this will not work'")) {
printf("Error: %s\n", mysqli_error($conn));
}
myslqi_stmt_fetch_assoc($result);
var_dump($result);
}
Is there something wrong with this code.Can somebody sort it out?
You have a syntax error in the query, you didn't quote the strings. And then you need to fetch the result row.
if ($result = mysqli_query($conn, "SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'titan3d' AND TABLE_NAME = 'bookings'", MYSQLI_USE_RESULT)) {
$row = mysqli_fetch_assoc($result);
echo "Auto-increment is {$row['AUTO_INCREMENT']}";
} else {
echo mysqli_error($conn);
}

MySQL - Get column names from table and display them (using PHP)

I want to take the column names from a table and display them, so i can compare them later.
To get the names, i tried:
$entry = mysqli_query($con, 'SHOW COLUMNS FROM table');
and
$entry = mysqli_query($con, "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '$db' AND TABLE_NAME = 'table'");
I don't know whether this runs correctly or not, since i don't get an error message there.
If i try to print the contents of $entry via echo, i keep getting errors.
Previously in my code, i print other entries using:
$test = mysqli_query($con, 'SELECT DISTINCT LK_Release FROM table');
while($row = mysqli_fetch_object($test))
{
echo "Releasename: " . "$row->LK_Release". "<br>";
... }
This output works for me.
What i tried to output the columnnames:
while($row = mysqli_fetch_object($entry))
{
echo $row;
}
Any ideas?
Your query is already correct. But you lack something in the fetching:
while($row = mysqli_fetch_object($entry))
{
echo $row->Field . '<br/>';
// ^ access the objects properties
}
The following SQL statements are nearly equivalent:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
You can use DESCRIBE:
DESCRIBE my_table;
Or in newer versions you can use INFORMATION_SCHEMA:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table';
Or you can use SHOW COLUMNS:
SHOW COLUMNS FROM my_table;

how to get the number of rows in a table that come after a row with a specific id?

how do i get the number of rows in an sql table that come after a row with a specific id with php? I want to get the number of rows that come after the row with an id of 6.
Select count(*) from TableName where ID > 6
The SQL for a query to count the rows with an ID greater than 6, assuming your table is named table and the ID column is named id will be:
SELECT count(*) FROM table WHERE id > 6;
To do this from PHP, you can modify the example in the docs to add your own query. The output could also be tweaked to return a scalar value.
<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');
// Performing SQL query
$query = 'SELECT count(*) FROM table WHERE id > 6';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>

Generating a list of products based on database content

I've created a database which contains one table for each product series, basically I'm trying to list all the distinct models (table rows in each table), in a list where the first row is the table_name. Why does this not work?
$result = mysql_query("SELECT DISTINCT TABLE_NAME FROM
INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME
IN ('id') AND TABLE_SCHEMA='products-ropox'");
while($row = mysql_fetch_array($result))
{
$serie = $row["TABLE_NAME"];
echo "<ul>";
echo "<li class='ldd_heading'><a class='link'
href='products.php?category=".$serie."'>"
.ucfirst($serie)."</a></li>";
$query = mysql_query("SELECT DISTINCT model FROM $serie
ORDER by model ASC");
while($row = mysql_fetch_array($query))
{
echo "<li><a href='products.php?category=".$serie.
"&model=".$row['model']."'>".$row['model']."</a></li>";
}
echo "</ul>";
}
The first loop works well, but the second query generates an error...
Rename the second $row and you need mysql_fetch_assoc not mysql_fetch_array. If you want to use mysql_fetch_array you'll need to use $row[0]
$result = mysql_query("SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('id') AND TABLE_SCHEMA='products-ropox'");
while($row = mysql_fetch_array($result))
{
$serie = $row["TABLE_NAME"];
echo "<ul>";
echo "<li class='ldd_heading'><a class='link' href='products.php?category=".$serie."'>".ucfirst($serie)."</a></li>";
$query = mysql_query("SELECT DISTINCT model FROM $serie ORDER by model ASC");
while($row2 = mysql_fetch_array($query))
{
echo "<li><a href='products.php?category=".$serie."&model=".$row2['model']."'>".$row2['model']."</a></li>";
}
echo "</ul>";
}
Also a good pratice is to stick a or die(mysql_error()) after a query to output an error if there is one in your query.

Exclude certain columns from SHOW COLUMNS mysql

I have this query: SHOW COLUMNS FROM mash which works fine in my while loop for building a select element made from table column names. But in my table i have "id" and "tstamp" which i dont want in the select element, is this possible to exclude these columns?
echo "<form action='".$_SERVER['PHP_SELF']."' method='get'>";
connectDB();
$result = mysql_query("SHOW COLUMNS FROM mash") or die(mysql_error());
echo '<select name="column" class="column">';
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row[0]."'>".ucwords($row[0])."</option>";
}
closeConn();
echo '</select>';
echo "</form>";
PHP Way:
Use a continue in the while loop, when those fields are fetched, like this:
echo "<form action='".$_SERVER['PHP_SELF']."' method='get'>";
connectDB();
$result = mysql_query("SHOW COLUMNS FROM mash") or die(mysql_error());
echo '<select name="column" class="column">';
while ($row = mysql_fetch_array($result))
{
if($row[0] == 'id' || $row[0] == 'tstamp')
continue;
echo "<option value='".$row[0]."'>".ucwords($row[0])."</option>";
}
closeConn();
echo '</select>';
echo "</form>";
This will just skip the id and tstamp fields and process all others. continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
MySQL Way:
Remove those fields in the query like this:
SHOW COLUMNS FROM mash WHERE Field NOT IN ('id', 'tstamp');
Yes, it's possible. Instead of using SHOW COLUMNS use INFORMATION_SCHEMA.
INFORMATION_SCHEMA is the ANSI way of extracting metadata from a relational database. In MySQL you can use:
$sql = "select column_name from information_schema.columns c " +
" where c.table_schema = 'db_name' " +
" and c.table_name='table_name' " +
" and c.column_name like 'name%'";
Information Schema has the advantage that is SQLANSI compliant. You can use it in MySQL, PostgreSQL, SQLServer and other relational databases.
You can use LIKE operator.
SHOW COLUMNS FROM mash LIKE "name%"
To see just the "Collation" column that shows up in SHOW FULL COLUMNS from tablename for just one field, it's:
select COLLATION_NAME from information_schema.columns
where TABLE_SCHEMA = 'tableschemaname' and TABLE_NAME = 'tablename' and COLUMN_NAME = 'fieldname';
Ddrop the fieldname in the where, if you want to see that column for all field names.
To see all possible column names that show up from SHOW FULL COLUMNS so that you can select what you want:
select * from information_schema.columns
where TABLE_SCHEMA = 'tableschemaname' and TABLE_NAME = 'tablename' and COLUMN_NAME = 'fieldname';

Categories