Page 1 of 1

This should be really easy to answer, honest

Posted: Thu Oct 29, 2009 7:40 pm
by MrRSMan
I'm using the code below to check for if a certain table exists in a MySQL database. The code works perfectly as it is by the way.

What I want to do is too allow people to check for table of their choice, by way of a form. So basically I just want to substitute $table for $var. How do I do this?

Code: Select all

<?php
 
$var = $_POST[name];
 
function table_exists($table, $database) { 
    mysql_connect('localhost', 'username', 'password') or die(mysql_error()); 
    mysql_select_db(techtac2_rsstats) or die(mysql_error()); 
    if (mysql_query("SELECT 1 FROM `".'$table'."` LIMIT 0")) { 
        return true; 
    } 
    else { 
        return false; 
    } 
} 
if (table_exists('table_name', 'database_name')) { 
    echo "User $name already exists\n"; 
}
else { 
echo"my bad";
}
 
?>
Thanks in advance!

Re: This should be really easy to answer, honest

Posted: Thu Oct 29, 2009 8:07 pm
by it2051229
you mean something like phpMyAdmin where you get to choose or navigate through your tables?

Re: This should be really easy to answer, honest

Posted: Thu Oct 29, 2009 8:08 pm
by requinix
MrRSMan wrote:The code works perfectly as it is by the way.
No it doesn't.

Code: Select all

if (mysql_query("SELECT 1 FROM `".'$table'."` LIMIT 0")) {
That's wrong. Your table_exists(), as you posted it, will always return false.


Variables from External Sources