Page 1 of 1

MySQL returned table issue....

Posted: Mon Apr 03, 2006 11:17 pm
by Deseree
Been trying to figure this out all day

Code: Select all

$mysql_table = "user0101_db";		
		$sql = "SHOW TABLES";$sql = mysql_query($sql);// or die('WTF:'.mysql_error());

		echo "$mysql_table <br><br>";
	while($object = mysql_fetch_object($sql))
	{
		echo $object->Tables_in_.$mysql_table; // doesn't work
		echo $object->Tables_in_user0101_db; // works
	}
I can't quite get this to work like I want.. how can I fix it because it's important the database is is a variable not hardcoded...

Do you guys know how i can fix this, the great feyd or jcart?

Peace.

Re: MySQL returned table issue....

Posted: Mon Apr 03, 2006 11:39 pm
by Christopher
I get the impression that you want to show all the tables in a database. So the order is:

1. Connect to mysql server
2. Select database
3. Execute query
4. Fetch results

And reusing variables names "$sql" for result sets is really confusing, so let's call things what they are:

Code: Select all

mysql_connect($dsn);
    // don't die, add a real if(mysql_error()) check here

    $mysql_database = "user0101_db";		
    mysql_select($mysql_database);
    // don't die, add a real if(mysql_error()) check here
 
   $sql = "SHOW TABLES";
    $result = mysql_query($sql);// or die('WTF:'.mysql_error());

    echo "database : $mysql_database <br><br>";
    $field = "Tables_in_$mysql_table";
    while($object = mysql_fetch_object($result))
    {
        echo $object->$field
    }

Posted: Tue Apr 04, 2006 9:10 am
by Deseree
righteous, dunno whay i didn't thiink that!