Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Database query failed

    I always get this error: "Database query failed". Does someone knows what the problem is?

    CONTENT.PHP
    PHP Code:
    <?php require_once("includes/functions.php"); ?>
    <?php 
    require_once("includes/header.php"); ?>
    <?php 
        
    if(isset($_GET['subj'])) {
            
    $sel_subj $_GET['subj'];    
            
    $sel_page "";
        } elseif(isset(
    $_GET['page'])) {
            
    $sel_subj "";
            
    $sel_page $_GET['page'];    
        } else {
            
    $sel_subj "";
            
    $sel_page "";    
        }
        
    $sel_subject get_subject_by_id($sel_subj);
    ?>
    <?php 
    include("includes/connection.php"); ?>
    <table id="structure">
        <tr>
            <td id="navigation">&nbsp;
                <ul class="subjects">
            <?php
                $subject_set 
    get_all_subjects();
                
                while(
    $subject mysql_fetch_array($subject_set)) {
                    echo 
    "<li";
                    if (
    $subject["id"] == $sel_subj) { echo " class=\"selected\"";    }
                    
                    echo 
    "><a href=\"content.php?subj=" urlencode($subject["id"]) . 
                        
    "\">{$subject["menu_name"]}</a></li>";
        
                    
    $page_set get_pages_for_subject($subject["id"]);
                
                    echo 
    "<ul class=\"pages\">";
                    while(
    $page mysql_fetch_array($page_set)) {
                        echo 
    "<li";
                        if (
    $page["id"] == $sel_page) { echo " class=\"selected\"";    }
                        echo 
    "><a href=\"content.php?page=" 
                        
    urlencode($page["id"]) . "\">{$page["menu_name"]}</a></li>";
                    }
                    echo 
    "</ul>";
                }
            
    ?>
                </ul>
            </td>
            <td id="page">
                <h2><?php echo $sel_subject['menu_name']; ?></h2>
                <br />
                <?php echo $sel_page?><br />
             </td>
        </tr>
    </table>
    <?php include("includes/footer.php"); ?>
    FUNCTIONS.PHP
    PHP Code:
    <?php
    function confirm_query($result_set) {
        if(!
    $result_set) {
            die(
    "Database query failed: " .mysql_error());
        }
    }

    function 
    get_all_subjects() {
        global 
    $connection;
        
    $query "SELECT * 
                 FROM subjects 
                 ORDER BY position ASC"
    ;
        
    $subject_set mysql_query($query $connection);
        
    confirm_query($subject_set);
        return 
    $subject_set;    
    }

    function 
    get_pages_for_subject($subject_id) {
        global 
    $connection;
        
    $query "SELECT * 
                 FROM pages 
                 WHERE subject_id = {$subject_id} 
                 ORDER BY position ASC"
    ;
        
    $page_set mysql_query($query$connection);
        
    confirm_query($page_set);
        return 
    $page_set;
    }

    function 
    get_subject_by_id($subject_id) {
        global 
    $connection;
        
    $query "SELECT * ";
                 
    "FROM subject ";
                 
    "WHERE id=" $subject_id ." ";
                 
    "LIMIT 1";
        
    $result_set mysql_query($query$connection);
        
    confirm_query($result_set);
        if(
    $subject mysql_fetch_array($result_set)) {
            return 
    $subject;
        } else {
            return 
    NULL;    
        }
        
    }

    ?>

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by RDLyTN View Post
    I always get this error: "Database query failed". Does someone knows what the problem is?

    CONTENT.PHP
    PHP Code:
    <?php require_once("includes/functions.php"); ?>
    <?php 
    require_once("includes/header.php"); ?>
    <?php 
        
    if(isset($_GET['subj'])) {
            
    $sel_subj $_GET['subj'];    
            
    $sel_page "";
        } elseif(isset(
    $_GET['page'])) {
            
    $sel_subj "";
            
    $sel_page $_GET['page'];    
        } else {
            
    $sel_subj "";
            
    $sel_page "";    
        }
        
    $sel_subject get_subject_by_id($sel_subj);
    ?>
    <?php 
    include("includes/connection.php"); ?>
    <table id="structure">
        <tr>
            <td id="navigation">&nbsp;
                <ul class="subjects">
            <?php
                $subject_set 
    get_all_subjects();
                
                while(
    $subject mysql_fetch_array($subject_set)) {
                    echo 
    "<li";
                    if (
    $subject["id"] == $sel_subj) { echo " class=\"selected\"";    }
                    
                    echo 
    "><a href=\"content.php?subj=" urlencode($subject["id"]) . 
                        
    "\">{$subject["menu_name"]}</a></li>";
        
                    
    $page_set get_pages_for_subject($subject["id"]);
                
                    echo 
    "<ul class=\"pages\">";
                    while(
    $page mysql_fetch_array($page_set)) {
                        echo 
    "<li";
                        if (
    $page["id"] == $sel_page) { echo " class=\"selected\"";    }
                        echo 
    "><a href=\"content.php?page=" 
                        
    urlencode($page["id"]) . "\">{$page["menu_name"]}</a></li>";
                    }
                    echo 
    "</ul>";
                }
            
    ?>
                </ul>
            </td>
            <td id="page">
                <h2><?php echo $sel_subject['menu_name']; ?></h2>
                <br />
                <?php echo $sel_page?><br />
             </td>
        </tr>
    </table>
    <?php include("includes/footer.php"); ?>
    FUNCTIONS.PHP
    PHP Code:
    <?php
    function confirm_query($result_set) {
        if(!
    $result_set) {
            die(
    "Database query failed: " .mysql_error());
        }
    }

    function 
    get_all_subjects() {
        global 
    $connection;
        
    $query "SELECT * 
                 FROM subjects 
                 ORDER BY position ASC"
    ;
        
    $subject_set mysql_query($query $connection);
        
    confirm_query($subject_set);
        return 
    $subject_set;    
    }

    function 
    get_pages_for_subject($subject_id) {
        global 
    $connection;
        
    $query "SELECT * 
                 FROM pages 
                 WHERE subject_id = {$subject_id} 
                 ORDER BY position ASC"
    ;
        
    $page_set mysql_query($query$connection);
        
    confirm_query($page_set);
        return 
    $page_set;
    }

    function 
    get_subject_by_id($subject_id) {
        global 
    $connection;
        
    $query "SELECT * ";
                 
    "FROM subject ";
                 
    "WHERE id=" $subject_id ." ";
                 
    "LIMIT 1";
        
    $result_set mysql_query($query$connection);
        
    confirm_query($result_set);
        if(
    $subject mysql_fetch_array($result_set)) {
            return 
    $subject;
        } else {
            return 
    NULL;    
        }
        
    }

    ?>
    seo.guru, it might be in his functions or header file. Regardless, what's the mysql_error()? Are you telling us the complete error message?

    My guess is a syntax error. It looks like you're only assigning 'SELECT *' as the query from this:
    PHP Code:
    $query "SELECT * ";
                 
    "FROM subject ";
                 
    "WHERE id=" $subject_id ." ";
                 
    "LIMIT 1"
    $query is only getting assigned on the first line of that snippet - the rest are just strings that aren't assigned to variables.
    Last edited by BluePanther; 01-10-2012 at 03:11 PM.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    seo.guru, it might be in his functions or header file. Regardless, what's the mysql_error()? Are you telling us the complete error message?

    My guess is a syntax error. It looks like you're only assigning 'SELECT *' as the query from this:
    PHP Code:
    $query "SELECT * ";
                 
    "FROM subject ";
                 
    "WHERE id=" $subject_id ." ";
                 
    "LIMIT 1"
    $query is only getting assigned on the first line of that snippet - the rest are just strings that aren't assigned to variables.
    I already fixed that, but that wasn't the problem. I still get the same error.

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by RDLyTN View Post
    I already fixed that, but that wasn't the problem. I still get the same error.
    Have you shown the entire error message? Also, post the code that you claim has fixed that problem, just to make sure.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    Have you shown the entire error message? Also, post the code that you claim has fixed that problem, just to make sure.
    PHP Code:
    <?php
    function get_subject_by_id($subject_id) {
        global 
    $connection;
        
    $query "SELECT * "
        
    $query .= "FROM subjects ";
        
    $query .= "WHERE id=" $subject_id " "
        
    $query .= "LIMIT 1";
        
    $result_set mysql_query($query$connection);
        
    confirm_query($result_set);
        if(
    $subject mysql_fetch_array($result_set)) {
            return 
    $subject;
        } else {
            return 
    NULL;    
        }
    }
    ?>
    He only shows: "Database query failed:", he doesn't show the mysql_error()

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by RDLyTN View Post
    PHP Code:
    <?php
    function get_subject_by_id($subject_id) {
        global 
    $connection;
        
    $query "SELECT * "
        
    $query .= "FROM subjects ";
        
    $query .= "WHERE id=" $subject_id " "
        
    $query .= "LIMIT 1";
        
    $result_set mysql_query($query$connection);
        
    confirm_query($result_set);
        if(
    $subject mysql_fetch_array($result_set)) {
            return 
    $subject;
        } else {
            return 
    NULL;    
        }
    }
    ?>
    He only shows: "Database query failed:", he doesn't show the mysql_error()
    For now, change to this:
    PHP Code:
        $result_set mysql_query($query$connection) or die(mysql_error($connection)); 
    That should pump out a better error.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #7
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    For now, change to this:
    PHP Code:
        $result_set mysql_query($query$connection) or die(mysql_error($connection)); 
    That should pump out a better error.
    I already tried that, now I won't get any error at all.

  • #8
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by RDLyTN View Post
    I already tried that, now I won't get any error at all.
    On content.php, are these not backwards?
    PHP Code:
    <?php  
        
    if(isset($_GET['subj'])) { 
            
    $sel_subj $_GET['subj'];     
            
    $sel_page ""
        } elseif(isset(
    $_GET['page'])) { 
            
    $sel_subj ""
            
    $sel_page $_GET['page'];     
        } else { 
            
    $sel_subj ""
            
    $sel_page "";     
        } 
        
    $sel_subject get_subject_by_id($sel_subj); 
    ?> 
    <?php include("includes/connection.php"); ?>
    Shouldn't the connection be before the get_subject_by_id()? That would explain the lack of mysql_error() messages. If this is the case, I'm very surprised PHP didn't moan a lot about using mysql_* functions without a valid mysql connection identifier.
    Edit: Woo hoo, my 1000th post! :P
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #9
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    On content.php, are these not backwards?
    PHP Code:
    <?php  
        
    if(isset($_GET['subj'])) { 
            
    $sel_subj $_GET['subj'];     
            
    $sel_page ""
        } elseif(isset(
    $_GET['page'])) { 
            
    $sel_subj ""
            
    $sel_page $_GET['page'];     
        } else { 
            
    $sel_subj ""
            
    $sel_page "";     
        } 
        
    $sel_subject get_subject_by_id($sel_subj); 
    ?> 
    <?php include("includes/connection.php"); ?>
    Shouldn't the connection be before the get_subject_by_id()? That would explain the lack of mysql_error() messages. If this is the case, I'm very surprised PHP didn't moan a lot about using mysql_* functions without a valid mysql connection identifier.
    Edit: Woo hoo, my 1000th post! :P
    LOL W00T! Thanks my friend, I searched a whole day after this problem!

  • #10
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    no problem

    One more thing - your site is quite open to SQL injection. I would advise using mysql_real_escape_string() around any user input (GET or POST values).
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #11
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BluePanther View Post
    no problem

    One more thing - your site is quite open to SQL injection. I would advise using mysql_real_escape_string() around any user input (GET or POST values).
    Oke, I'll do that. Thanks for your help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •