Unstoppable Domains

What's wrong here? [Php]

Spacemail by SpaceshipSpacemail by Spaceship
Watch
Impact
19
hey
i am not getting an error..it just doesnt show anything when it should

PHP:
		function loadMedia($limit,$cid=-1)
		{
		$media = array();
		if ($cid == -1) {
		$where = "WHERE `status` = 1"; }
		$q = "SELECT * FROM `media` $where order by `timestamp` desc LIMIT 0,$limit";
		$result = $this->query($q);
		for ($b = 0; $b <(mysql_num_rows($result));$b++)
		{
			$db = mysql_fetch_array($result);
			$media[$b] = array($db['id'],$db['cat_id'],$db['by_id'],$db['file_name'],$db['dp'],$db['title'],
						$db['description'],$db['views'],$db['timestamp']);
		}
		return $media or die ("cant");;
		}
and then :
PHP:
<?$media = $database->loadMedia(1); echo$media['0']['5'];?>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
It sounds like $cid != -1.

Either do "$cid = -1" before you call the function, or do it after $media = array();.
 
0
•••
EDIT:
I didn't notice before.

Change:
PHP:
return $media or die ("cant");;

To:
PHP:
return $media;

That should fix it. With the 'or die' currently in the function, it will not be returning the array.


Possible Shorty but since he did not supply the second argument when using the function it should have been -1 by default. By it could be :P

Run this:
PHP:
echo '<pre>';
print_r( $media )
echo '</pre>';

To see if the array is populated.

As a side note it does not matter in this case however you do not use '' for numerical array keys.
 
Last edited:
0
•••
You should have had an error for an unexpected semi colon, too. When you are trying to find what's wrong, use error_reporting(E_ALL);
 
0
•••
Dan said:
You should have had an error for an unexpected semi colon, too. When you are trying to find what's wrong, use error_reporting(E_ALL);

The extra semi colon wasn't the problem. (although could well have been in other cases)

I tried a little experiment since i didn't know 100% if it was the problem but:

PHP:
function test($test) 
{
	$string = 'hi';
	
	if($test == true)
	{
		return $string or die('cant');
	}
	else
	{
		return $string;
	}
}
PHP:
echo test(false);
// will output "hi"

echo test(true);
// will output "1"

Unsure as to why but adding the or die seems to return 1.
 
0
•••
Here i rewrote a little your code.

Hope it works :)

If it's not working then you might have problems with your the mysql class that you are using.

PHP:
function loadMedia ( $limit , $cid = '-1' ){
	
	$media = array();
    
	if ($cid == '-1'){
    	$where = "WHERE `status` = 1";
	}else{
		$where = "";
	}
    
	$q = "SELECT * FROM `media` $where order by `timestamp` desc LIMIT 0,$limit";
    $result = $this->query($q);
    
	$db = mysql_fetch_array($result);
	
	for ($b = 0; $b <(mysql_num_rows($result));$b++){
            
    	$media[$b] = array	(	$db['id'],$db['cat_id'],$db['by_id'],$db['file_name'],$db['dp'],
								$db['title'],$db['description'],$db['views'],$db['timestamp']
							);
    }
	
	return $media;
}
 
0
•••
If you change

if($test == true)
to
if($test === true)

it will run just return $string;

I can't figure out why it makes it return 1..
"void exit ( [string status] )"



Ahtum, you need to have it get the row inside of the for loop to get all of the rows. What you did would only get one.
 
0
•••
Dan said:
If you change

if($test == true)
to
if($test === true)

it will run just return $string;

No it wont? :-/ :hehe:
 
0
•••
Matthew. said:
No it wont? :-/ :hehe:
... not if you use true... He's not using true as the input, so your test doesn't necessarily do anything except for showing that it's weird how it returns 1.
 
0
•••
Dan said:
... not if you use true... He's not using true as the input, so your test doesn't necessarily do anything except for showing that it's weird how it returns 1.

Which was the aim of my test ;) The conditional is irrelevant, it was just to make it easier to swap between the 2 (I'm bored gimme a break.)

PHP:
function test($string) 
{
	return $string or die("cant");
}


:hehe:

I'm determined to find out why it returns 1...
 
0
•••
Matthew. said:
Which was the aim of my test ;) The conditional is irrelevant, it was just to make it easier to swap between the 2 (I'm bored gimme a break.)

PHP:
function test($string) 
{
	return $string or die("cant");
}


:hehe:

I'm determined to find out why it returns 1...

return is a langauge construct not a function therefore it does not return an exit code.

in other words

return $somestring or die("some message") is a pointless line of code
:)
 
0
•••
Amnezia said:
return is a langauge construct not a function therefore it does not return an exit code.

in other words

return $somestring or die("some message") is a pointless line of code
:)

That i know (which is why i said remove it from the original code) :)

My point/query was why it returns 1 :P It is not important, merely something i was wondering :P
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back