Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[HELP]PHP Related question

Joined
Oct 14, 2008
Messages
1,277
Reaction score
249
So i have a Parse Error: Syntax Error: I've already tryed to look up with the editors that sometimes show the error but without result also I already checked : PHP parse/syntax errors on google; and how to solve them but not sure on how to fix it. Have a good day. Parse error: syntax error, unexpected 'else' (T_ELSE), expecting end of file on line 24

PHP:
<?php
include 'config.php';

// connection query
$Connection = new PDO("odbc:Driver=$SQLDriver;Server=$IPAddress;Database=$DATABase;", $USERName, $PASSWORD);


// define rank number
$i = 1;
$top = $i++;


// get data query
$rankquery = "SELECT TOP 1000 * FROM Users ORDER BY userLevel, totalExp, equipItemLevel";

// prepare data query
$result = $Connection->query($rankquery);


// output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$top."</td><td>".$row[userName]."</td><td>".$row[userLevel]."</td><td>".$row[totalExp]."</td><td>".$row[equipItemLevel]."</td></tr>";
    }
 else {
    echo "No data available";
}
$Connection->close();

?>
 
Joined
Feb 2, 2012
Messages
413
Reaction score
249
12345
So i have a Parse Error: Syntax Error: I've already tryed to look up with the editors that sometimes show the error but without result also I already checked : PHP parse/syntax errors on google; and how to solve them but not sure on how to fix it. Have a good day. Parse error: syntax error, unexpected 'else' (T_ELSE), expecting end of file on line 24

PHP:
<?php

try fixing to an if statement..  or if you want to stay with while put it in an outside statement and check afterwards the row..

// output data of each row

    while ( $row = $result->fetch_assoc() ) {
    if( row != null )
   {
        echo "<tr><td>".$top."</td><td>".$row[userName]."</td><td>".$row[userLevel]."</td><td>".$row[totalExp]."</td><td>".$row[equipItemLevel]."</td></tr>";
    }
    else { echo "No data available"; }
}
?>
 
Upvote 0
Joined
Oct 14, 2008
Messages
1,277
Reaction score
249
Now i've got this error:
PHP:
Fatal error: Uncaught Error: Call to undefined method PDOStatement::fetch_assoc() in X:\Xampp\htdocs\rank.php:22 Stack trace: #0 {main} thrown in X:\Xampp\htdocs\rank.php on line 22
 
Upvote 0
Joined
Feb 2, 2012
Messages
413
Reaction score
249
was just as example..

this one should do...

Code:
// output data of each row
if ($result)
{
    while ($row = $result->fetch_assoc())
    {
		echo "<tr><td>".$top."</td><td>".$row[userName]."</td><td>".$row[userLevel]."</td><td>".$row[totalExp]."</td><td>".$row[equipItemLevel]."</td></tr>";
    }
}
else
{
    echo "No data available";
    echo "Prepared Statement Error:\n" + $Connection->error;
}
 
Upvote 0
Joined
Oct 14, 2008
Messages
1,277
Reaction score
249
Well, after reading some articles i finally managed to fix it, thank you anyway for the help provided it leaded me to a better keyword search on google :).
This is the working code, i also included HTML table for positioning.
PHP:
<?php
// DATABASE CONNECTION
include 'config.php';
$serverName = "$IPAddress";
$connectionInfo = array( "Database"=>$DATABase, "UID"=>$USERName, "PWD"=>$PASSWORD);
$Connection = sqlsrv_connect($serverName, $connectionInfo);

// PREPARE QUERY
$sql = "SELECT userName, userLevel, totalExp, equipItemLevel FROM Users ORDER BY totalExp DESC";
$stmt = sqlsrv_query( $Connection, $sql );
if( $stmt === false)
{die( print_r( sqlsrv_errors(), true) );}

// RANK NUMBER DEFINITION
$i = 1;

//START HTML TABLE

echo '<table>';

// HTML TOP ROWS
echo '<tr>';
echo '<th>Rank</th>';
echo '<th>Character Name</th>';
echo '<th>Level</th>';
echo '<th>Total Experience</th>';
echo '<th>Equipment Item Level</th>';
echo '</tr>';

// CHECK CONNECTION
if( $Connection )
	
// PRINT QUERY
{while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) )

// QUERY RESULT ROWS
{echo '<tr><td>' .$i++, '</td><td>' . $row['userName'] . '</td><td>' . $row['userLevel'] . '</td><td>' . $row['totalExp'] . '</td><td>' . $row['equipItemLevel'] . '</td></tr>';}

