#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    Function not woring


    Hello guys. I'm tryng to use a function so i can retreive data from my database and i keep geting an error. Here is my function code:

    PHP Code:
    function get_subject_by_id($subject_id){

        global 
    $connection;
        
    $query "SELECT * ";
        
    $query .= "FROM subjects ";
        
    $query .= "WHERE id =" $subject_id;
        
    $query .= " LIMIT 3";
        
    $sql=mysql_query($query,$connection); 
        
    $result_set1 mysql_fetch_assoc($sql);
        
    confirmQuery($result_set1);
        
            return 
    $result_set1;

    and the error code is:
    "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\lynda\widget corp\includes\functions.php on line 37
    Database query failed: 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 'LIMIT 3' at line 1"

    I keep getting this error when i try to call the function on my main page. Anybody has any ideea? I tried different ways to construct my sql query and nothing seems to work! Thank you!
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,382
    Rep Power
    9400
    $subject_id is empty. Maybe null, maybe an empty string.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Here is the code that i'm tryng to use the function on:

    PHP Code:
    <?php require_once("includes/connection.php"); ?> 
    <?php require_once("includes/functions.php"); ?>
    <?php
        
    if(isset($_GET['subj'])){
            
    $sel_subj $_GET['subj'];
            
    $sel_page "";
            } elseif (isset(
    $_GET['page'])){
                
    $sel_subj "";
                
    $sel_page $_GET['page'];
            } else {
                
    $sel_page "";
                
    $sel_subj "";
                echo 
    "No values!";
            }
    $sel_subject get_subject_by_id($sel_subj);  

    ?>
    <?php 
    include("includes/header.php"); ?>
            <table id="structure">
                <tr>
                    <td id="navigation">
                    <ul class="subjects">
                    <?php
                    
                    
                    $subject_set 
    get_all_subjects();
                    while(
    $subjects mysql_fetch_array($subject_set)){
                    echo 
    "<li"
                    if (
    $subjects["id"] == $sel_subj) { echo " class=\"selected\""; }
                    echo 
    "><a href = \"content.php?subj=" 
                    
    urlencode($subjects["id"]) . "\">{$subjects["menu_name"]}</a></li>";
                    
                    
                    
    $page_set get_pages_for_subject($subjects["id"]);
                    echo 
    "<ul class = \"pages\">";
                    while (
    $pages mysql_fetch_array($page_set)) {
                    echo 
    "<li";
                    if(
    $pages["id"] == $sel_page){
                    echo 
    " class = \"selected\"";
                    }
                    echo 
    "><a href=\"content.php?page=" urlencode($pages["id"]) .
                        
    "\">{$pages["menu_name"]}</a></li>";
                    }
                                                   
                    echo 
    "</ul>";
                    }               
                     
    ?>   
                     </ul>
                    </td>
                    <td id="page">
                        <h2><?php get_subject_by_id($sel_subj?></h2>
                        
                        
                        <p>Welcome to staff area.</p>
                        <?php echo $sel_subject?> <br />
                        <?php echo $sel_page?> <br />
                        <ul>
                            <li><a href="content.php">Manage Website Content</a></li>
                            <li><a href="new_user.php">Add Staff User</a></li>
                            <li><a href="logout.php">Logout</a></li>
                        </ul>
                    </td>
                </tr>
        </table>
    <?php require("includes/footer.php"); ?>

    is it necesseary to set a condition on my function not to execute before i send an info with my $_GET variable? How can i do that if that is a solution?
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    119
    Rep Power
    33
    Just move your function call into your if else statement as otherwise if there is no $_GET['subj'] value being sent by the server then the function will try to run with an empty value and therefore you will get the error.

    PHP Code:
    if(isset($_GET['subj'])){ 
            
    $sel_subj $_GET['subj']; 
            
    $sel_page ""
            
    $sel_subject get_subject_by_id($sel_subj);
            } elseif (isset(
    $_GET['page'])){ 
                
    $sel_subj ""
                
    $sel_page $_GET['page']; 
            } else { 
                
    $sel_page ""
                
    $sel_subj ""
                echo 
    "No values!"
            } 
    You can also tidy up this if else statement by declaring empty values for your variables first:
    PHP Code:
    $sel_subj '';
    $sel_page '';
    if(isset(
    $_GET['subj'])){ 
            
    $sel_subj $_GET['subj']; 
            
    $sel_subject get_subject_by_id($sel_subj);
            } elseif (isset(
    $_GET['page'])){ 
                
    $sel_page $_GET['page']; 
            } else { 
                echo 
    "No values!"
            } 
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Thanks it worked!

    Comments on this post

    • Jacques1 disagrees : No, it does *not* "work"! Your code is riddled with security holes. But since you decided the lock the thread for some reason, we can't help you with that.

IMN logo majestic logo threadwatch logo seochat tools logo