Jump to content

[solved] Baffled by script error.


Perad

Recommended Posts

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=9

This 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 65
Bad 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.
Link to comment
https://forums.phpfreaks.com/topic/24105-solved-baffled-by-script-error/
Share on other sites

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;
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
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 . "&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";
}
?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.