I'm trying to take a query:
SHOW TABLES;
which will display a bunch of tables with the chat_ prefix. I want to remove the chat_ prefix from the string, format the variable (with a link), and display it. How is this accomplished?
I'm trying to take a query:
SHOW TABLES;
which will display a bunch of tables with the chat_ prefix. I want to remove the chat_ prefix from the string, format the variable (with a link), and display it. How is this accomplished?
Don't know what you mean by "link". Iterate through your tables and replace "chat_" with an empty string to remove the prefix:
$formatted_table_name = str_replace("chat_", "", $table_name);
//... do something
$link = '<a href="#">' . $formatted_table_name .'</a>'; //add to link
You can remove the chat_ prefix in SQL using:
SELECT REPLACE(t.table_name, 'chat_', '')
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.table_schema = 'your_db_name'
AND t.table_name LIKE 'chat_%'
Reference:
That should do the whole job:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if($result = $mysqli->query("SHOW TABLES")){
while($row = $result->fetch_row()){
echo '<a href="link_target">'.str_replace('chat_', '', row[0]).'</a>';
}
}
As there is not standard column name with the "SHOW TABLES" array, I used the fetch_row() function instead of the fetch_assoc() function.
This shows how to do the parsing on the PHP side of things:
<?php
@$db = mysql_pconnect('server', 'user', 'password') or die('Error: Could not connect to database: ' . mysql_error());
mysql_select_db('database_name') or die('Error: Cannot select database: ' . mysql_error());
$query = "SHOW TABLES";
$result = mysql_query($query) or die('Error: ' . mysql_error());
$num = mysql_num_rows($result);
for ($i = 0; $i < $num; $i++)
{
$row = mysql_fetch_assoc($result);
$table = $row['Tables_in_database_name'];
$table2 = str_replace("chat_", "", $table);
$link[] = "<a href=\"http://whatever\">" . $table2 . "</a><br />";
}
print_r($link);
?>