Hello there, everyone,

I'm trying to convert my stuff over to PDO and I've been doing ok but for some reason, I can't figure out what's causing the error on this one. Could someone help me with what I'm failing to fix?
PHP 8.3

$banchk = $db->run("SELECT id, public_note, type FROM bans WHERE ip = ? OR ( $usr['id'] <> ? AND user_id = ? ) AND ( ban_end = ? OR ban_end > ? )", [$usr['ip'], '0', $usr['id'], '0', $rightnow])->fetch();

Error:

Error: syntax error, unexpected string content "", expecting "-" or identifier or variable or number

    It looks like it didn't care for my $usr['id'] in the SQL statement. Changing that seems to have resolved the error:

    $banchk = $db->run(
    	"SELECT
    		id, 
    		public_note, 
    		type 
    	FROM bans WHERE
    		ip = ?
    	OR 
    		( 
    			? <> ? 
    		AND 
    			user_id = ?
    		) 
    	AND 
    		( 
    			ban_end = ? 
    		OR 
    			ban_end > ?
    		)
    	",
    	[
    		$usr['ip'],
    		$usr['id'], 
    		'0', 
    		$usr['id'], 
    		'0', 
    		$rightnow
    	]
    )->fetch();

      Reformatted a bit (a lot?) to help visualize what is being done. One problem was the use of a PHP array element (e.g. $user['id']) within the double-quoted string, plus the closing out of that string. Maybe this at least gets you close?

      // * Note wrapping of array element in braces within PHP string
      // * Not sure about whether my balancing of parentheses is logically correct?
      $sql = <<<EOD
      SELECT id, public_note, type
      FROM bans 
      WHERE ip = ? OR (
        {$usr['id']} <> ? AND user_id = ? ) AND 
        ( ban_end = ? OR ban_end > ? )
      )
      EOD; // must be no white-space at start of this line
      
      // Now let's use that in the DB function with some formatting to help readability
      $banchk = $db->run(
        "$sql",
        [
          $usr['ip'],
          '0',
          $usr['id'],
          '0',
          $rightnow
        ]
      )->fetch();

      Heh...was typing my reply while you posted yours. 🙂

      Write a Reply...