Jump to content

error accessing mysql_fetch_row results


mysterio

Recommended Posts

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.