This should be really easy to answer, honest

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MrRSMan
Forum Newbie
Posts: 20
Joined: Sun Feb 03, 2008 8:11 am

This should be really easy to answer, honest

Post 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!
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: This should be really easy to answer, honest

Post by it2051229 »

you mean something like phpMyAdmin where you get to choose or navigate through your tables?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: This should be really easy to answer, honest

Post 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
Post Reply