mysqli function not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

mysqli function not working

Post by nite4000 »

I have this function I changed to mysqli nd everything is defined however when I try to echo something to see if its working then nothing is showing and there are no errors

Code: Select all


$query  = "SELECT * FROM accounts WHERE acct_status = 'Active'";
	$result = $conn->query($query) or die($conn->error.__LINE__);

	$accounts = array();
	
	while($row = $result->fetch_assoc())
	{
		$accounts[] = $row;
	}
	
	return $accounts;
I would like to leave it like this without removing that array stuff

help would be great
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: mysqli function not working

Post by Celauran »

nite4000 wrote:nothing is showing and there are no errors
Check your error logs. Aside from the fact that it doesn't show the entire function, I didn't see anything obviously wrong with the snippet of code you posted.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: mysqli function not working

Post by nite4000 »

there is no error log and the only part I didn't show was the beginning which here is the entire function

Code: Select all

function get_active_accounts()
{
	
	
	$query  = "SELECT * FROM accounts WHERE acct_status = 'Active'";
	$result = $conn->prepare($query) or die($conn->error.__LINE__);

	$accounts = array();
	
	while($row = $result->fetch_assoc())
	{
		$accounts[] = $row;
	}
	
	return $accounts[];
	
	
	
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: mysqli function not working

Post by Celauran »

$conn isn't defined, which is why this isn't working. That's also going to generate an error, so you might want to check your error reporting settings.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: mysqli function not working

Post by nite4000 »

conn is in a include file at the top so it is defined
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: mysqli function not working

Post by Celauran »

Not within the scope of the function, it isn't. You'll need to pass it in as a parameter.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: mysqli function not working

Post by nite4000 »

I changed the query to this
$sql = 'SELECT * FROM accounts WHERE acct_status = "Active",$conn';

is that what you meant or a different way
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: mysqli function not working

Post by Celauran »

$conn does not exist within the scope of that function. Any variable not passed in as a parameter or defined within the function itself is undefined. You need to add $conn to the function signature and you need to pass it in when calling the function.

Code: Select all

function get_active_accounts($conn)
{
        
        $query  = "SELECT * FROM accounts WHERE acct_status = 'Active'";
        $result = $conn->prepare($query) or die($conn->error.__LINE__);

        $accounts = array();
        
        while($row = $result->fetch_assoc())
        {
                $accounts[] = $row;
        }
        
        return $accounts[];
        
}
and call it as get_active_accounts($conn)
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: mysqli function not working

Post by nite4000 »

does that return accounts will that show me results or would I echo accounts?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: mysqli function not working

Post by Celauran »

... and just noticed a typo.

Code: Select all

return $accounts[];
should be

Code: Select all

return $accounts;
It was right in the OP but wrong in later posts.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: mysqli function not working

Post by nite4000 »

ok yeah I saw that as well.

nothing shows up on the screen when I run this and the return $accounts isn't displaying

also if I try to echo $accounts it always add a [ on the end of it
Post Reply