0

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;
	    }
4
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by Stefano Mtangoo
0

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

0

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;
}
0

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;
}
0

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

0
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?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.