• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

php/mysql script help

mlowery

New Member
I am trying to get information out of a data base and print it onto a page and have the information print into a table and have the table rows alternate between two colors. The script that I am trying to fix is this:
PHP:
<?
$host = "localhost";
$user = "user";
$pass = "pass";
$db = "db"; 
mysql_connect($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db ($db);
$result = mysql_query("SELECT * FROM roster ORDER BY roster.screen_name ASC");


echo "<TABLE>";
while($row = mysql_fetch_array($result) ) {

    if (!isset($bgcolor) || $bgcolor == "#5B4613") $bgcolor = "#D2B77E";
    else $bgcolor = "#5B4613";

echo "<tr><td bgcolor=\"".$bgcolor."\">$screen_name['screen_name']</td></tr>";
}
echo "</TABLE>"
?>
It's results print this: http://www.mlowery.net/AC/roster2.php

The original script that I used which doesn't print the info into a table is this:
PHP:
<font ="textsize">
<?
$host = "localhost";
$user = "user";
$pass = "pass";
$db = "db"; 
mysql_connect($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db ($db);
$result = mysql_query("SELECT * FROM roster ORDER BY roster.screen_name ASC");


while($screen_name = mysql_fetch_array($result) )
{
echo $screen_name['screen_name'] . "<br>";
}
?>
It's results print this: http://www.mlowery/net/AC/roster.php
Can someone please help.
 
Well I changed it to this...
PHP:
?
$host = "localhost";
$user = "user";
$pass = "pass";
$db = "db"; 
mysql_connect($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db ($db);
$result = mysql_query("SELECT * FROM roster ORDER BY roster.screen_name ASC");


echo "<TABLE>";
while($row = mysql_fetch_array($result) ) {

    if (!isset($bgcolor) || $bgcolor == "#5B4613") $bgcolor = "#D2B77E";
    else $bgcolor = "#5B4613";

echo "<tr><td bgcolor=\"".$bgcolor."\">$roster.screen_name['screen_name']</td></tr>";
}
echo "</TABLE>"
?>
It prints this... http://www.mlowery.net/AC/roster2.php
It looks a little bit closer but its still not working.
 
PHP:
<?
$host = "localhost";
$user = "user";
$pass = "pass";
$db = "db"; 
mysql_connect($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db ($db);
$result = mysql_query("SELECT * FROM roster ORDER BY roster.screen_name ASC");


echo "<TABLE>";
while($row = mysql_fetch_array($result) ) {

    if (!isset($bgcolor) || $bgcolor == "#5B4613") $bgcolor = "#D2B77E";
    else $bgcolor = "#5B4613";

echo "<tr><td bgcolor=\"".$bgcolor."\">$row['screen_name']</td></tr>";
}
echo "</TABLE>"
?>
$row = mysql_fetch_array($result) means that you should use $row insead of $roster.screen_name :p
 
PHP:
<? 
 
$connect = mysql_connect("localhost","user","pass"); 
$db = mysql_select_db("db_name", $connect); 

$sql = mysql_query("SELECT * FROM roster ORDER BY roster.screen_name ASC"); 

$alternate = "2";
while ($row = mysql_fetch_array($sql)) { 
$name = $row["roster.screen_name"];

if ($alternate == "1") { 
$colour = "#5B4613"; 
$alternate = "2"; 
} 
else { 
$colour = "#D2B77E"; 
$alternate = "1"; 
} 

echo ("<table>
<tr><td bgcolor=$colour>$name</td>
</tr></table>"); 
} 

?>

try that

bah, you posted while i was typing :p
 
just a shot in the dark, not sure if this will work or not...
PHP:
<?
$host = "localhost";
$user = "user";
$pass = "pass";
$db = "db";

mysql_connect($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($db) or die('Unable to select database '.$db);

$result = mysql_query("SELECT * FROM roster ORDER BY screen_name ASC");


echo "<TABLE>\n";

while ($row = mysql_fetch_array($result)) {

    if (!isset($bgcolor) || $bgcolor == "#5B4613") $bgcolor = "#D2B77E";
    else $bgcolor = "#5B4613";

echo "<tr><td bgcolor=\"".$bgcolor."\">".$row['screen_name']."</td></tr>\n";
}

echo "</TABLE>\n";
?>
i don't know the structure of your database, but using the given info i don't see how that couldn't work.
 
Last edited:
Hmm, I have found some mistakes in the above code. Below is code I beleive should work:

PHP:
<?PHP
$host = "localhost";
$user = "user";
$pass = "pass";
$db = "db";

mysql_connect($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db($db) or die('Unable to select database '.$db);

$result = mysql_query("SELECT * FROM roster ORDER BY screen_name ASC");


echo "<TABLE>";

while ($row = mysql_fetch_array($result)) {

    if (!isset($bgcolor) || $bgcolor == "#5B4613") $bgcolor = "#D2B77E";
    else $bgcolor = "#5B4613";

echo "<tr><td bgcolor='" . $bgcolor . ">" . $row['screen_name'] . "</td></tr>";
}

echo "</TABLE>"
?>
 
am i mising something, or is your code identical to mine? i think so, except for the fact your bgcolor code will come out like so:

... <td bgcolor='#5B4613> ...

while the others will come out:

... <td bgcolor="#5B4613"> ...
 
Last edited:
nope, Yours:
PHP:
><td bgcolor=\"".$bgcolor."\">".$row['screen_name']."</
mine:
PHP:
><td bgcolor='" . $bgcolor . ">" . $row['screen_name'] . "</


thoses missing spaces are making the errors.
 
http://www.mlowery.net/AC/roster2.php
Thanks a lot guys, I would have never figured it out. I ended up using dawizman's code but had to change
PHP:
><td bgcolor='" . $bgcolor . ">" . $row['screen
to
PHP:
><td bgcolor='" . $bgcolor . "'>" . $row['screen
Had to add the ' at the end of the bgcolor statement.
Thanks
 
???

you actually need spaces in there? i never put spaces around the whites-space periods. huh...
 
Back
Top