SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Query help please.

    Hi Guys,

    In my query I want to output data entered in four columns in MYSQL database:

    - manual_link_one_txt
    - manual_link_one_url
    - manual_link_two_txt
    - manual_link_two_url

    Now if there is data entered into these columns for a unique record the data is outputted – great!

    …BUT

    If there is NO data entered in these columns for a unique record I want it to output the message sorry, there are no related stories – this bit does not work.

    If nothing is entered in these columns for a unique record it just outputs two dot.jpg images and I can’t understand why.

    Can someone take a look at my query? I’ve asked others and they are confused to why this is doing it as well!

    Thanks

    PHP Code:
    <? 
    $query 
    "SELECT manual_link_one_txt, manual_link_one_url, manual_link_two_txt, manual_link_two_url FROM cms_stories where story_id=".$_GET['story_id']; 
    $result mysql_query($query) OR die(mysql_error());     
    $defineResults mysql_num_rows($result); 
    if (
    $defineResults == 0)  { 
        echo (
    "Sorry, there are no related stories"); 

    } else { 

        while(
    $row mysql_fetch_assoc($result))   { 
            echo 

            <img src='../images/story_images/dot.jpg'><a href='
    {$row[manual_link_one_url]}'>{$row[manual_link_one_txt]}</a><br /> 
            <img src='../images/story_images/dot.jpg'><a href='
    {$row[manual_link_two_url]}'>{$row[manual_link_two_txt]}</a><br />
            "

        } 
    ?>

  2. #2
    SitePoint Addict ruba's Avatar
    Join Date
    Apr 2005
    Location
    Amman -Jordan
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know exactly what to do , but you can use SQL_CALC_FOUND_ROWS
    you can find

    Note: If you use mysql_unbuffered_query(), mysql_num_rows() will not return the correct value until all the rows in the result set have been retrieved.


    example ,
    Code:
     $temp = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM $table LIMIT 1");
    $result = mysql_query("SELECT FOUND_ROWS()");
    try to use it
    Open Blocked website
    Open Blocked Website
    Knowledge Is Knowing That A Tomato Is A Fruit,
    Wisdom Is Not Putting It In A Fruit Salad.

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    If there is nothing in the row to output, nothing will be output!
    I would hazard a guess that if you viewed the source of the webpage you would have something like
    Code:
    <img src='../images/story_images/dot.jpg'><a href=''></a><br /> 
    <img src='../images/story_images/dot.jpg'><a href=''></a><br />
    in which I also noticed that you aren't closing the <a href> tag after the variables
    PHP Code:
    <img src='../images/story_images/dot.jpg'><a href='{$row[manual_link_one_url]}'>{$row[manual_link_one_txt]}[b]'>[/b]</a><br /> 
    Use an if statement to check if the field is empty@
    PHP Code:

            
    while($row mysql_fetch_assoc($result))   { 
            if(
    $row[manual_link_one_txt] != '') {
            echo 

            <img src='../images/story_images/dot.jpg'><a href='
    {$row[manual_link_one_url]}'>{$row[manual_link_one_txt]}</a><br /> 
            <img src='../images/story_images/dot.jpg'><a href='
    {$row[manual_link_two_url]}'>{$row[manual_link_two_txt]}</a><br /> 
            "

            } else {
                echo 
    'sorry, there are no related stories';
            }
        } 
    You can also use empty() to check the field.

    You could also do it with the SQL directly to only pull the records that have data:
    Code:
    $query = "
    	SELECT 
    		manual_link_one_txt, 
    		manual_link_one_url, 
    		manual_link_two_txt, 
    		manual_link_two_url 
    	FROM 
    		cms_stories 
    	where 
    		story_id=".$_GET['story_id'] ."
    	and
    		manual_link_one_txt != ''	
    		";
    Try one of those
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


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
  •