Page 1 of 1

fetch_assoc() - does it support disparate field elements?

Posted: Sun Jun 22, 2008 10:14 am
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?

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

Posted: Sun Jun 22, 2008 11:45 am
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).

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

Posted: Sun Jun 22, 2008 12:06 pm
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...

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

Posted: Sun Jun 22, 2008 12:16 pm
by Eran
var_dump the $result variable. In my opinion those columns are simply returned as empty strings (check your table contents)

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

Posted: Sun Jun 22, 2008 12:38 pm
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.

???

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

Posted: Sun Jun 22, 2008 2:22 pm
by Eran
dump the $row variable as well, what does it say under those fields?

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

Posted: Sun Jun 22, 2008 4:39 pm
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.

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

Posted: Sun Jun 22, 2008 4:55 pm
by Eran
i meant var_dump the $row variable, not remove it from the code...

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

Posted: Sun Jun 22, 2008 5:15 pm
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

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

Posted: Sun Jun 22, 2008 5:25 pm
by Eran
cheers :)