Receiving error in PHP syntax

Hello! I am trying to make a small script, and when I put in the following code:
[php]else {
$banner = mysql_fetch_array(mysql_query(“SELECT * FROM bungeebans WHERE player=’$player’”));
$reason = mysql_fetch_array(mysql_query(“SELECT * FROM bungeebans WHERE player=’$player’”));
echo "

Status: <span class=“label label-danger”>Banned




Banned by: $banner




Reason: $reason

";
}[/php]
I have already logged into the MySQL database in the script, but the error logs says that the “[]” were unexpected, meaning the [‘by’] and [‘reason’] are not in the correct place, but I cannot find another place to put them. Is there anyone on here who can help me? Thanks for your time.

You are returning an array. You need to deal with it as an array. Your queries are returning the same exact data, so all you are doing is duplicating it. You are also using obsolete code.

Do you mind helping me with that? And I don’t really mind if it is obsolete, as long as it works. This works but returns an array when I remove the [‘banner’] as I previously had said, though, so it should be simple. I just can’t seem to get it. >:(

Oh, wow. I didn’t even see that I didn’t put in the full code that I was having issues with. Here is the code that is giving me errors:
[php]$con = mysql_connect("$host","$user","$password");
mysql_select_db("$database");

$sql = mysql_query(“SELECT * FROM bungeebans WHERE player=’$player’”);
if(mysql_num_rows($sql) == 0) {
echo “

Status: <span class=“label label-success”>Not banned

”;
} else {
$banner = mysql_fetch_array(mysql_query(“SELECT * FROM bungeebans WHERE player=’$player’”))[‘by’];
$reason = mysql_fetch_array(mysql_query(“SELECT * FROM bungeebans WHERE player=’$player’”))[‘reason’];
echo "

Status: <span class=“label label-danger”>Banned




Banned by: $banner




Reason: $reason

";
}
} [/php]

You can take my help or you can leave it but I think you need to switch over to mysqli or PDO, in this case I am using PDO. I feel PDO while it can be difficult to understand at first will become really easy to understand.

I’ll jump into the code right away then explain a few things afterwords:

[php]<?php

/* important! use actual prepared statements (default: emulate prepared statements) /
/
throw exceptions on errors (default: stay silent) /
/
fetch associative arrays (default: mixed arrays) */
$db_options = array(
PDO::ATTR_EMULATE_PREPARES => false
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);

/* Make the connection to the database */
$pdo = new PDO(‘mysql:host=localhost;dbname=games;charset=utf8’, ‘username’, ‘password’, $db_options);

/* Set the query variable */
$query = ‘SELECT id, banner, reason, player, status, score FROM bungeebans ORDER BY player’;

/* Prepare the Query for Execution /
$stmt = $pdo->prepare($query);
/
Execute the Query */
$stmt->execute();

?>

Untitled Document <?php /* Even though you executed the query, you still need */ /* to fetch the rows and in this case it is an */ /* associative array that is put into $row. */ while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { if ( $row['status'] == 'banned' ) { echo '

Player ' . $row['player'] . ' was banned by ' . $row['banner'] . ' and the reason is ' . $row['reason'] . '

' . "\n"; } else { echo '

Player ' . $row['player'] . ' score is ' . $row['score'] . ' points

' . "\n"; } } ?> [/php] Now, I Re-Structured your database table to this: [code]CREATE TABLE IF NOT EXISTS `bungeebans` ( `id` int(5) NOT NULL, `banner` varchar(60) COLLATE utf8_bin NOT NULL, `reason` varchar(120) COLLATE utf8_bin NOT NULL, `player` varchar(60) COLLATE utf8_bin NOT NULL, `status` varchar(30) COLLATE utf8_bin NOT NULL, `score` int(6) NOT NULL DEFAULT '0' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=8 ; [/code]

The reason I did this is that it is easier to let the mysql handle all the data and php hand all the processing data (well as much as possible). My suggestion is study up on PDO by going to http://php.net/manual/en/pdostatement.fetch.php and checking out all the PDO Methods (Functions). Like I said you can take my help or not, but I think you would be better if you took it, instead of using obsolete code.

I don't really mind if it is obsolete

You really should mind. [member=57087]Strider64[/member] gave you a good current example to work with. We/anyone would be doing you wrong helping with obsolete code.

Thank you for your help [member=57087]Strider64[/member]. I will attempt to use that… Thanks.

Sponsor our Newsletter | Privacy Policy | Terms of Service