Unexpected T_Array error

I’m having trouble with my PHP class. My professor gave us a code but it seems he did it incorrectly and it’s giving me an unexpected T_array error. I can’t figure out what he did wrong or how I can fix it to do my project. Here’s his code:

<?php
class Songs_model extends CI_Model{
public function __construct(){
	parent::__construct();
	$this->load->database();
}


public function get_songs($slug=null){
	if ($slug==null){
		$rs=$this->db->get('songs');
		return $rs->result_array();
	}else{
		$rs=$this->db->get_where('songs'array('song_slug' => $slug));
		return $rs->row_array();
	}
}
}
?>

Any idea what’s wrong? The error states it’s on line 14:
$rs=$this->db->get_where('songs’array(‘song_slug’ => $slug));
Nothing I do works.

$this->db->get_where('songs'array('song_slug' => $slug))

This is a method call; $this->db is an object that has a method called get_where.

When you call a method, you provide arguments between two brackets. This is the 'songs'array('song_slug' => $slug) part. Arguments can be any kind of PHP value. If more than one argument is passed these arguments should be separated by a comma.

In 'songs'array('song_slug' => $slug) there are two arguments; the string 'songs' and the array array('song_slug' => $slug). However, they have not been separated by a comma.

When PHP reads this code it gets to the argument list, reads the first argument, then breaks with an error as it expects a comma or a closing bracket. It tries to help you by telling you what it found instead - in English, the error reads “I found an unexpected array”.

Add a comma to the argument list as follows:

$rs=$this->db->get_where('songs', array('song_slug' => $slug));
// Add this comma here..........^

and your code should run.

3 Likes

Thank you for the explanation. It helped me understand a lot better.

Sponsor our Newsletter | Privacy Policy | Terms of Service