My better (imho) version of GolezTrol's script. Defaults to Genesis, chapter 1, verse 1 if no information is provided in the URL. Also fixed some syntax errors/misspellings in his script. Also, he forgot the book. ;) Finally, removed excess type casting, and used escaping where necessary:
$book = empty($_GET['book']) ? 'Genesis' : $_GET['book'];
$chapter = empty($_GET['chapter']) ? '1' : $_GET['chapter'];
$verses = empty($_GET['verses']) ? '1' : $_GET['verses'];
$book = mysql_real_escape_string($book);
$chapter = mysql_real_escape_string($chapter);
$verseRanges = explode(',', $verses);
$vC = array();
foreach($verseRanges as $verseRange) {
$vR = explode('-', $verseRange);
foreach ($vR as &$value) {
$value = mysql_real_escape_string($value);
}
$vC[] = count($vR) === 1
? 'verse = ' . $vR[0]
: 'verse BETWEEN ' . implode(' AND ', $vR);
}
$query = "SELECT * FROM Bible WHERE chapter = '" . $chapter . "' ";
$query .= "AND book = '" . $book . "' ";
$query .= 'AND (' . implode(' OR ', $vC) . ')';
Using type casting in lieu of escaping makes your code less readable/understandable. Always code for readability in the future. Also, $_GET
values are already strings.