// END TABLE
echo '</table>';}

// FAIL CONNECTION
else
{echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));}

// CLOSE CONNECTION
sqlsrv_free_stmt( $stmt);
?>



So, i'm trying to use the if/elseif for the class name to switch the variables with a predefined text
PHP:
if($row["class"] == "0"){$charclass="WA0";}
else if($row["class"] == "1"){$charclass="WA1";}
else if($row["class"] == "2"){$charclass="WA2";}
else if($row["class"] == "3"){$charclass="WA3";}
else if($row["class"] == "4"){$charclass="WA4";}
else if($row["class"] == "5"){$charclass="WA5";}
else if($row["class"] == "6"){$charclass="WA6";}
else if($row["class"] == "7"){$charclass="WA7";}
else if($row["class"] == "8"){$charclass="WA8";}
else if($row["class"] == "9"){$charclass="WA9";}
else if($row["class"] == "10"){$charclass="WA10";}
else if($row["class"] == "11"){$charclass="WA11";}
else if($row["class"] == "12"){$charclass="WA12";}
else if($row["class"] == "13"){$charclass="WA13";}

And it's give's me "Notice: Trying to access array offset on value of type null"
The number 12 - WA12 shows up but with the error from above at every character
 
Upvote 0
Junior Spellweaver
Joined
Jan 22, 2021
Messages
168
Reaction score
52
Well, after reading some articles i finally managed to fix it, thank you anyway for the help provided it leaded me to a better keyword search on google :).
This is the working code, i also included HTML table for positioning.
PHP:
<?php
// DATABASE CONNECTION
include 'config.php';
$serverName = "$IPAddress";
$connectionInfo = array( "Database"=>$DATABase, "UID"=>$USERName, "PWD"=>$PASSWORD);
$Connection = sqlsrv_connect($serverName, $connectionInfo);

// PREPARE QUERY
$sql = "SELECT userName, userLevel, totalExp, equipItemLevel FROM Users ORDER BY totalExp DESC";
$stmt = sqlsrv_query( $Connection, $sql );
if( $stmt === false)
{die( print_r( sqlsrv_errors(), true) );}

// RANK NUMBER DEFINITION
$i = 1;

//START HTML TABLE

echo '<table>';

// HTML TOP ROWS
echo '<tr>';
echo '<th>Rank</th>';
echo '<th>Character Name</th>';
echo '<th>Level</th>';
echo '<th>Total Experience</th>';
echo '<th>Equipment Item Level</th>';
echo '</tr>';

// CHECK CONNECTION
if( $Connection )
    
// PRINT QUERY
{while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) )

// QUERY RESULT ROWS
{echo '<tr><td>' .$i++, '</td><td>' . $row['userName'] . '</td><td>' . $row['userLevel'] . '</td><td>' . $row['totalExp'] . '</td><td>' . $row['equipItemLevel'] . '</td></tr>';}

// END TABLE
echo '</table>';}

// FAIL CONNECTION
else
{echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));}

// CLOSE CONNECTION
sqlsrv_free_stmt( $stmt);
?>



So, i'm trying to use the if/elseif for the class name to switch the variables with a predefined text
PHP:
if($row["class"] == "0"){$charclass="WA0";}
else if($row["class"] == "1"){$charclass="WA1";}
else if($row["class"] == "2"){$charclass="WA2";}
else if($row["class"] == "3"){$charclass="WA3";}
else if($row["class"] == "4"){$charclass="WA4";}
else if($row["class"] == "5"){$charclass="WA5";}
else if($row["class"] == "6"){$charclass="WA6";}
else if($row["class"] == "7"){$charclass="WA7";}
else if($row["class"] == "8"){$charclass="WA8";}
else if($row["class"] == "9"){$charclass="WA9";}
else if($row["class"] == "10"){$charclass="WA10";}
else if($row["class"] == "11"){$charclass="WA11";}
else if($row["class"] == "12"){$charclass="WA12";}
else if($row["class"] == "13"){$charclass="WA13";}

And it's give's me "Notice: Trying to access array offset on value of type null"
The number 12 - WA12 shows up but with the error from above at every character

why doing such a if/else fields?
You can prepare an array ahead like so

PHP:
$class_name = [
          'WA1',
          'WA2',
           'etc...'
];

echo $class_name[intval($row['class'])];

This will access directly your array and output a given string depending on the class number in your row variable.

Now for your error, it says that your $row['class'] is undefined prob, try to var_dump($row); to see what's in there.
 
Upvote 0
Back
Top