Jump to content


Photo

[solved] Baffled by script error.


  • Please log in to reply
5 replies to this topic

#1 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 16 October 2006 - 03:13 PM

OK... i just don't get this.

Here is the problem bit of 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";

Now... this leads me to the following URL.
http://localhost/mat...=show&scoreid=9

This comes up with the following error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\webs\test\viewmatchreports.php on line 65
Bad news id

Yet if i change
$query = "SELECT * FROM matchreports WHERE scoreid=$scoreid";
to
$query = "SELECT * FROM matchreports WHERE scoreid=9";

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.

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 16 October 2006 - 03:18 PM

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 this

global $db, $scoreid;

to

global $db;
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 16 October 2006 - 03:21 PM

How is $scoreid set? It looks like you're assuming that register_globals 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

#4 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 16 October 2006 - 03:24 PM

Yeah sorry i put $scoreid as a global variable just trying anything to make it work.

Score ID is defined like so.

echo "<tr><td><a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&scoreid={$row['scoreid']}\">See Detailed Report</a>" . "</td></tr>";

I might as well post the full script. Its a bit messy  :-\

<?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 . "&nbsp;&nbsp;&nbsp;&nbsp;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 &nbsp; vs &nbsp; " . $o . "&nbsp;&nbsp;-" . $mdd . "/" . $mmm . "/" . $myyyy. "</td></tr>\n";
    echo "<tr><td colspan=\"2\">" . $l . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 	
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $si . "</td></tr> \n";
	echo "<tr><td width=\"200\" height=\"200\">Insert Map Photo</td><td> UNC Score: " . $row['uncscore1'] . "&nbsp;&nbsp;" . $ot . ": " . $row['oppscore1']
	. "</td></tr>\n";
	echo "<tr><td width=\"200\" height=\"200\">Insert Map Photo</td><td> UNC Score: " . $uncsecondmap . "&nbsp;&nbsp;" . $ot . ": " . $oppsecondmap .
	"</td></tr>\n";
	echo "<tr><td>Total Score</td><td> UNC Score: " . $row['uncscore2'] . "&nbsp;&nbsp;" . $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";
}
?>


#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 16 October 2006 - 03:27 PM

On your URL you have "&scoreid=", yet in the script you are referring to $_GET['id'], change the $_GET['id'] to $_GET['scoreid'].

Ken

#6 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 16 October 2006 - 03:31 PM

bah, thanks a lot, i was editing this directly from a news comment system... i missed that edit.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users