View Full Version : grrr
brcolow 11-20-2002, 01:36 AM when i try to add this code into my exsiting code it says unexcpected t else.. the code im trying to add is...
mysql_select_db("revscreenshots");
$result = mysql_query("select * from articles WHERE id='$screen_id'") or die(mysql_error());
while ($row = mysql_fetch_object($result)) {
print"<h3><u>Screenshots</u></h3>";
print"<A HREF=\"http://www.flashstand.com/images/screenshots/{$row->name}\"><IMG HEIGHT=50 WIDTH=50 SRC=\"A HREF=\"http://www.flashstand.com/images/screenshots/{$row->name}\"></A>";
print"<center><h3><u>Options</u></h3><img src=\"http://www.flashstand.com/images/print.gif\"><a href=\"http://www.flashstand.com/printable.php?type=review&id=$id\">Printable Version</a> | <a href=\"http://www.flashstand.com/reviews/index.php\">Go back to reviews.</a>";
}
else
{
echo "<center>Sorry, this review doesnt exsist!</center>";
}
cortices 11-20-2002, 02:45 AM Well, if that is the complete code, then you are missing the beginning part of the if else statement. You can not have an else block, without first having an if block.
if (some condition)
{
// do something here if true
}
else
{
// do something here if not true
}
Rich2k 11-20-2002, 06:40 AM mysql_select_db("revscreenshots");
$result = mysql_query("select * from articles WHERE id='$screen_id'") or die(mysql_error());
if ($result) {
while ($row = mysql_fetch_object($result)) {
print"<h3><u>Screenshots</u></h3>";
// SNIP
}
}
else {
echo "<center>Sorry, this review doesnt exsist!</center>";
}
Try this.
brcolow 11-22-2002, 02:22 AM no. that is not the whole code, the if statment starts here...
$numrows = mysql_num_rows($result);
if ($numrows > 0)
{
then there is more after that THEN i add the other one... do i need to make a whole other query because i am selecting from a new table?
jtrovato 11-22-2002, 02:57 AM why don't you post the entie code, instead of us guessing what it is. this way we can help you my son.....
Rich2k 11-22-2002, 05:33 AM Originally posted by brcolow
no. that is not the whole code, the if statment starts here...
$numrows = mysql_num_rows($result);
if ($numrows > 0)
{
then there is more after that THEN i add the other one... do i need to make a whole other query because i am selecting from a new table?
You don't need to do that you can do it simply by detecting if $result is true.
if ($result) {
// rows returned
}
else {
// no rows returned
}
And yes I agree with jtrovato you ask use to help you find an error in your code and then don't post all of it.
brcolow 11-22-2002, 11:08 AM <?
@ $db = mysql_pconnect("localhost", "", "");
if (!$db)
{
echo "<b>Could not connect to database, try again later.</b>";
exit;
}
mysql_select_db("news");
$id = $_GET['id'];
$result = mysql_query("select * from reviews WHERE id='$id'");
$numrows = mysql_num_rows($result);
if ($numrows > 0)
{
$row = mysql_fetch_object($result);;
$gameplay = $row->gameplay;
$sounds = $row->sounds;
$graphics = $row->graphics;
$interface = $row->interface;
$presentation = $row->presentation;
$screen_id = $HTTP_GET_VARS['id'];
print"<u><CENTER><H3>$row->title</H3></CENTER></u>";
print"<center>$row->author</center>";
print"<p align=\"center\"><table border=\"1\" width=\"50%\">
<TR>
<TD ALIGN=\"center\" BGCOLOR=\"#330099\"><FONT COLOR=\"#FFFF00\">Publisher: $row->publisher <br>
Release Date: $row->release <br>
Overall Rating: $row->rating/10 <br>";
print"</font>
</TD>
</TR>
</TABLE>
</p>";
print"<p>$row->about</p>";
$screen_id = $HTTP_GET_VARS['id'];
mysql_select_db("revscreenshots");
$result = mysql_query("select * from articles WHERE id='$screen_id'") or die(mysql_error());
while ($row = mysql_fetch_object($result)) {
print"<h3><u>Screenshots</u></h3>";
print"<A HREF=\"http://www.flashstand.com/images/screenshots/{$row->name}\"><IMG HEIGHT=50 WIDTH=50 SRC=\"A HREF=\"http://www.flashstand.com/images/screenshots/{$row->name}\"></A>";
print"<center><h3><u>Options</u></h3><img src=\"http://www.flashstand.com/images/print.gif\"><a href=\"http://www.flashstand.com/printable.php?type=review&id=$id\">Printable Version</a> | <a href=\"http://www.flashstand.com/reviews/index.php\">Go back to reviews.</a>";
}
else
{
echo "<center>Sorry, this review doesnt exsist!</center>";
}
?>
IDNet 11-22-2002, 11:33 AM $row = mysql_fetch_object($result);;
Line 18 (I think) you have a double semi-colon, try removing one of them.
jtrovato 11-22-2002, 02:06 PM Ok first of all, I see this all the time. I don't know who came up with the idea of having the "{" of the same line of code as If and While statements: example
if (condition) {
do something
}
This makes it very hard to trouble shoot coding. Also when you have blocks of code, try to indent the entire block so you can see what if part of that control and what is not. I re-formatted the code and added a } on line 41, I removed the extra ; on line 14. You have an Else statement connected to a while loop. You can not use an else statement on a while loop. So i added the } on 41 to make it belong to the if that was above.
This should work, try it out and let us know. Good luck buddy. BTW you don't need {} if there is only one line of code after an if,while statement. in line 44.
<?
@ $db = mysql_pconnect("localhost", "", "");
if (!$db)
{
echo "<b>Could not connect to database, try again later.</b>";
exit;
}
mysql_select_db("news");
$id = $_GET['id'];
$result = mysql_query("select * from reviews WHERE id='$id'");
$numrows = mysql_num_rows($result);
if ($numrows > 0)
{
$row = mysql_fetch_object($result);
$gameplay = $row->gameplay;
$sounds = $row->sounds;
$graphics = $row->graphics;
$interface = $row->interface;
$presentation = $row->presentation;
$screen_id = $HTTP_GET_VARS['id'];
print"<u><CENTER><H3>$row->title</H3></CENTER></u>";
print"<center>$row->author</center>";
print"<p align=\"center\"><table border=\"1\" width=\"50%\"><TR>
<TD ALIGN=\"center\" BGCOLOR=\"#330099\"><FONT COLOR=\"#FFFF00\">Publisher: $row->publisher <br>
Release Date: $row->release <br>
Overall Rating: $row->rating/10 <br>";
print"</font>
</TD>
</TR>
</TABLE>
</p>";
print"<p>$row->about</p>";
$screen_id = $HTTP_GET_VARS['id'];
mysql_select_db("revscreenshots");
$result = mysql_query("select * from articles WHERE id='$screen_id'") or die(mysql_error());
while ($row = mysql_fetch_object($result))
{
print"<h3><u>Screenshots</u></h3>";
print"<A HREF=\"http://www.flashstand.com/images/screenshots/{$row->name}\"><IMG HEIGHT=50 WIDTH=50 SRC=\"A HREF=\"http://www.flashstand.com/images/screenshots/{$row->name}\"></A>";
print"<center><h3><u>Options</u></h3><img src=\"http://www.flashstand.com/images/print.gif\"><a href=\"http://www.flashstand.com/printable.php?type=review&id=$id\">Printable Version</a> | <a href=\"http://www.flashstand.com/reviews/index.php\">Go back to reviews.</a>";
}
}
else
echo "<center>Sorry, this review doesnt exsist!</center>";
?>
This should line up correctly, if not you get the idea about keeping the code together by indenting it the same.
John
Rich2k 11-22-2002, 06:23 PM A friend of mine whose been a programmer for 20 years told me that traditionally the Americans use the braces on the same line whilst European programmers tend to put them on a seperate line. I don't know if there is any truth but I don't have any reason to disbelieve a C programmer who has worked for companies such as IBM 10-20 years ago
i.e. USA
if (condition) {
}
EU
if (condition)
{
}
jtrovato 11-22-2002, 06:26 PM strange right, I guess it's all about what you personally like. I get confused if I don't see things lined up within the same column
John
brcolow 11-22-2002, 06:48 PM for some weird reason when i do that code.... the option thing doesnt show up nor the screenshots.... or even the word Screnshots...
jtrovato 11-22-2002, 06:50 PM if ($numrows > 0) .... this condition must be true in order to execute the inside block on code. make sure that the query returns a record set.
Rich2k 11-22-2002, 07:35 PM As I say why not just to
if ($result)
{
}
else
{
}
as that will remove the need to check the row count (unless you are using it for other purposes as well).
brcolow 11-22-2002, 07:37 PM can u please post the code...?
Rich2k 11-22-2002, 08:01 PM If after you set a mysql query as $result then instead of getting a mysql_num_rows, if you are simply looking for if there is more than one row returned or not simply use
if ($result) {
// something got returned so go do something
}
else {
// oops nothing found
}
Just use it just after your query... instead
if ($numrows > 0)
Both work, I just tend to prefer doing the above rather than executing an additional mysql php function.
brcolow 11-22-2002, 08:32 PM i still dont know what to do...
kunal 11-23-2002, 06:49 AM Hey,
The problem is, you cant have a while and an else... the else syntax should come only with an if...
try this code :
mysql_select_db("revscreenshots");
$result = mysql_query("select * from articles WHERE id='$screen_id'") or die(mysql_error());
$num = mysql_num_rows($resuly);
if(!$num) {
echo "<center>Sorry, this review doesnt exsist!</center>";
}
while ($row = mysql_fetch_object($result)) {
print"<h3><u>Screenshots</u></h3>";
print"<A HREF=\"http://www.flashstand.com/images/screenshots/{$row->name}\"><IMG HEIGHT=50 WIDTH=50 SRC=\"A HREF=\"http://www.flashstand.com/images/screenshots/{$row->name}\"></A>";
print"<center><h3><u>Options</u></h3><img src=\"http://www.flashstand.com/images/print.gif\"><a href=\"http://www.flashstand.com/printable.php?type=review&id=$id\">Printable Version</a> | <a href=\"http://www.flashstand.com/reviews/index.php\">Go back to reviews.</a>";
}
That should do what your trying to do.
kunal
|