View Full Version : php/mysql script help
mlowery
July 6th, 2003, 14:36
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:
<?
$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:
<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.
Loon
July 6th, 2003, 14:43
Shouldn't your variable be $roster.screen_name ? not just $screen_name
mlowery
July 6th, 2003, 14:50
Well I changed it to this...
?
$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.
hohoho
July 6th, 2003, 15:20
<?
$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
Loon
July 6th, 2003, 15:28
<?
$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
mlowery
July 6th, 2003, 22:26
I tried both of the scripts and both of them ended up looking like this... http://www.mlowery.net/AC/roster2.php
keith
July 6th, 2003, 23:16
just a shot in the dark, not sure if this will work or not...
<?
$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.
dawizman
July 7th, 2003, 00:19
Hmm, I have found some mistakes in the above code. Below is code I beleive should work:
<?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>"
?>
keith
July 7th, 2003, 00:23
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"> ...
dawizman
July 7th, 2003, 00:57
nope, Yours:
><td bgcolor=\"".$bgcolor."\">".$row['screen_name']."</
mine:
><td bgcolor='" . $bgcolor . ">" . $row['screen_name'] . "</
thoses missing spaces are making the errors.
mlowery
July 7th, 2003, 01:38
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
><td bgcolor='" . $bgcolor . ">" . $row['screen
to
><td bgcolor='" . $bgcolor . "'>" . $row['screen
Had to add the ' at the end of the bgcolor statement.
Thanks
dawizman
July 7th, 2003, 02:03
NP.
Dam, cant beleive I missed that.:o
keith
July 7th, 2003, 22:22
???
you actually need spaces in there? i never put spaces around the whites-space periods. huh...
dawizman
July 7th, 2003, 23:18
Well, you learn something new every day. :D
keith
July 7th, 2003, 23:59
:D
why the hell does it work in some places and then not work in others?
Powered by vBulletin® Version 4.1.7 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.