Yii Music Store Help Please

Hi, I am new to Yii framework and I am in the process of doing the Yii Music Store App. I have run into a problem and need some expert advice please. I am trying to browse the store and get this database error.

…localhost/MusicStore/index.php/store/browse?gid=1

CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '. ‘Name’ . ‘t1’ . ‘ArtistId’ FROM tbl_artist t1 LEFT JOIN ‘tbl_album’ ON ‘tb’ at line 1. The SQL statement executed was: SELECT DISTINCT ‘t1’ . ‘Name’ . ‘t1’ . ‘ArtistId’ FROM tbl_artist t1 LEFT JOIN ‘tbl_album’ ON ‘tbl_album’ . ‘ArtistId’ = ‘t1’ . ‘ArtistId’ WHERE ‘tbl_album’ . ‘GenreId’ = 1 ORDER BY ‘t1’ . ‘ArtistId’ ASC

This is the error im getting in the error handler.

3:47:29.763791 error system.db.CDbCommand

CDbCommand::fetchAll() failed: SQLSTATE[42000]: Syntax error or access

violation: 1064 You have an error in your SQL syntax; check the manual that

corresponds to your MySQL server version for the right syntax to use near

'. ‘Name’ . ‘t1’ . ‘ArtistId’ FROM tbl_artist t1 LEFT JOIN ‘tbl_album’

ON ‘tb’ at line 1. The SQL statement executed was: SELECT DISTINCT ‘t1’ .

‘Name’ . ‘t1’ . ‘ArtistId’ FROM tbl_artist t1 LEFT JOIN ‘tbl_album’ ON

‘tbl_album’ . ‘ArtistId’ = ‘t1’ . ‘ArtistId’ WHERE ‘tbl_album’ . ‘GenreId’

= 1 ORDER BY ‘t1’ . ‘ArtistId’ ASC.

in C:\wamp\www\MusicStore\protected\controllers\StoreController.php (40)

in C:\wamp\www\MusicStore\index.php (13)

23:47:29.829086 error exception.CDbException

exception ‘CDbException’ with message 'CDbCommand failed to execute the SQL

statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have

an error in your SQL syntax; check the manual that corresponds to your

MySQL server version for the right syntax to use near '. ‘Name’ . ‘t1’ .

‘ArtistId’ FROM tbl_artist t1 LEFT JOIN ‘tbl_album’ ON ‘tb’ at line 1.

The SQL statement executed was: SELECT DISTINCT ‘t1’ . ‘Name’ . ‘t1’ .

‘ArtistId’ FROM tbl_artist t1 LEFT JOIN ‘tbl_album’ ON ‘tbl_album’ .

‘ArtistId’ = ‘t1’ . ‘ArtistId’ WHERE ‘tbl_album’ . ‘GenreId’ = 1 ORDER BY

‘t1’ . ‘ArtistId’ ASC’ in C:\wamp\www\framework\db\CDbCommand.php:528

Stack trace:

#0 C:\wamp\www\framework\db\CDbCommand.php(390):

CDbCommand->queryInternal(‘fetchAll’, Array, Array)

#1 C:\wamp\www\framework\db\ar\CActiveRecord.php(1291):

CDbCommand->queryAll()

#2 C:\wamp\www\framework\db\ar\CActiveRecord.php(1410):

CActiveRecord->query(Object(CDbCriteria), true)

#3 C:\wamp\www\MusicStore\protected\controllers\StoreController.php(40):

CActiveRecord->findAll(Object(CDbCriteria))

#4 C:\wamp\www\framework\web\actions\CInlineAction.php(50):

StoreController->actionBrowse()

#5 C:\wamp\www\framework\web\CController.php(309):

CInlineAction->runWithParams(Array)

#6 C:\wamp\www\framework\web\CController.php(287):

CController->runAction(Object(CInlineAction))

#7 C:\wamp\www\framework\web\CController.php(266):

CController->runActionWithFilters(Object(CInlineAction), Array)

#8 C:\wamp\www\framework\web\CWebApplication.php(283):

CController->run(‘browse’)

#9 C:\wamp\www\framework\web\CWebApplication.php(142):

CWebApplication->runController(‘store/browse’)

#10 C:\wamp\www\framework\base\CApplication.php(162):

CWebApplication->processRequest()

#11 C:\wamp\www\MusicStore\index.php(13): CApplication->run()

