fetch_assoc() - does it support disparate field elements?

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
jharbin
Forum Newbie
Posts: 5
Joined: Sun Jun 22, 2008 10:00 am

fetch_assoc() - does it support disparate field elements?

Post by jharbin »

Hi. I'm unable to get the fetch_assoc() function to return all fields from a select query. The first two fields, both integer-type, display fine, but the other fields -- all char types -- display nothing at all. Do all fields have to have the same type for fetch_assoc to work properly?

I'm using PHP 5.2.0 with MySQL 5.01. My code is below, and again, the first two fields (Week, Game) display fine. (When I inserted the fields, they all used addslashes.)

for ($i=0; $i <$num_results; $i++)
{
$row = $result->fetch_assoc();
echo '<br>Week: ';
echo stripslashes($row['week']);
echo '<br>Game: ';
echo stripslashes($row['game']);
echo '<br>Visitor: ';
echo stripslashes($row['visitor']);
...etc.
}

Can anyone help, please?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: fetch_assoc() - does it support disparate field elements?

Post by califdon »

jharbin wrote:Do all fields have to have the same type for fetch_assoc to work properly?
Surely not. I can't see anything wrong with your syntax, but I'd suggest removing the stripslashes to see if that's involved. I'm not sure what stripslashes does, if anything, when applied to a numeric field type (although I suppose php casts it as a char).
jharbin
Forum Newbie
Posts: 5
Joined: Sun Jun 22, 2008 10:00 am

Re: fetch_assoc() - does it support disparate field elements?

Post by jharbin »

califdon,

I tried removing the stripslashes, as well as the addslashes I used when inserting, and started with fresh data. I get the same results -- the two integer fields display properly, and I get nothing for the char fields.

I also tried using mysqli_fetch_assoc, with no better results. I'm stumped...
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: fetch_assoc() - does it support disparate field elements?

Post by Eran »

var_dump the $result variable. In my opinion those columns are simply returned as empty strings (check your table contents)
jharbin
Forum Newbie
Posts: 5
Joined: Sun Jun 22, 2008 10:00 am

Re: fetch_assoc() - does it support disparate field elements?

Post by jharbin »

pytrin,

var_dump returns: object(mysqli_result)#2 (0) { } - But the first two fields are listed properly, and doing a select in MySQL on the table shows the fields populated properly also.

???
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: fetch_assoc() - does it support disparate field elements?

Post by Eran »

dump the $row variable as well, what does it say under those fields?
jharbin
Forum Newbie
Posts: 5
Joined: Sun Jun 22, 2008 10:00 am

Re: fetch_assoc() - does it support disparate field elements?

Post by jharbin »

pytrin,

When I remove $row, so the code looks like:

// echo ($row['visitor']);
echo ($visitor);

PHP repeats the current $visitor for each record - in this case, 'ABC' repeated 16 times.

The syntax I'm trying to emulate (from PHP and MySQL Web Development) does employ the $row function.

I'm going to delete my problematic table, re-create it, re-populate it, and try again. Maybe I messed up when I was inserting records with addslashes.

I'll let you know if my surgery works.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: fetch_assoc() - does it support disparate field elements?

Post by Eran »

i meant var_dump the $row variable, not remove it from the code...
jharbin
Forum Newbie
Posts: 5
Joined: Sun Jun 22, 2008 10:00 am

Re: fetch_assoc() - does it support disparate field elements?

Post by jharbin »

pytrin,

I've got things working after dropping the table and recreating it. I messed around with assigning data types and that may have caused my problem, but the PHP syntax I was using is fine, minus the addslashes & stripslashes.

I should have known PHP wasn't messing up -- I think an "operator error" with MySQL was causing the problem.

I didn't know about the var_dump function (I'm new to PHP), and it looks very helpful.

Thanks for your advice -- and patience with a newbie!

Joe Harbin
Saint Paul, MN
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: fetch_assoc() - does it support disparate field elements?

Post by Eran »

cheers :)
Post Reply