fetch_assoc() - does it support disparate field elements?
Moderator: General Moderators
fetch_assoc() - does it support disparate field elements?
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?
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?
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 wrote:Do all fields have to have the same type for fetch_assoc to work properly?
Re: fetch_assoc() - does it support disparate field elements?
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...
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?
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?
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.
???
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?
dump the $row variable as well, what does it say under those fields?
Re: fetch_assoc() - does it support disparate field elements?
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.
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?
i meant var_dump the $row variable, not remove it from the code...
Re: fetch_assoc() - does it support disparate field elements?
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
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