Perad Posted October 16, 2006 Share Posted October 16, 2006 OK... i just don't get this.Here is the problem bit of code.[code]function displayOneItem($scoreid) { global $db, $scoreid; /* query for item */ $query = "SELECT * FROM matchreports WHERE scoreid=$scoreid"; $result = mysql_query ($query); /* if we get no results back, error out */ if (mysql_num_rows ($result) == 0) { echo "Bad news id\n"; return; } $row = mysql_fetch_assoc($result); echo "<TABLE border=\"1\" width=\"580\">\n";[/code]Now... this leads me to the following URL.http://localhost/matchreports.php?action=show&scoreid=9This comes up with the following error[code]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\webs\test\viewmatchreports.php on line 65Bad news id[/code]Yet if i change [code]$query = "SELECT * FROM matchreports WHERE scoreid=$scoreid";[/code]to [code]$query = "SELECT * FROM matchreports WHERE scoreid=9";[/code]It comes up with the same URL as above but works! Whats going on? Could someone help me solve this problem. As you can see $scoreid generates the correct number but is still making an error. Quote Link to comment https://forums.phpfreaks.com/topic/24105-solved-baffled-by-script-error/ Share on other sites More sharing options...
.josh Posted October 16, 2006 Share Posted October 16, 2006 you are passing an argument in your function and calling it $scoreid as a local copy, but also declaring it as a global variable that already exists. assuming that you are doing something like this:$blah = displayOneItem(9);or just displayOneItem(9);change thisglobal $db, $scoreid;to global $db; Quote Link to comment https://forums.phpfreaks.com/topic/24105-solved-baffled-by-script-error/#findComment-109569 Share on other sites More sharing options...
kenrbnsn Posted October 16, 2006 Share Posted October 16, 2006 How is $scoreid set? It looks like you're assuming that [url=http://www.php.net/register_globals]register_globals[/url] is enabled. This is usually not the case anymore, since the default was changed from "enabled" to "disabled" at least 3 years ago.Also, you have $scoreid used as both a passed variable and a global variable. This could be the real problem. Use one or the other, not both.Ken Quote Link to comment https://forums.phpfreaks.com/topic/24105-solved-baffled-by-script-error/#findComment-109572 Share on other sites More sharing options...
Perad Posted October 16, 2006 Author Share Posted October 16, 2006 Yeah sorry i put $scoreid as a global variable just trying anything to make it work.Score ID is defined like so.[code]echo "<tr><td><a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&scoreid={$row['scoreid']}\">See Detailed Report</a>" . "</td></tr>";[/code]I might as well post the full script. Its a bit messy :-\[code]<?php function viewmatchreports() { /* user config variables */ $max_items = 5; /* max number of news items to show */ /* make database connection */ $db = mysql_connect ('localhost','root','admin'); mysql_select_db ('UNC',$db);function displayNews($all = 0) { /* bring in two variables * $db is our database connection * $max_items is the maximum number * of news items we want to display */ global $db, $max_items; /* query for news items */ if ($all == 0) { /* this query is for up to $max_items */ $query = "SELECT scoreid, postdate, opponenttag, matchdatedd, matchdatemm, matchdateyyyy, outcome FROM matchreports ORDER BY postdate DESC LIMIT 5"; } else { /* this query will get all news */ $query = "SELECT scoreid, postdate, opponenttag, matchdatedd, matchdatemm, matchdateyyyy, outcome FROM matchreports ORDER BY postdate DESC"; } $result = mysql_query ($query) or die("Problem with the query: $query on line:" . __LINE__ . "<br>" . mysql_error()); while ($row = mysql_fetch_assoc ($result)) { /* display news in a simple table */ echo "<TABLE border=\"1\" width=\"580\">\n"; /* place table row data in * easier to use variables. * Here we also make sure no * HTML tags, other than the * ones we want are displayed */ $ot = htmlentities ($row['opponenttag']); $mdd = ($row['matchdatedd']); $mmm = ($row['matchdatemm']); $myyyy = ($row['matchdateyyyy']); $outcome = ($row['outcome']); /* display the data */ echo "<tr><td>UNC vs " . $ot . " - " . $mdd . "/" . $mmm . "/" . $myyyy . " Outcome: " . $outcome . "</td></tr>"; echo "<tr><td><a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&scoreid={$row['scoreid']}\">See Detailed Report</a>" . "</td></tr>"; /* finish up table*/ echo "</TABLE>\n"; echo "<BR>\n"; } /* if we aren't displaying all news, * then give a link to do so */ if ($all == 0) { echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=all\">View all matches</a>\n"; }}function displayOneItem($scoreid) { global $db; /* query for item */ $query = "SELECT * FROM matchreports WHERE scoreid=$scoreid"; $result = mysql_query ($query); /* if we get no results back, error out */ if (mysql_num_rows ($result) == 0) { echo "Bad news id\n"; return; } $row = mysql_fetch_assoc($result); echo "<TABLE border=\"1\" width=\"580\">\n"; /* easier to read variables and * striping out tags */ $o = ($row['opponent']); $l = ($row['ladder']); $si = ($row['server']); $ot = htmlentities ($row['opponenttag']); $uncsecondmap = (($row['uncscore2']) - ($row['uncscore1'])); $oppsecondmap = (($row['oppscore2']) - ($row['oppscore1'])); $mdd = ($row['matchdatedd']); $mmm = ($row['matchdatemm']); $myyyy = ($row['matchdateyyyy']); $outcome = ($row['outcome']); $report = nl2br (strip_tags ($row['report'], '<a><b><i><u>')); /* display the items */ echo "<tr><td colspan=\"2\">United Nations Clan vs " . $o . " -" . $mdd . "/" . $mmm . "/" . $myyyy. "</td></tr>\n"; echo "<tr><td colspan=\"2\">" . $l . " " . $si . "</td></tr> \n"; echo "<tr><td width=\"200\" height=\"200\">Insert Map Photo</td><td> UNC Score: " . $row['uncscore1'] . " " . $ot . ": " . $row['oppscore1'] . "</td></tr>\n"; echo "<tr><td width=\"200\" height=\"200\">Insert Map Photo</td><td> UNC Score: " . $uncsecondmap . " " . $ot . ": " . $oppsecondmap . "</td></tr>\n"; echo "<tr><td>Total Score</td><td> UNC Score: " . $row['uncscore2'] . " " . $ot . " Score: " . $row['oppscore2'] . "</td></tr>\n"; echo "<tr><td colspan=\"2\">" . $report . "</td></tr>"; echo "</TABLE>\n"; echo "<BR>\n"; /* now show the comments */}/* this is where the script decides what do do */echo "<CENTER>\n";switch($_GET['action']) { case 'show': displayOneItem($_GET['id']); break; case 'all': displayNews(1); break; default: displayNews();}echo "</CENTER>\n";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24105-solved-baffled-by-script-error/#findComment-109578 Share on other sites More sharing options...
kenrbnsn Posted October 16, 2006 Share Posted October 16, 2006 On your URL you have "&scoreid=", yet in the script you are referring to $_GET['id'], change the $_GET['id'] to $_GET['scoreid'].Ken Quote Link to comment https://forums.phpfreaks.com/topic/24105-solved-baffled-by-script-error/#findComment-109579 Share on other sites More sharing options...
Perad Posted October 16, 2006 Author Share Posted October 16, 2006 bah, thanks a lot, i was editing this directly from a news comment system... i missed that edit. Quote Link to comment https://forums.phpfreaks.com/topic/24105-solved-baffled-by-script-error/#findComment-109583 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.