Page 1 of 1

Array not working

Posted: Tue Jul 15, 2008 6:34 am
by HogDog
Hello all first time poster, and new to PHP. I'm having some trouble with an array, I'm trying to list several websites (currently all random) that have the same usergroup number as the users from a mysql table but all I'm getting is a blank page. Heres my code:
<?php
$query = mysql_connect("*********", "******", "******") or die(mysql_error());
mysql_select_db('******', $query) or die(mysql_error());

session_start();

if(isset($_SESSION['UserON'])) {

// User is logged in!
$conn ="SELECT url FROM links WHERE usergroup = " . $_SESSION['UserON'] . " LIMIT 0,50";
$result = mysql_query($conn,$query);

while($row=mysql_fetch_row($result))
{
$url[] = $row[1];
}

echo "<p>\n";
foreach( $url as $v )
{
echo $v."<br />\n";
}
echo "</p>\n";

} else {
header('location: login.php');
}
mysql_free_result($result);
?>
but it only outputs the HTML:
<p>
<br />
<br />
</p>
now if I use the following bit I get an output from the database, but since this is not an array i just get a single url.
<?php
$query = mysql_connect("************", "******", "*******") or die(mysql_error());
mysql_select_db('*****', $query) or die(mysql_error());

session_start();

if(isset($_SESSION['UserON'])) {

// User is logged in!
$query = mysql_query("SELECT url FROM links WHERE usergroup = " . $_SESSION['UserON'] . " LIMIT 0,50") or die(mysql_error());

list($url) = mysql_fetch_row($query);
echo $url ;
}
else {

header('location: login2.php');
}
?>
any ideas? thanks

Re: Array not working

Posted: Tue Jul 15, 2008 6:41 am
by alex.barylski
First, some tips. When your trying to figure out why code isn't working, simplify what you have already by removing or commenting out conditional statements like IF, SWITCH, etc. Focus only on the code at hand that is giving you troubles.

Code: Select all

<?php
    $query = mysql_connect("*********", "******", "******") or die(mysql_error());
    mysql_select_db('******', $query) or die(mysql_error());
    
    session_start();
    
    // User is logged in!
    $conn ="SELECT url FROM links WHERE usergroup = " . $_SESSION['UserON'] . " LIMIT 0,50";
    $result = mysql_query($conn,$query);
        
    while($row=mysql_fetch_row($result))
    {
        echo '<div>'.$row[0].'</div>';
    }
 
Try this and see what happens. I think part of the problem was that you were referencing the $row variable with index 1 when you are only returned one field per row, so you should have used index ZERO -- I think anyways. Try it and see what happens.

Re: Array not working

Posted: Tue Jul 15, 2008 6:54 am
by HogDog
did the trick, thanks a ton.