Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Php session syntax

Status
Not open for further replies.

rouse01

IS-IT--Management
Sep 10, 2001
143
US
I'm trying to echo tables from MySQL database to browser using php 4.2.2. The following reads the correct number of tables in my database [entries], but I get a syntax error in my 'for' loop. I know the problem is with my 'rows' variable, but I've tried many variations and am back to this: (this page is called help.php)
<?php
session_start();
if(!$_SESSION['count'])
{
echo "Not Registered<br>" ;
$_SESSION['count'] = 1;
$_SESSION['conn'] = mysql_pconnect("localhost", "user", "password");
mysql_select_db("database");
$qry = "show tables";
$result = MYSQL_QUERY($qry);
$_SESSION['entries'] = mysql_num_rows($result);
$row = mysql_fetch_array($result);
}
else
{
echo "Registered<br>" ;
$_SESSION['count']++;
}
?>
<body>
<?php
echo "<br>Number of Tables: ".$_SESSION['entries']."<br>";
for ($i = 0; $i<$_SESSION['entries']; $i++)
{
echo "$_SESSION['row[$i]']" ;
}
?>
</form>
<br>Number of Clicks: <? echo $_SESSION['count']; ?><br />
<br />
<a href="help.php">Submit this page</a><br />
<a href="destroy.php">Destroy Session</a><br />
</body>

Thanks - Keith
 
Read up on the mysql_fetch_array function. It only retrieves one row from your database (the array is an array of fields). To loop through rows, you need to call the function repeatedly. Also, in your echo statement, don't put the variable inside quotes - array variables cannot be
simply substituted this way, unlike simple variables.


Rob
[flowerface]
 
Another thing I noticed in your code, too, about this line:

$_SESSION['conn'] = mysql_pconnect("localhost", "user", "password");

mysql_pconnect(), mysql_connect(), and fopen() return handles, not values. You cannot store handles in a session variable. It's not necessary anyway, as PHP will keep track of the permanent MySQL connection without storing the variable somewhere.

Want the best answers? Ask the best questions

TANSTAAFL!!
 
Thanks both of you for the post back. I've peeled out alot of the $_SESSION declarations & looked at the mysql_fetch_array documentation. I'm trying to have the script present tables in the database. I hope to read these tables in once and save the results to an array - to be operated on later - but can't figure the syntax to display results, which display fine in the initial call, but not in the 'body'.
I'm now using this in the session init portion of previous posted:
Start:
$_SESSION[entries] = mysql_num_rows($result);
for ($i = 0; $i <$_SESSION[entries]; $i++)
{
$row = mysql_fetch_array($result, MYSQL_NUM);
$p = $row[0];
echo "P".$i."=".$p."<br>" ;
}
:Stop
The echo here helps me debug & displays expected results, even if I replace $p with $row[0].

The problem is echoing results in the body. I'm using this with no success:
Start
echo "<br>Number of Tables: ".$_SESSION[entries]."<br>";
for ($i = 0; $i <$_SESSION[entries]; $i++)
{
echo $p[$i]."<br />" ;
}
:Stop

The result is wierd. I have three tables in the database and the last table is called ees. The output of the above returns:
e
e
s
If I change the $p[$i] to just $p, then the output is: ees.

Thanks for the tips - Keith
 
I don't know whay you put the number of rows in a SESSION variable... but anyway, your code:

Code:
...
$_SESSION['entries'] = mysql_num_rows($result);
   $row = mysql_fetch_array($result);
...

echo "<br>Number of Tables: ".$_SESSION['entries']."<br>"; 
for ($i = 0; $i<$_SESSION['entries']; $i++)
   {
   echo "$_SESSION['row[$i]']"

the "for" loop can be replaced by "while", take a llok to the example pasted & modified from php.net:

Code:
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
   die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SHOW TABLES");
$num_rows = mysql_num_rows($result);

echo "Number of Tables: " . $num_rows . "\n<br>\n";

echo "<b>····Tables·····</b><br>\n";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
   printf("Name: %s  Name: %s", $row[0]);  
}

mysql_free_result($result);
?>
 
Ok, I've ripped all the session declarations out except the count.
My reasoning to keep 'count' is that if count is 1, then client needs to read the database. If count is > 1, then the client has already hit the database & should have the number of & name of tables by now. I'm trying to have the Browser request the sql database only once to grab the table info. Then on browser refresh, the counter is >1 so no need to rehit the sql database, just display what was learned on 'count = 1'. That's also why I was trying to use session variables, because my understanding on php & server side is that unless I use 'POST' or 'GET', the info is lost on a refresh.

So, is this not feasable using the <a href="help.php">Submit this page</a><br /> technique?

The code in my first post has a mistake with the closing 'form' tag above the a href. That was left over from a earlier rewrite. And thanks Chacalinc, I tried working that example from php.net into my code last night with same results.

I really like the way php can be used to interface with mysql & push pages to a browser. Right now I'm having a tough time coding my (probably inept) concept.

Keith
 
My Syntax problem was with the braces []. Look at the $_SESSION['p'][], compared to my earlier tries. This works fine...

Top of script:
$entries = mysql_num_rows($result);
echo "<br>Top Loop - Number of Tables: ".$entries."<br>";
for ($i = 0; $i <$entries; $i++)
{
$row = mysql_fetch_array($result, MYSQL_NUM);
$_SESSION['p'][] = $row[0];
echo $i." = ".$_SESSION['p'][$i]."<br>" ;
}

and in the Body:

$entries = count($_SESSION['p']);
echo "<br>Body Loop - Number of Tables: ".$tables."<br>";
for ($i = 0; $i <$entries; $i++)
{
echo $_SESSION['p'][$i]."<br />" ;
}

Now browser hits MySql only once to read the tables. I hope someone else can benefit from this.
Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top