Question about PHP Arrays syntax
| From: | Vania Smrkovski | Date: | Mon, 22 Apr 2002 04:54:26 +0000 |
| Subject: | Question about PHP Arrays syntax | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-18711@lists.php.net to get a copy of this message | ||
Hello, folks, and I apologize in advance if I break any rules here. This is
my first post on this list and I couldn't find any archive to review
existing posts for the answer to this question.
I'm an advanced HTML/JavaScript/DHTML programmer with relational database
experience using FileMaker and Access on IIS with ASP. I finally managed to
put that experience together in this last month when I found an inexpensive
host with PHP and MySQL support.
As a test (http://test.pandorasdream.com), I am building a page with will
initially (currently) find all tables in a database and then iterate a TABLE
dump of each database table. Next, I plan to add forms to make these tables
editable, but that's another ball of wax.
Here're my questions:
I have the below code working exactly the way I want it to, but it was
through some hacking. I want to learn how I SHOULD have done it.
a) How would you change the following the automatically find the tables and
run through the iteration. I'm new at PHP, and I learn by doing, so forgive
me for not doing the RTFM thing, but I just haven't found the answer yet.
So instead I created a table called tableNames with one field, which I
personally populated with the names of all tables.
How should I have done it so that wouldn't be necessary.
and
b) When I grab the values of the table names using my prepared table
tableNames, I couldn't figure out how to get the TEXT value of that record.
When I put in a debug statement, all I got was a value of "Array", rather
than a name of a table. You'll see below that I hacked a work-around by
using a "foreach()" statement, which works flawlessly.
How can I grab a specific field of an array without using a foreach()?
<?php
$hostname = "localhost";
$usernamedb = "margretormaybejoe";
$passworddb = "yeahright";
$dbName = "boohoohaahaa";
$tableNamesTable = "tableNames";
/* Connecting, selecting database */
$link = mysql_connect($hostname, $usernamedb, $passworddb)
or die("Could not connect to database host.");
print "Connected successfully.<br>\n";
mysql_select_db($dbName) or die("<h1>Error</h1><p>Could not select
database.</p>");
/* Performing SQL query */
$queryOfTableNames = "SELECT Name FROM $tableNamesTable";
$resultOfTableNamesTable = mysql_query($queryOfTableNames) or
die("<h1>Error</h1><p>Tried Query for all elements of \$tableNamesTable.
Query failed.</p>");
while ($tableLine = mysql_fetch_array($resultOfTableNamesTable,
MYSQL_ASSOC))
{
foreach($tableLine as $tableName)
{
/* Performing SQL query */
$query = "SELECT * FROM $tableName";
print "<h4>$query.</h4>";
$result = mysql_query($query) or die("<h1>Error</h1><p>Tried Query for
all elements of $tableName. Query failed.</p>");
/* Printing results in HTML */
print "<table border=\"1\"">\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
print "\t<tr>\n";
foreach ($line as $col_value)
{
print "\t\t<td>$col_value </td>\n";
}
print "\t</tr>\n";
}
print "</table><br> \n\n";
/* Free result set */
mysql_free_result($result);
}
}
mysql_free_result($resultOfTableNamesTable);
/* Closing connection */
mysql_close($link);
?>
Thanks for your help!
_________________________
Vania Smrkovski
Internet/Interactivity Consultant
vania@pandorasdream.com
http://pandorasdream.com