#12 {main}

REQUEST_URI=/MusicStore/index.php/store/browse?gid=1


my store index.php


<?php

$this->breadcrumbs=array(

	'Store',

);

?>

<img

	src="<?php echo Yii::app()->request->baseUrl; ?>/images/logo.png" />

	<h1><em><?php echo CHtml::encode(Yii::app()->name); ?></em></h1>


<?php if(isset($_GET['gid'])

	foreach ($Genres as $Genre){

		echo '<h1>' . $Genre->Name . "</h1><br />";

		$desc = $Genre->Description;

	}

	?>


<div id="gmenu">

    

    <?php echo $desc; ?></div>


<table>

	<tr>

	<?php

	$cntRow = 0;

	foreach ($Albums as $Album)

	{

		$aid = $Album->ArtistId;

		$cntRow++;

		if($cntRow % 2) echo "</tr><tr>";

		foreach ($Artists as $Artist){

			if($Artist->ArtistId === $aid){

				$aname = $Artist->Name . "<br />";

			}

		}

		echo "<td><center><strong>" . CHtml::link($aname, array('/Store/ArtistDetails/', 'artistid'=>$aid)) . "</strong>";

		echo CHtml::link('<img src="' . Yii::app()->request->baseUrl . $Album->AlbumArtUrl . '" /><br />', array('store/details/', 'albid' => $Album->AlbumId, 'aid'=>$aid));

		echo $Album->Title . "<br />" . $Album->Price . "</center></td>";

	}

	?>

	</tr>

        

</table>


<?php }

	elseif($_GET["albumid"] && $_GET["aid"]){

		foreach($Artists as $Artist){

			$aname = $Artist->Name;

			$abio = $Artist->bio;

		}

		foreach($Albums as $Album){

			$title = $Album->Title;

			$tracks = $Album->tracks;

			$price = $Album->Price;

			$albid = $Album->AlbumId;

			$aid = $Album->ArtistId;

			$lnotes = $Album->LinerNotes;

			$AlbumArtLargeUrl = $Album->AlbumArtLargeUrl;

		} ?>

<h1><?php echo CHtml::link($aname, array('/Store/ArtistDetails/', 'artistid'=>$aid)); ?></h1>

<table>

	<tr>

		<td valign="top"><?php echo '<img src="' . Yii::app()->request->baseUrl . $AlbumArtLargeUrl . '"  /><br /></td>'; ?>

		<td></td>

		<td valign="top"><?php echo "<h4>TRACKS</h4>" . $tracks; ?></td>

	

	</tr>

	<tr>

		<td valign="top"><?php echo $title . "<br />"; 

		echo $price . "<br />";

		echo CHtml::link('Add to Cart', array('/ShoppingCart/', 'albid'=>$albid, 'aid'=>$aid)) . "<br />"; ?>

		</td>

	</tr>

</table>

		<?php echo '<h3>Liner Notes</h3>' . $lnotes; ?>

		<?php

	} 	

	elseif($_GET['artistid']){

		foreach($Artists as $Artist){

			$aname = $Artist->Name;

			$abio = $Artist->bio;

			$artistArtlUrl = $Artist->ArtistGraphicUrl;

		} ?>

<h1><?php echo $aname; ?></h1>

<?php echo '<img style="float:left; margin: 0 5px 5px 0;" src="' . Yii::app()->request->baseUrl . $artistArtlUrl . '"  /><br /></td>'; ?>

<?php echo "<h4>Bio</h4>" . $abio; ?>	


<?php } else {

     echo "<h1>" . $this->id . '/' . $this->action->id . "</h1>";

     echo "<h3>" . $content . "</h3>";

}

?>



my store controller.php


<?php


class StoreController extends Controller

{

        public $message;

	public function actionIndex()

	{

                $this->message = "Hello from Store.index()";

		$this->render('index' , array('content'=>$this->message));

	}

        

        

        

        

        

        

        

        public function actionBrowse()

