I am kinda new to PHP and MySql and I am trying to find a way to echo the table names of my database in my page.
When I use : SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'myDBname'
directly in PHPMyAdmin, I get the results I want but I just don't know I to "echo" it in my page.
This is what I am using at the moment :
$statement = $db->prepare("SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'myDBname'");
$row = $statement->fetchAll();
What would be, in your opinion, the best way to display it on my page if I want to eventually echo the result of the query in a dropdown menu ?
This works for me.
$tables = array();
$stmt = $db->query("SHOW TABLES");
while($row = $stmt->fetch(PDO::FETCH_NUM)){
$tables[] = $row[0];
}
var_dump($tables);
Related
I want to get the last time my database was updated, so I use this query in my PHP code:
$query = mysqli_query($mysqli, "SELECT UPDATE_TIME
FROM information_schema.tables
WHERE TABLE_SCHEMA = 'map_db'
AND TABLE_NAME = ".$objects_tab."");
$lastUpdateTime = mysqli_fetch_array($query);
echo "<div id ='lastUpdate'>".$lastUpdateTime."</div>";
For some reason the query won't work, does anyone know whats the problem?
It works when I do other queries so its not the $mysqli connect variable or the table name variable that's wrong.
Table name value should be wrapped in single quotes:
"SELECT UPDATE_TIME
FROM information_schema.tables
WHERE TABLE_SCHEMA = 'map_db'
AND TABLE_NAME = '".$objects_tab."'"
I think that's incorrect. mysql_fetch_array() returns an array of results. You must to modify like this:
$rows = mysqli_fetch_array($query);
echo "<div id ='lastUpdate'>".$rows['lastUpdateTime']."</div>";
Assuming lastUpdateTime as the key in the database.
I want to make select and print out all of the tables I have (I got that so far), and then limit it using `` and then ordering them by table name, and have 10 results per page.
How would I go about doing that? I know how to do it getting data from tables, but I don't know how to do it using just tables.
I have this so far:
function list_tables($type){
$sql = "SHOW TABLES FROM example";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result)){
$table_name = $row[0];
echo $table_name; //edited out a lot to keep it simple
//I'm just printing out a lot of data based on table name anyway
}
mysql_free_result($result);
}
So far, it only prints out all of the table names (+ extra info I print for table names) all in the same page and it's getting the the point where it takes forever to scroll. I'd like to limit it to about 10-20 posts per page instead of a few hundred posts on one page.
Thanks in advanced if anyone can help me. Much appreciated.
Calculate the offset and limit according to the page number and try the below query:
function list_tables($type, $offset, $limit){
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'example' ORDER BY TABLE_NAME LIMIT $offset, $limit";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result)){
$table_name = $row[0];
echo $table_name; //edited out a lot to keep it simple
//I'm just printing out a lot of data based on table name anyway
}
mysql_free_result($result);
}
Use below given query which supports LIMIT so you can do pagination with your table names.
select * from information_schema.tables LIMIT 5
i did this:
function list_tables(){
$amtperpage = 15;
$sql = "SELECT COUNT(TABLE_NAME) FROM information_schema.tables WHERE TABLE_SCHEMA = 'my_dbname'";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$total_rows = $row[0];
//pagination stuff here
if(isset($_GET['p'])) $curpage = intval($_GET['p']); else $curpage=1;
$start = abs(($curpage-1)*amtperpage);
$sql = "SELECT TABLE_NAME FROM information_schema.tables ORDER BY TABLE_NAME ASC LIMIT $start,$per_page";
$res = mysql_query($sql);
while($row=mysql_fetch_array($res)) $DATA[++$start]=$row;
$uri = strtok($_SERVER['REQUEST_URI'],"?")."?";
$tmpget = $_GET;
unset($tmpget['p']);
if($tempget){
$uri .= http_build_query($tmpget)."&";
}
$num_pages=ceil($total_rows/$amtperpage);
for($i=1;$i<=$num_pages;$i++) $PAGES[$i]=$uri.'p='.$i;
?><div id="container">Pages:
foreach ($PAGES as $i => $link){
if($i == $curpage){
=$i
} else {
?><?=$i?>
}
} ?>
foreach($DATA as $i => $row){
$table_name = $row[0];
//use my functions to get data for each table name and list it and such
}
}
this is highly butchered since I have a lot of stuff that would've gotten in the way of the point but this should work. Thanks to the people who helped me. :)
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;
Here i'm trying to get the next auto increment value from database and should be shown on the webpage. Before i'm using mysql and its working perfectly. Now, i'm rewriting my coding using pdo but i'm stuck in that place. Below i've posted my codes. How can i fetch the next auto increment value using PHP PDO?
Using Mysql :
include('config.php');
$result = mysql_query("SHOW TABLE STATUS LIKE 'ebvouchers'");
$data = mysql_fetch_assoc($result);
$autoid = array(
'next_increment' => htmlentities( $data['Auto_increment'], ENT_QUOTES, 'UTF-8' )
);
echo $autoid['next_increment'];
Using PDO :
<?php
include('config.php');
$stmt = $db->query("SHOW TABLE STATUS LIKE ebvouchers");
$size = $stmt->fetch(PDO::FETCH_ASSOC);
HERE ??? I"M STUCK
?>
Try:
include('config.php');
$string = "SHOW TABLE STATUS LIKE 'ebvouchers'";
$query = $db->connection->prepare($string);
$query->execute();
$size = $query->fetch(PDO::FETCH_ASSOC);
echo $size['Auto_increment'];
With PDO, I use this "single line" solution :
$nextId = $db->query("SHOW TABLE STATUS LIKE 'tablename'")->fetch(PDO::FETCH_ASSOC)['Auto_increment'];
Use this query:
SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'TableName';
and in php:
echo $size['AUTO_INCREMENT'];
I have two separate remote databases, the table in both databases is identicle and I want to copy a record from the old database to the new using PHP
While this the best way to copy new records from one database to another gives the solution as
mysqli_query($db1,"SELECT field1,field2,field3 FROM table1");
mysqli_query($db2,"INSERT INTO table1 (field1,field2,field3)");
Because of the number of fields involved I want to try and avoid naming all the fields
I was thinking of something like this...
$m = mysqli_query($db1,"SELECT * FROM table1");
****THIS IS WHERE I'M STUCK****
HOW DO I GET TO THIS FROM THE ABOVE STATEMENT?
$values = "'".implode("','",array_values($m))."'";
$columns = implode(",",array_keys($m));
So I can do this
mysqli_query($db2,"insert into table1 ($columns) values ($values)")
I'm aware I will need to change the PRIMARY KEY id to null.
To fetch the actual column names you could do like this:
$sql="SELECT column_name FROM information_schema.columns WHERE table_schema = 'database_name' AND table_name = table1";
Fetch the above into an array.
$result = $mysqli->query($query);
$cols = $result->fetch_array(MYSQLI_NUM);
and create comma-separated list into $columns-variable
$columns = implode(",", $cols);
For the values, just do a regular select-statement:
$sql="SELECT * FROM table1";
$result = $mysqli->query($query);
$vals = $result->fetch_array(MYSQLI_NUM);
in the end it was quite simple to modify my own code and create the array using mysqli_fetch_assoc
$m = mysqli_fetch_assoc(mysqli_query($db1,"SELECT * FROM table1"));
and then to make the id null
$m["id"] = 'replacethis';
$values = str_replace("'replacethis'","null","'".implode("','",array_values($m))."'");
$columns = implode(",",array_keys($m));
then finally...
mysqli_query($db2,"insert into table1 ($columns) values ($values)");