Jump to content


Photo

error accessing mysql_fetch_row results


  • Please log in to reply
5 replies to this topic

#1 mysterio

mysterio
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 September 2005 - 08:17 PM

The following php code is failing when a valid connection to a mysql database exists. I know $row is not empty because I have verified that.

$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$status = $row['status'];
$logincount = $row['logincount'];


$status and $logincount have no value, i.e. = '', done this way. However, the calls

$status = $row[3];
$logincount = $row[4];

work and assign the correct value to $status and $logincount. Any idea why? It's so much easier to read if you can access the values by strings and not numbers, especially if you reorder your database.


#2 marker5a

marker5a
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 15 September 2005 - 08:34 PM

What is the mysql_query?

#3 mysterio

mysterio
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 September 2005 - 08:41 PM

What is the mysql_query?

View Post



The query is

$sql = "SELECT * FROM users WHERE email = '$email' and password = '$pwd_hash' LIMIT 1";

The query does return a value because mysql_num_rows($sql) != 0 when I execute the query.

#4 czambran

czambran
  • Staff Alumni
  • Advanced Member
  • 52 posts
  • LocationFairfield,IA

Posted 15 September 2005 - 08:50 PM

change:
$result = mysql_query($sql);

to
$result = mysql_query($sql) or die(mysql_error());

and post back the error message u get.
Chris
Using undefined variables in PHP is not an error, just sloppy coding practice. PHP5 Power Programming BOOK
--------------------------------------------------------------------------
PHP F.A.Q.
Linux User #394154

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 16 September 2005 - 02:57 AM

If you want to use an associative array, you need to tell mysql to give it to you.
[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]
[span style=\"color:#0000BB\"]<?php
$result [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"]);
[/span][span style=\"color:#0000BB\"]$row [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_fetch_assoc[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$result[/span][span style=\"color:#007700\"]); [/span][span style=\"color:#FF8000\"]// changed mysql_fetch_row to mysql_fetch_assoc
[/span][span style=\"color:#0000BB\"]$status [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$row[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'status\'[/span][span style=\"color:#007700\"]];  [/span][span style=\"color:#FF8000\"]// now these two lines will work
[/span][span style=\"color:#0000BB\"]$logincount [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$row[/span][span style=\"color:#007700\"][[/span][span style=\"color:#DD0000\"]\'logincount\'[/span][span style=\"color:#007700\"]];
[/span][span style=\"color:#0000BB\"]?>
[/span]
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

Ken

#6 mysterio

mysterio
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 16 September 2005 - 07:23 AM

Thanks Ken. That works.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users