...

View Full Version : Why does this function keep printing the result?!!!



theside
11-11-2010, 12:54 AM
Hi all,

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

I have a function:



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($result, MYSQL_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!


function mySum($numX, $numY){
$total = $numX + $numY;
return $total;
}
$myNumber = 0;
echo "Before the function, myNumber = ". $myNumber ."<br />";
$myNumber = mySum(3, 4); // 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

Keleth
11-11-2010, 01:10 AM
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?

theside
11-11-2010, 01:44 AM
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


mysql_fetch_array($result, MYSQL_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

MattF
11-11-2010, 09:51 AM
If I've understood you correctly:



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);

theside
11-13-2010, 02:12 PM
Hi MattF

You have understood me perfectly!!!!

The following is what I needed:


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

MattF
11-13-2010, 02:19 PM
You're welcome. Looking at that return again, you'd be better off changing that to:



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


just so that you can return a default if there's no result:



$page_title = short_name($website_domain, $network); # This will be 'Default title' if no entry exists in the DB.

theside
11-14-2010, 12:03 PM
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:


return mysql_result($result, 0);

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:


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($result, 0);
}

$page_title = network_name($website_domain, $network);


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

So should i use:


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($result, 0) : 'Default title');

}

$page_title = network_name($website_domain, $network);


Thank you very much for your help!

K

MattF
11-14-2010, 12:19 PM
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. :D 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.

theside
11-14-2010, 12:28 PM
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 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

MattF
11-14-2010, 04:22 PM
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?



return mysql_result($result);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum