I have afunction to retrieve data and it gives error.
Please correct me!

public function getswachapter($book, $chapter){
			//SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id;		
			//remember to sanitize inputs
			$sql = mysql_query("SELECT * FROM kiswahili WHERE Chapter = '{$chapter}' AND Book = '{$book}'")or die(mysql_error());
			$limit = mysql_num_rows($sql);
		    
			$query = "SELECT  kiswahili.Chapter, kiswahili.Verse, kiswahili.Scripture, vitabu.Name FROM kiswahili  WHERE kiswahili.Chapter = '{$chapter}' AND kiswahili.Book = '{$book}'  INNER JOIN vitabu ON kiswahili.Book=vitabu.Book LIMIT {$limit} ";// INNNER JOIN  vitabu ON kiswahili.id = vitabu.id 
	        echo $query;
			$res = mysql_query($query) or die(mysql_error());
	        return  $res;
	    }

Can you provide the error so we can have some idea what is going on?

First: What is the error you get
Second: why are you using different indents?
this is a) very ugly and b) will give errors

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 'INNER JOIN vitabu ON kiswahili.Book=vitabu.Book LIMIT 31' at line 1

public function getswachapter($book, $chapter){
	//SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id;
	//remember to sanitize inputs
	$sql = mysql_query("SELECT * FROM kiswahili WHERE Chapter = '{$chapter}' AND Book = '{$book}'")or die(mysql_error());
	$limit = mysql_num_rows($sql);

	$query = "SELECT  kiswahili.Chapter, kiswahili.Verse, kiswahili.Scripture, vitabu.Name FROM kiswahili  WHERE kiswahili.Chapter = '{$chapter}' AND kiswahili.Book = '{$book}'  INNER JOIN vitabu ON kiswahili.Book=vitabu.Book LIMIT {$limit} ";// INNNER JOIN  vitabu ON kiswahili.id = vitabu.id
	echo $query;
	$res = mysql_query($query) or die(mysql_error());
	return  $res;
}

Give this a try.

public function getswachapter($book, $chapter){
//SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id;
//remember to sanitize inputs
    $sql = mysql_query("SELECT * FROM kiswahili WHERE Chapter = '{$chapter}' AND Book = '{$book}'")or die(mysql_error());
    $limit = mysql_num_rows($sql);

    $query = "SELECT kiswahili.Chapter, kiswahili.Verse, kiswahili.Scripture, vitabu.Name FROM kiswahili INNER JOIN vitabu ON kiswahili.Book=vitabu.Book WHERE kiswahili.Chapter = '{$chapter}' AND kiswahili.Book = '{$book}'  LIMIT {$limit} ";// INNNER JOIN vitabu ON kiswahili.id = vitabu.id
    echo $query;
    $res = mysql_query($query) or die(mysql_error());
    return $res;
}
Member Avatar for diafol

Do you really need to run 2 queries? I don't know your table structures, but the first query looks redundant.

This works :)
What was wrong with mine?

The first query was for purpose of testing limit
The second one is the actual query

Thanks guys

Member Avatar for diafol
public function getswachapter($book, $chapter){
    //you still need to clean inputs!
    $query = "SELECT kiswahili.Chapter, kiswahili.Verse, kiswahili.Scripture, vitabu.Name FROM kiswahili INNER JOIN vitabu ON kiswahili.Book=vitabu.Book WHERE kiswahili.Chapter = '{$chapter}' AND kiswahili.Book = '{$book}'";
    $res = mysql_query($query) or die(mysql_error());
    return $res;
}

Does this not work?

You used an inner join after the where clause..

You used an inner join after the where clause..

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.