SRL Archive - Web Development

show tables with mysql


omgh4x0rz

2007-04-21 18:59:24

Ok, I'm just starting to learn php and I just hooked it up to a mysql database. How do I get it to show the data on the page? I tried using SHOW TABLES, but nothing happens. Help please

Heres my code (sorry if the standards suck balls) I'm 99% sure it is connecting to the database right because I have it say "database accessed" if the select_db returns true.


<?php
$con = mysql_connect("host","database","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
echo 'connected successfully...';

if (mysql_select_db("database", $con))
{
echo 'Database accessed!';
$sql= "CREATE TABLE persons
(
personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
Firstname varchar(15),
Lastname varchar(15),
Age int
)";
mysql_query($sql, $con);
mysql_query("INSERT INTO persons(Firstname, Lastname, Age)
VALUES('Im not', 'Telling My Name', 16)");
mysql_query("SHOW TABLES");
}
else
{
echo "You suck at mysql: ".mysql_error();
}
}
mysql_close($con);
?>

Night

2007-04-21 22:00:10

ok, so usually when you run a query, you toggle through the results.

So some thing like

$query = mysql_query("SHOW TABLES");
while($row = mysql_fetch_array($query) {
echo($row['Tables_in_database']); // database should be replaced with your db name.
}

fetch_array() takes a rows out of queries, so you can go through them. These rows are arrays, with the collumn names as the array names. (In this case, the only column name was Tables_in_Database.

Hope it helps =p

arti

2007-04-24 23:44:10

mysql_fetch_array() being a wonderful tool, look into mysql_fetch_assoc().

If youre comfortable with arrays, this function can be extremely useful at times.
http://us2.php.net/mysql_fetch_assoc

munk

2007-07-25 01:00:36

PhpMyAdmin
^get it!!!!

saves alot of time working with your databases, and gives you more time to script:)


its just a bunch of php files that make a GUI for a mySQL database. Throw it into your http root directory and navigate to it, but MAKE SURE YOU PASSWORD PROTECT IT, otherwise someone could navigate to it and mess with your tables! .htaccess files are the easiest way.

EDIT: oh yeah, and it shows you all the sql querys its running to access your database so you can copy and paste it into your scripts.