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 10 of 10
  1. #1
    New Coder
    Join Date
    Oct 2010
    Location
    United Kingdom
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Why does this function keep printing the result?!!!

    Hi all,

    Wonder if you can help me with this dilema...

    I have a function:

    PHP Code:
    function short_name($website_domain$network)
    {
    include (
    '/database_connection.php');

    $result mysql_query("SELECT id, provider_code, provider_short_name "
    " FROM providers "
    " WHERE (providers.id = '".$network."' || providers.provider_code = '".$network."') "
    " AND display = 1 LIMIT 1") or die(mysql_error());

    while (
    $row mysql_fetch_array($resultMYSQL_ASSOC)) 
    {
    $return $row['provider_short_name'];
    }
    return 
    $return;


    Now, I want to call on that function, and store the result in a variable called $page_title along with a suffix text:

    $page_title = short_name($website_domain, $network). ' some other text here';


    Then, later on in the page, I want to use $page_title (it's going to be passed to the template file).

    Now, the variable $page_title IS getting passed to the page title where it is supposed to be...
    BUT...
    where I am saving the function result into the $page_title variable, the result is echoing out on the page!

    I didn't think this was supposed to happen?!!!!


    I have done some research and I found a function (on tizag.com) doing a similar task, and tried it and it works perfectly! Why does the example below work, and mine does not!

    PHP Code:
    function mySum($numX$numY){
        
    $total $numX $numY;
        return 
    $total
    }
    $myNumber 0;
    echo 
    "Before the function, myNumber = "$myNumber ."<br />";
    $myNumber mySum(34); // Store the result of mySum in $myNumber
    echo "After the function, myNumber = " $myNumber ."<br />"
    So this example does not echo out the mySum(3, 4) result until it is supposed to...

    Why will mine not do that?!!!

    Many thanks for reading,

    look forward to your comments

    K

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    First, you realize in your while loop, you're overwriting $return over and over, so you'll only get one result?

    Regardless, can you show us your exact code where you're function is called? And a few lines around it? Have you searched (using a search function I mean) for the function name to make sure you didn't accidentally put an echo out while testing and missed it?

  • #3
    New Coder
    Join Date
    Oct 2010
    Location
    United Kingdom
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi Keleth

    The reason for the overwriting is that.... you're not gonna like this, but, all the examples i have read with respect to mysql queries all use the while loop after retrieving data as their is always multiple rows being retrieved!!!
    I have limited my result to one so there is only one result, but I'm not sure how to use that one result!

    I have tried

    PHP Code:
    mysql_fetch_array($resultMYSQL_ASSOC)
    return 
    $result[1]; 
    but that failed!!!! So I use what I know - can you help with that??!!


    So, now for the big issue at hand,,,

    I isolated the function and the calling of the function on to a tester page.. I.e. removng all other scripting and... Hey presto it works as it should!
    But this is strange...
    I do call the function earlier on in the page, in fact a few times, and in those cases the function result is echo'd out as it should be.



    ****************
    ***************


    OK... I am being a complete fool....

    I was echoing out the $page_title a couple of lines down!!!

    Thank you for asking for more code!!!! Amazing how one can overlook these little things - sometimes concentrating too much on what appears to be the problem and not what could be the problem!!!!

    K

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    If I've understood you correctly:

    Code:
    function short_name($website_domain, $network)
    {
            include ('/database_connection.php');
    
            $result = mysql_query("SELECT provider_short_name "
                    . " FROM providers "
                    . " WHERE (providers.id = '".$network."' || providers.provider_code = '".$network."') "
                    . " AND display = 1 LIMIT 1") or die(mysql_error());
    
            return mysql_result($result);
    }
    
    $page_title = short_name($website_domain, $network);
    Last edited by MattF; 11-11-2010 at 09:53 AM.

  • #5
    New Coder
    Join Date
    Oct 2010
    Location
    United Kingdom
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi MattF

    You have understood me perfectly!!!!

    The following is what I needed:

    PHP Code:
    return mysql_result($result); 
    Strangely, I have been using mysql for a good while now but never knew this because in most cases, i am returning a number of rows and therefore use a while statement to go through them. Only very occasionally do i use LIMIT 1 and never knew exactly how to get the result!!!!!!

    I looked it up a few times but every example used a while loop - i gave up looking after a while... Even now, I Googled it and couldn't find the answer!!!

    Thank you!

    K

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    You're welcome. Looking at that return again, you'd be better off changing that to:

    Code:
    return ((mysql_num_rows($result)) ? mysql_result($result) : 'Default title');
    just so that you can return a default if there's no result:

    Code:
    $page_title = short_name($website_domain, $network); # This will be 'Default title' if no entry exists in the DB.
    Last edited by MattF; 11-13-2010 at 02:26 PM.

  • #7
    New Coder
    Join Date
    Oct 2010
    Location
    United Kingdom
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi MattF

    Actually, I was having a couple of issues once i implemented it...

    I looked up the reasons for this and it was suggested to write the code as:

    PHP Code:
    return mysql_result($result0); 
    which is what i am using and it is working very well...

    It is a good idea to have a default, i agree - good consideration...

    Currently using:

    PHP Code:
    function network_name($website_domain$network)
    {
    //include ('connect_to_database.php');

            
    $result mysql_query("SELECT provider_short_name "
                    
    " FROM providers "
                    
    " WHERE (providers.id = '".cleaner($network)."' || providers.provider_code = '".cleaner($network)."') "
                    
    " AND display = 1 LIMIT 1") or die(mysql_error());

            return 
    mysql_result($result0);
    }

    $page_title network_name($website_domain$network); 

    NOTE: cleaner() is a function used to prevent MySQL injection!

    So should i use:

    PHP Code:
    function network_name($website_domain$network)
    {
    //include ('connect_to_database.php');

            
    $result mysql_query("SELECT provider_short_name "
                    
    " FROM providers "
                    
    " WHERE (providers.id = '".cleaner($network)."' || providers.provider_code = '".cleaner($network)."') "
                    
    " AND display = 1 LIMIT 1") or die(mysql_error());

           return ((
    mysql_num_rows($result)) ? mysql_result($result0) : 'Default title');

    }

    $page_title network_name($website_domain$network); 

    Thank you very much for your help!

    K
    Last edited by theside; 11-14-2010 at 12:06 PM. Reason: Put 0 in the wrong place on last bit of code!

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Crap. I'd forgotten about the row number parameter. Apologies. One of the drawbacks of using layers. You tend to forget the parts you do automatically in the class. I'd definitely go with the latter option and set some default just incase. Chances of there being no title might be slim to none, but it's the better option to make sure the var is always set rather than empty.

  • #9
    New Coder
    Join Date
    Oct 2010
    Location
    United Kingdom
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hey thanks again..

    But...

    Can you believe this - another error!!!!

    This time, I am using the code as above BUT WITHOUT the default option - I have yet to put that in place (because as you said above very unlikely that there will be no result returned, as a check is actually done before hand...). Anyway

    This is the error in my error log:

    PHP Code:
    PHP Warning:  mysql_result() [<a href='function.mysql-result'>function.mysql-result</a>]: Unable to jump to row 0 on MySQL result index 24 in 

    Although, the code is actually working on the website!

    K
    Last edited by theside; 11-14-2010 at 12:29 PM. Reason: spelling mistake!

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Can't see any reason for that. It shouldn't even be attempting to fetch a result if no row exists.


    Edit: That is unless you're still using this variant in the function?

    Code:
    return mysql_result($result);


  •  

    Posting Permissions

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