SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    not pulling down from database properly

    Hi,

    I have been trying to create a small CMs with a Staff page, content page and index page. I have created a navigation system and this appears to work fine. The database has fields id, menu_name, position, visible, and content.

    the content page is a trial page. A lot of it is html and css. I am trying out the php in the first main box in the centre.

    However when i try to draw down the contents of the table, when I click on home I get the home contents "this is home". But when i click on other menu items the same content comes up - "this is home".

    http://www.books3333.com/content.php
    The functions are as follows:

    PHP Code:
     function confirm_query($result){
        global 
    $connection;
        if(!
    $result){
        die(
    "Database query failed".mysqli_error($connection));
    }
    }
    /*---------------------------------------get_menu_names----------------------------------------*/
    function get_menu_names(){
        global 
    $connection
        
    $query="Select * From pages ";
        
    $result=mysqli_query($connection,$query);
        
    confirm_query($result);
        return 
    $result;
    }
    /*--------------------------------------Read database--------------------------------------------*/
    function get_subjects_by_id($subject_id){
        global 
    $connection;
        
    $query "SELECT * FROM pages ";
        
    $result=mysqli_query($connection,$query);
        
    confirm_query($result);
        if(
    $subject mysqli_fetch_array($result)){
        return 
    $subject;
        }else{
        return 
    NULL;
    }

    The content page is as follows:

    PHP Code:
     <?php include("includes/connect.php");
    include(
    "includes/functions.php");
    if(isset(
    $_GET['entry'])){
        
    $sel_name $_GET['entry'];
        }else{
        
    $sel_name "";
    }
    $allpages get_subjects_by_id($sel_name);
    include(
    "includes/header.php");
    ?>
    <link href="temps1.css" rel="stylesheet" type="text/css">


    <div class="menu">
    <ul >
    <?php
    //get-menu-names() draws down the data. The while loop puts the data in the right place in the list.
    $result get_menu_names();
        while(
    $row=mysqli_fetch_array($result )){
        echo 
    "<li><a href=\"content.php?entry="
        
    .urlencode($row['id'])."\">"
        
    .$row['menu_name']."</a></li>";
        }
        
    ?>
    </ul>

    </div><!--end menu-->
    </div><!--end outerbanner-->
    <div  class="maincontent"><div class="newsbox"><h3><b><u>Corporate News</u></b></h3><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque ornare, nibh a posuere viverra, leo leo varius felis, nec molestie nisi arcu quis lorem.</p><h4 align="center" class="style1"><u>New Policies</u></h4>
    <p> Fusce dignissim, tortor et ullamcorper eleifend, diam felis auctor mauris, mollis blandit est lacus vel nulla. Vestibulum ante ipsum primis. Fusce dignissim, tortor et ullamcorper eleifend, diam felis auctor mauris, mollis blandit est lacus vel nulla.</p><h4 align="center" class="style1"><u>Wind of Change</u></h4> 
    <p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Fusce porttitor nisl ac tellus tincidunt adipiscing. </p></div><!--endof newsbox-->
    <div class="splash"><div class="servicepics1" >
    <div class="servicepic"><img src="images/offices1.jpg" /></div><!--pic1-->
    <div class="servicepic"><img src="images/officemeeting1.jpg" /></div><!--pic2-->
    <div class="servicepic"><img src="images/officeadvice1.jpg" /></div><!--pic3-->
    <div class="servicepic"><img src="images/officeGirl1.jpg" /></div><!--pic4-->
    </div><!--end of servicepics wrap-->
    </div><!--end of splash-->
    <div class="databox">
      <div class="data1"><?php echo $allpages['content'];   ?> </div>
        <!--end of data1-->
    I have not put all the html and css for this page in.

    Can any one see where I am going wrong?

    Thanks for your help

    cass27

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your get_subjects_by_id() function does not actually use the subject_id passed into it. You need to add a WHERE clause to the query to only get the row for that ID.

    PHP Code:
    $query "SELECT * FROM pages WHERE id = $subject_id"

  3. #3
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,

    I did as you suggested but I got this error message:

    Database query failedYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

    The function now looks like this:

    PHP Code:
     function get_subjects_by_id($subject_id){
        global 
    $connection;    
        
    $query "SELECT * FROM pages WHERE id = $subject_id ";    
        
    $result=mysqli_query($connection,$query);
        
    confirm_query($result);
        if(
    $subject mysqli_fetch_array($result)){
        return 
    $subject;
        }else{
        return 
    NULL
    Any ideas?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Does $subject_id have a value in it?

  5. #5
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Your get_subjects_by_id() function does not actually use the subject_id passed into it. You need to add a WHERE clause to the query to only get the row for that ID.

    PHP Code:
    $query "SELECT * FROM pages WHERE id = $subject_id"
    You might need to add single quotes in the WHERE clause:

    PHP Code:
    $query "SELECT * FROM pages WHERE id = '$subject_id'"
    Denny Schlesinger
    web services

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'm assuming his id column is numeric since all the values in his links are numbers. Numeric columns aren't quoted.

  7. #7
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    I'm assuming his id column is numeric since all the values in his links are numbers. Numeric columns aren't quoted.
    But if empty it will throw the error he reported.
    Denny Schlesinger
    web services

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I'm guessing the error is originating here:

    Code:
    if(isset($_GET['entry'])){ 
        $sel_name = $_GET['entry']; 
        }else{ 
        $sel_name = ""; 
    }
    The default should be the ID of the default page, not a blank.

  9. #9
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, Thanks for all your input.

    The solution was the commas around '$subject_id'

    I adapted this script from one on the web as i am still a bit of a newbie with php. What I am still trying to get my head around is when you need to pass an argument and when you dont need to. And what that argument should be.


    cheers

    cass27

  10. #10
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cass27 View Post
    What I am still trying to get my head around is when you need to pass an argument and when you dont need to. And what that argument should be.


    cheers

    cass27
    Cass:

    Think of a function as a car body shop. If you want them to work on your car, you have to drive your car to the shop. If you want them to work on your wife's car, then that is the car or 'argument' or 'parameter' you pass to the body_shop($car) function.


    PHP Code:
    function body_shop($car) {
      
    work on $car;
      return 
    fixed $car;
    }

    $myCar 'VW';
    // to fix my car
    body_shop($myCar);

    $wifeCar 'Mercedes';
    // to fix wife's car
    body_shop($wifeCar); 
    Denny Schlesinger
    web services

  11. #11
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i find the fastest way to debug SQL is to dump the SQL string before each "query" and run them through mysql manualy

    that way i see WHERE the SQL string looks wierd, or the result is not what i expected!

  12. #12
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Denny wrote:

    Cass:

    Think of a function as a car body shop. If you want them to work on your car, you have to drive your car to the shop. If you want them to work on your wife's car, then that is the car or 'argument' or 'parameter' you pass to the body_shop($car) function.



    PHP Code:
    function body_shop($car) { work on $car; return fixed $car;}$myCar = 'VW';// to fix my carbody_shop($myCar);$wifeCar = 'Mercedes';// to fix wife's carbody_shop($wifeCar);


    This makes sense. $car although an empty variable in the function is a kind of class in a way.

    So why are thier times when no argument is passed?

    cass

    p.s. Surely i should have the mercedes and my wife should have the VW....?

  13. #13
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cass27 View Post
    So why are thier times when no argument is passed?

    cass
    I suppose the answer to that depends on how the function is written. The SQL query function that started this thread expected a non-empty number and it didn't bother to check if the incoming data was valid. Since it wasn't, the function blew up. Let's call it "less than optimal coding." By adding the single quotes, I prevented the function from blowing up from bad or missing data but it probably still gives bad results when passed an empty argument.

    Since functions work on data it makes sense to pass them arguments. The only time you would not do so is if there are default values the function can use. For example, the function time() takes a void argument because it uses "now" as the default argument.
    Denny Schlesinger
    web services


Bookmarks

Posting Permissions

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