;

PDA

Click to See Complete Forum and Search --> : php mysql question


mbannonb
05-01-2001, 06:52 AM
Here's the problem, I have two usernames, both of which are being called and stored in the variable $username, but for some reason, only one is being printed.


<?php

//just get the all the user's names from the username column
$sql="SELECT username FROM useradmins";

//connect to db, etc.
include("connect.php");

//while there exists a username in the array, run this code
while($row=mysql_fetch_array($sql_result))
{
//set the variable $username to hold the username value that was in the mysql column
$username=$row["username"];

//call the script that then takes the username variable and prints it out
include("featured.php");

print "<br>";

}

?>


While there are 2 usernames, only one gets printed. Any thoughts?

Musicman
05-01-2001, 07:35 AM
Hi,

I am sure you hve already tried to just print $username, so the problem might lie in featured.php. May I see it?

Musicman

mbannonb
05-01-2001, 03:44 PM
I may have oversimplified things in the previous post. Basically, I'm setting up a script that loads 'featured' editors data. You can see the output here:

http://www.inflash.com/showplayer/testindex.php

It is currently only displaying one editor when there are 3, all with publish=1


<?php

$sql="SELECT username, website, picurl, websitetitle FROM useradmins WHERE publish=1";

//connect db
include("connect.php");

while($row=mysql_fetch_array($sql_result))
{
$username=$row["username"];
$website=$row["website"];
$picurl=$row["picurl"];
$websitetitle=$row["websitetitle"];

include("testfeatured.php");

print "<br>";

}

?>

in testfeatured.php is contained:

[code]

<?php

print "<table>
<tr>
<td align=center colspan=4><font face=Verdana size=2><b>$username's Picks</b></a><br>";

if(isset($website))
{
print "<font face=Verdana><a href=$website>$websitetitle</a></font>";
}

print "</td><td colspan=2>";

if(isset($picurl))
{
print "<img src=$picurl border=1>";
}

etc.



here's the data (all test data) contained in the useradmins table (sorry it's not in alignment):


id_num username email password rand validated ip_address website picurl websitetitle publish
10 Nastor nastor@inflash.com ***** 1 24.30.117.121 BoneLand.com http://members.spree.com/entertainment/matrixspoof/nastorthumb.jpg BoneLand.com 1 Edit Delete
8 mbannonb stanley@inflash.com **** 1 24.30.117.121 http://www.inflash.com http://members.spree.com/entertainment/matrixspoof/darrow-100-100.jpg InFlash.com 1 Edit Delete
12 tester test@inflash.com 1234 1 http://www.inflash.com 1 Edit Delete


If you need the full scripts, let me know and I'll email them to you. It's got me stumped.

Musicman
05-01-2001, 07:29 PM
Hi mbannonb,

I changed script to fit my system (postgresql rather than mysql) and got three users rather than one.
One thing looks somewhat different - the xx_fetch_array code. Probably check this one in ph/mysql manual

<?php

$sql="SELECT username, website, picurl, websitetitle FROM useradmins WHERE publish=1";

$myDB = pg_connect("dbname=notenversand");
$sql_result = pg_exec($myDB, $sql);
$nrow = 0;
while($row=@pg_fetch_array($sql_result, $nrow++))
{
$username=$row["username"];
$website=$row["website"];
$picurl=$row["picurl"];
$websitetitle=$row["websitetitle"];

include("testfeatured.php");

print "<br>";
}

?>


Musicman

mbannonb
05-01-2001, 09:12 PM
First of all, thanks so much for your help.

I put the selected code above up on my server (for mysql) and it was working fine as well, but it wasn't working when combined with the whole script.

So, I figured it was something with the rest of the script. The problem was, I was fixated on the $username variable, thinking that it was the culprit. Why I stuck on that, I don't know.

But it turns out it was the $sql_result variable that was causing the problem.

The line: while($row=mysql_fetch_array($sql_result))

occurs in index.php, and then index.php calls featured.php, which also has

while($row=mysql_fetch_array($sql_result))

Thus, '$sql_result' gets looped through until it's empty. When it returns to 'index.php', '$sql_result' is empty, so when it revisits 'while($row=mysql_fetch_array($sql_result))', it's considered empty and the loop is terminated, even though only one username's data has been processed.

So there it is.

Musicman
05-01-2001, 09:41 PM
Hi,

you are right on that ... but this piece of code did not shine up on your post.
Anyway, it is interesting to see that some of the more common functions look different for different databases, although it was probably a major concern in php development to make them as similar as possible

Musicman