开发者

Dreaded PHP T_STRING Error on MySQL query returned resource

I am getting the dreaded PHP error

Parse error: syntax error, unexpected T_STRING in /path/to/the/file in line 14

Here is the code:

function getSomeList($userid) {

    $query = "SELECT list_id, username_id FROM user_list WHERE username_id = 1";

    $result = mysql_query($query, $this->db);
    if (! $result ) {
        $message = "Bad Query: getUserSiteList(): " . mysql_error() . "<br>";
        $message .= "Query: " . $query;
        die($message);
    }
    $num = mysql_num_rows($result);
    echo "Number of Rows Found " . $num . "!!<br>";
    echo get_resource_type($result) . "<br>";
    if ( $num > 0 ) {
        echo "No rows found, nothing to print so am exiting<br>";
    }

    ... more code ...

   }

So everything works until the 'if' statement where I use the $num variable. The $result variable is a is a mysql result. Also anytime I use the $result resource I get this error. For example later in the function when I attempt to access a row of data using this call:

wh开发者_如何学运维ile ( $row = mysql_fetch_assoc($result)) { ... more code ...

It errors out with the same T_STRING error, (this is after removing the if statement shown above.) I am at a loss, anyone have any suggestions where my error is? I personal feel it has something to do with the resource returned by the mysql query. Something is amiss with it.

Thanks for the help.

PS I am using PHP 5.3.0 and MySQL 5.1 on OS X


The unexpected T_STRING error happen when you have a string in a place it is not suppose to be. This can be because you forgot to concatenate something or you forgot to place a semi colon after a line.

AKA: It is a problem with your code's compisition, not it's execution.

The code you have provided does not seem to have the problem, and as you said, taking that line out does not seem to fix the problem.

Perhaps it is somewhere else? Often the problem is related to the line above the one given. My guess is that the line above that while statement does not have a proper semicolon, or is somehow is syntactically incorrect.


The problem was not syntax in my code. There was some hidden character causing issues (not ^M, everything was developed on OS X and ran on the same system). I copied and pasted all the lines from my original function and other called functions to create a simple MySQL connect and query that still got the error. The key here was that I copied and pasted. So whatever odd character that was in the original source file got moved. I created a second file for testing and verified that it was broken like the first. I completely deleted the offending line and retyped it. And everything worked. I did a diff on the 2 files and this was the output.

$ diff testquery.php testquery_same.php 
11,14c11,14
<   
<       if ( $result ) {
<           echo "No rows found, nothing to print so am exiting<br>";
<       }
---
>       
>       if ( $result ) {
>           echo "No rows found, nothing to print so am exiting<br>";
>       }   
16c16


As you can see there is something different but I don't know what. So the issue is resolved, I am not sure about the cause, but I need to move on at this point.
Thanks for the responses.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