Page 1 of 1

mysqli function not working

Posted: Wed Apr 23, 2014 7:01 pm
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

Re: mysqli function not working

Posted: Wed Apr 23, 2014 7:54 pm
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.

Re: mysqli function not working

Posted: Thu Apr 24, 2014 5:30 am
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[];
	
	
	
}

Re: mysqli function not working

Posted: Thu Apr 24, 2014 6:30 am
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.

Re: mysqli function not working

Posted: Thu Apr 24, 2014 6:35 am
by nite4000
conn is in a include file at the top so it is defined

Re: mysqli function not working

Posted: Thu Apr 24, 2014 6:42 am
by Celauran
Not within the scope of the function, it isn't. You'll need to pass it in as a parameter.

Re: mysqli function not working

Posted: Thu Apr 24, 2014 7:04 am
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

Re: mysqli function not working

Posted: Thu Apr 24, 2014 7:19 am
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)

Re: mysqli function not working

Posted: Thu Apr 24, 2014 7:21 am
by nite4000
does that return accounts will that show me results or would I echo accounts?

Re: mysqli function not working

Posted: Thu Apr 24, 2014 7:24 am
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.

Re: mysqli function not working

Posted: Thu Apr 24, 2014 7:31 am
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