	{

                if (isset($_GET["gid"]))

                    $genreCriteria = new CDbCriteria();

                    $genreCriteria->select = "'GenreId', 'Name', 'Description'";

                    $genreCriteria->condition = "GenreId = " .(isset($_GET["gid"]));

                    

                    $artistCriteria = new CDbCriteria();

                    $artistCriteria->alias = "t1";

                    $artistCriteria->select = "DISTINCT 't1' . 'Name' . 't1' . 'ArtistId'";

                    $artistCriteria->join = "LEFT JOIN 'tbl_album' ON 'tbl_album' . 'ArtistId' = 't1' . 'ArtistId'";

                    $artistCriteria->condition = "'tbl_album' . 'GenreId' = " .(isset($_GET["gid"]));

                    $artistCriteria->order = "'t1' . 'ArtistId' ASC";

                    

                    $albumCriteria = new CDbCriteria();

                    $albumCriteria->alias = "t2";

                    $albumCriteria->select = "'AlbumId' , 'GenreId', 'ArtistId', 'Title', 'Price', 'AlbumArtUrl'";

                    $albumCriteria->condition = "'GenreId' = " . (isset($_GET["gid"]));

                    $albumCriteria->order = " 'ArtistId' ASC"; 

                    

                    $this->render('index', array('Albums' => Album::model()->findAll($albumCriteria),

                            

                                                'Artists'=> Artist::model()->findAll($artistCriteria),

                                                'Genres'=> Genre::model()->findAll($genreCriteria)));

                    

                    $this->message = 'Hello from Store.Browse()';

                    $this->render('index', array('content'=>$this->message));

                    

                            

                }                            

                

              

        public function actionDetails()

                

                

	{

         

                $this->message = "Hello from Store.Details()";

		$this->render('index', array('content'=>$this->message));

	}

        




	// Uncomment the following methods and override them if needed

	/*

	public function filters()

	{

		// return the filter configuration for this controller, e.g.:

		return array(

			'inlineFilterName',

			array(

				'class'=>'path.to.FilterClass',

				'propertyName'=>'propertyValue',

			),

		);

	}


	public function actions()

	{

		// return external action classes, e.g.:

		return array(

			'action1'=>'path.to.ActionClass',

			'action2'=>array(

				'class'=>'path.to.AnotherActionClass',

				'propertyName'=>'propertyValue',

			),

		);

	}

	*/

}

Please let me know what is wrong…Thanks

HI MikeShawn, Merry Christmas!

Your code is long enough and personally I couldn’t find the error without regenerate with many tests

(may other members too)

The best way to solve this, is to check step by step your code. So I recommend you to make a test action and build your conditions step by step with testing

Then it will be more easy to help you (and may other members of Yii) :)

Are your tables created?

Hi Mike,

As you should have already noticed from the error message, the error comes from the SQL that you have constructed.

I think this part won’t give an error.

But the last line doesn’t seem to work as you are expecting.

Probably you wanted to write:




$genreCriteria->condition = "GenreId = :gid";

$genreCriteria->params = array(':gid' => $_GET["gid"]);



Or:




$genreCriteria->compare("GenreId", (isset($_GET["gid"]));



Note that you should not write like this:




/* bad example */

$genreCriteria->condition = "GenreId = " . $_GET["gid"];



"select" part should be




$artistCriteria->select = "DISTINCT `t1`.`Name`, `t1`.`ArtistId`";



You miss the comma between the 2 fields.

And also the condition part should be corrected as in the 1st part.

The condition part should be corrected.

The table alias of "t2" is not necessary.

Thanks everyone for responding so fast especially on X-mas :0 I will make changes as everyone requested and update my thread on results. I love Yii so far and glad there is such a great community for support! Thanks again!

Ok I have done the changes but still have Sql error but its almost fixed. Thanks again for help!!

CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound. The SQL statement executed was: SELECT ‘AlbumId’ , ‘GenreId’, ‘ArtistId’, ‘Title’, ‘Price’, ‘AlbumArtUr’ FROM tbl_album t WHERE GenreId = :gid ORDER BY ‘ArtistId’ ASC

My new edited StoreController.php


CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound. The SQL statement executed was: SELECT 'AlbumId' , 'GenreId', 'ArtistId', 'Title', 'Price', 'AlbumArtUr' FROM `tbl_album` `t` WHERE GenreId = :gid ORDER BY 'ArtistId' ASC

O, I’m sorry.

I should have written




$genreCriteria->compare("GenreId", $_GET["gid"]);



And the error message shows that you forgot to set the parameter.




$albumCriteria->condition = "GenreId = :gid";

$albumCriteria->params = array(':gid' => $_GET["gid"]);