0

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:

while ( $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

3
  • 1
    Look at the var_dump of $result and $num Feb 12, 2010 at 16:44
  • For context - does line 14 refer to the sample code you provided?
    – thetaiko
    Feb 12, 2010 at 16:47
  • To answer thetaiko, Yes, the line in the error refers to the correct line in the provided code.
    – bkoch
    Feb 12, 2010 at 16:51

2 Answers 2

2

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.

2
  • The code sample above fails with the error and is a stripped down version of my working code. I have gone through the normal issues of looking for the concatenation and non terminated lines and can't see where the issue lies. I was hoping that StackTrace could spot the error in the provided code. I mentioned the while statement, to simply point out that problem appears to be with the resource returned by the mysql_query function. But the issue should be in the provided code unless it is outside the function. Can these types of errors carry over from outside code blocks?
    – bkoch
    Feb 12, 2010 at 16:57
  • I don't believe it can carry over, as you would get an Unexpected T_FUNCTION error if you tried to declare a function with bad code above it. Feb 12, 2010 at 17:08
0

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.

1
  • I have been having this issue when coding PHP for a long time. Both on Windows and OS X. I've done what I can to solve it, but lately I have to re-write almost every if-statement I make. It's super annoying and I don't know what to do. Almost going on dropping PHP for now. Using Sublime Text.
    – Mattis
    Mar 15, 2014 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.