|
-
Aug 26th, 2010, 06:36 PM
#1
Thread Starter
Hyperactive Member
how to change $sql when result is null or false
if chapter or book returns false it needs to go to the next book from chapter 1. This is what I have so far:
PHP Code:
$sql = "SELECT DISTINCT book_title FROM ".$dbTable3." WHERE book='".$getBook."' AND chapter='".$getChapter."' ORDER BY id ASC"; $result = mysql_query($sql) or die(mysql_error()); //echo $sql;
while($row = mysql_fetch_array($result)){ $book_title = $row["book_title"]; }
Book is number by the way and not a name as well as the chapter.
-
Aug 26th, 2010, 07:15 PM
#2
Re: how to change $sql when result is null or false
what does "if a chapter or book returns false" mean? if what chapter or book returns false? you mean $getBook and $getChapter? and what is "the next book"? this is all far too vague. explain what you're trying to do.
-
Aug 26th, 2010, 08:39 PM
#3
Thread Starter
Hyperactive Member
Re: how to change $sql when result is null or false
 Originally Posted by kows
what does "if a chapter or book returns false" mean? if what chapter or book returns false? you mean $getBook and $getChapter? and what is "the next book"? this is all far too vague. explain what you're trying to do.
$getBook goes up to 66. It should stop after that. And the chapter $getChapter varies from book to book.
If the chapter exceeds it should change the page.
example. The 1st book has 50 chapters. If the chapter goes to 51 it should change to (header):
?book=2&chapter=1
I think this is a big task. So just tell me how the coding should be in the while section. I'll figure out the rest.
-
Aug 26th, 2010, 09:37 PM
#4
Re: how to change $sql when result is null or false
nothing would go in your while loop. you should check the values of $getBook and $getChapter before you even query the database. because the books have a different number of chapters, you should query the database and count the number of chapters, then check if $getChapter is too high, then redirect to the next book's first chapter. then, if you didn't want to hard code the number of books into your script, you should be querying the database to check if the book number is valid. make any sense?
basic idea of what your script should do:
- assign $getBook and $getChapter
- check if $getBook is a valid book
- if $getBook is invalid, redirect to an error page or something
- check if $getChapter is a valid chapter inside of $getBook
- if $getChapter is not a valid chapter, redirect to the next book's page
- if we get here, both $getBook and $getChapter are valid, so query the database like normal
-
Aug 26th, 2010, 10:40 PM
#5
Thread Starter
Hyperactive Member
Re: how to change $sql when result is null or false
 Originally Posted by kows
nothing would go in your while loop. you should check the values of $getBook and $getChapter before you even query the database. because the books have a different number of chapters, you should query the database and count the number of chapters, then check if $getChapter is too high, then redirect to the next book's first chapter. then, if you didn't want to hard code the number of books into your script, you should be querying the database to check if the book number is valid. make any sense?
basic idea of what your script should do:
- assign $getBook and $getChapter
- check if $getBook is a valid book
- if $getBook is invalid, redirect to an error page or something
- check if $getChapter is a valid chapter inside of $getBook
- if $getChapter is not a valid chapter, redirect to the next book's page
- if we get here, both $getBook and $getChapter are valid, so query the database like normal
The point of this is to insert data in the db table page after page automatically.
I've figured out everything else except for the header and checking if the next chapter exists.
-
Aug 26th, 2010, 10:46 PM
#6
Re: how to change $sql when result is null or false
to see if the next chapter exists, query for that chapter and see if anything is returned. for example:
PHP Code:
extract(mysql_fetch_assoc(mysql_query("SELECT COUNT(*) as count FROM chapters_table WHERE chapter='{$chapter}' AND book='{$book}';"))); echo $count;
if $count is above 0, the chapter exists.
-
Aug 26th, 2010, 11:04 PM
#7
Thread Starter
Hyperactive Member
Re: how to change $sql when result is null or false
 Originally Posted by kows
to see if the next chapter exists, query for that chapter and see if anything is returned. for example:
PHP Code:
extract(mysql_fetch_assoc(mysql_query("SELECT COUNT(*) as count FROM chapters_table WHERE chapter='{$chapter}' AND book='{$book}';")));
echo $count;
if $count is above 0, the chapter exists.
There has to be a DISTINCT placed because this will show the number of verses in the chapter but I want the number of chapters in the book:
PHP Code:
<?php
$add = $getChapter+1;
mysql_query($sql,$con);
$sql = "SELECT COUNT(*) as count FROM ".$dbTable3." WHERE book='".$getBook."' AND chapter='".$add."'";
extract(mysql_fetch_assoc(mysql_query($sql)));
//echo $sql;
echo $count; //amount of verses in the chapter
if(){
$goto = "HTTP://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?book=".$getBook."&chapter=".$row["chapter"];
}
mysql_close($con);
header("Location: ".$goto);
?>
-
Aug 27th, 2010, 01:46 AM
#8
Re: how to change $sql when result is null or false
the query is just making sure that the chapter exists at all, the actual count doesn't matter. if you want to add a distinct and use it for something else, go ahead and do that.
-
Sep 14th, 2010, 06:47 AM
#9
Addicted Member
Re: how to change $sql when result is null or false
Hello
this is very simple, just test the rows of the resource like that:
PHP Code:
$query = "your SQL statement";
$result = mysql_query($query);
if (mysql_num_rows($result)){
// there is a result and this is a valid resource
} else {
// sorry, no result, change your sql and retry
}
Last edited by fjober; Sep 14th, 2010 at 06:50 AM.
-
Sep 17th, 2010, 11:48 AM
#10
Lively Member
Re: how to change $sql when result is null or false
i would have made function, something similar to thise.
Code:
function Get_book($sql)
{
$result = mysql_query($sql);
if(! isset($result)) { return false; }
return mysql_fetch_array($result);
}
function main()
{
do
{
$dbTable3 = ;
$getBook = ;
$getChapter = ;
}while($book = Get_book("SELECT DISTINCT book_title FROM ".$dbTable3." WHERE book='".$getBook."' AND chapter='".$getChapter."' ORDER BY id ASC"));
}
Nevermind seem to have a problem!!!
Last edited by snortop; Sep 17th, 2010 at 12:02 PM.
-
Sep 21st, 2010, 11:31 PM
#11
New Member
Re: how to change $sql when result is null or false
Can you explain me what you exactly want to do?? As here are pretty different code and replies and the thread is also 2 days old so let me know if still you are facing the problem. In that case I will try to resolve it only way and then share code with you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|