0

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?

2
  • So you have a variable containing the string 'SHOW TABLES;' and you want to do what with it? Commented Jun 9, 2010 at 21:36
  • 4
    Format how? Display how? You need to be more detailed to get a good answer. Commented Jun 9, 2010 at 21:37

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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>
2

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:

Comments

0

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.

Comments

0

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);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.