tags:

views:

66

answers:

4

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?

+3  A: 

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
Simon
It wasn't exactly in that order, or all on one line. I meant, I had planned on adding a link to the already formatted variable.
Nik
@Nik: like this?
Simon
Yes. Now I need to figure out how to edit 3 more queries to accommodate this change. I need to pass the room variable via GET to the pages, and it needs to stick that variable into the query, any ideas>
Nik
+2  A: 

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:

OMG Ponies
A: 

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.

Kau-Boy
A: 

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\"&gt;" . $table2 . "</a><br />";
    }
    print_r($link);
?>
JYelton