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
nite4000
Forum Contributor
Posts: 209 Joined: Sun Apr 12, 2009 11:31 am
Post
by nite4000 » Wed Apr 23, 2014 7:01 pm
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
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Wed Apr 23, 2014 7:54 pm
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
Post
by nite4000 » Thu Apr 24, 2014 5:30 am
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[];
}
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Thu Apr 24, 2014 6:30 am
$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
Post
by nite4000 » Thu Apr 24, 2014 6:35 am
conn is in a include file at the top so it is defined
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Thu Apr 24, 2014 6:42 am
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
Post
by nite4000 » Thu Apr 24, 2014 7:04 am
I changed the query to this
$sql = 'SELECT * FROM accounts WHERE acct_status = "Active",$conn';
is that what you meant or a different way
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Thu Apr 24, 2014 7:19 am
$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
Post
by nite4000 » Thu Apr 24, 2014 7:21 am
does that return accounts will that show me results or would I echo accounts?
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Thu Apr 24, 2014 7:24 am
... and just noticed a typo.
should be
It was right in the OP but wrong in later posts.
nite4000
Forum Contributor
Posts: 209 Joined: Sun Apr 12, 2009 11:31 am
Post
by nite4000 » Thu Apr 24, 2014 7:31 am
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