Results 1 to 11 of 11

Thread: how to change $sql when result is null or false

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    447

    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.
    Compare bible texts (and other tools):
    TheWheelofGod

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    447

    Re: how to change $sql when result is null or false

    Quote Originally Posted by kows View Post
    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.
    Compare bible texts (and other tools):
    TheWheelofGod

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    447

    Re: how to change $sql when result is null or false

    Quote Originally Posted by kows View Post
    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.
    Compare bible texts (and other tools):
    TheWheelofGod

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    447

    Re: how to change $sql when result is null or false

    Quote Originally Posted by kows View Post
    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);
    ?>
    Compare bible texts (and other tools):
    TheWheelofGod

  8. #8
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.

  9. #9
    Addicted Member
    Join Date
    Feb 2010
    Location
    Damascus - Syria
    Posts
    145

    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.

  10. #10
    Lively Member
    Join Date
    Feb 2008
    Location
    Denmark
    Posts
    113

    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.
    PHP: ClassDB

    VB6:
    Own code: Google Translater

    Over and out from Snortop!!

  11. #11
    New Member
    Join Date
    Sep 2010
    Posts
    6

    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
  •  



Click Here to Expand Forum to Full Width