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
Deseree
Forum Commoner
Posts: 84 Joined: Mon Feb 13, 2006 11:35 pm
Post
by Deseree » Mon Apr 03, 2006 11:17 pm
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.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Mon Apr 03, 2006 11:39 pm
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
}
(#10850)
Deseree
Forum Commoner
Posts: 84 Joined: Mon Feb 13, 2006 11:35 pm
Post
by Deseree » Tue Apr 04, 2006 9:10 am
righteous, dunno whay i didn't thiink that!