5 Replies - 558 Views - Last Post: 11 August 2010 - 04:22 PM Rate Topic: -----

#1 CodeVillain  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

My echo is returning "Array" instead of the intended value

Posted 11 August 2010 - 02:37 PM

So I'm trying to create my first simple account retrieval, which seems like it's super easy to do, except that that when I tried to test my code by creating an "echo" statement (which I of course won't be doing for the actual account retrieval) all it's returning is "Array Array" for what should be "Login Password". Can anyone shed a little light on this?

My best guess is I'm using the mysql_fetch_assoc incorrectly, but I don't know what else I should use.

Here's the important pieces of the code.

<?php

$qry3 = "SELECT login FROM members WHERE email='$email'";
$result = mysql_query($qry3);
if(!$result)
{
	error_log(mysql_error());
	die("There was an error with the database.  It has been logged.");
}
$login = mysql_fetch_assoc($result);

$qry4 = "SELECT passwd FROM members WHERE email='$email'";
$result1 = mysql_query($qry4);
if(!$result1)
{
	error_log(mysql_error());
	die("There was an error with the database.  It has been logged.");
}
$password = mysql_fetch_assoc($result1);

echo $login . " " . $password;

?>


Is This A Good Question/Topic? 0
  • +

Replies To: My echo is returning "Array" instead of the intended value

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: My echo is returning "Array" instead of the intended value

Posted 11 August 2010 - 02:43 PM

$password = mysql_fetch_assoc($result1);

Because that function returns an ARRAY! Please read the manual.
Was This Post Helpful? 0
  • +
  • -

#3 CodeVillain  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

Re: My echo is returning "Array" instead of the intended value

Posted 11 August 2010 - 03:06 PM

View PostJackOfAllTrades, on 11 August 2010 - 12:43 PM, said:

$password = mysql_fetch_assoc($result1);

Because that function returns an ARRAY! Please read the manual.


Thanks, but that wasn't exactly helpful. As I said in my original post I figured I was using the function incorrectly. Now I'm still stuck with not knowing what function I should be using instead or if there's a better way to be doing it.

I came here to avoid going through each MySQL function one by one until I figured out which one worked, especially since I'm not sure if I should be defining the PHP variable with a MySQL function to begin with.

This post has been edited by CodeVillain: 11 August 2010 - 03:09 PM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2910
  • View blog
  • Posts: 10,075
  • Joined: 08-August 08

Re: My echo is returning "Array" instead of the intended value

Posted 11 August 2010 - 03:18 PM

You should know the table you're accessing, but even if you don't you can still use var_dump or print_r to output it. That will tell you what the keys and values are.
Was This Post Helpful? 1
  • +
  • -

#5 CodeVillain  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

Re: My echo is returning "Array" instead of the intended value

Posted 11 August 2010 - 03:38 PM

I realized I could just be doing this instead. Please keep in mind I just learned how to make a "Hello World" HTML document a little over a year ago, so my inability to figure things out which may seem otherwise obvious I think is somewhat excusable. =P

$userresult = mysql_query("SELECT * FROM members WHERE email='$email'");

$userstuff = mysql_fetch_array($userresult);

$password = $userstuff['passwd'];
$login = $userstuff['login'];

Was This Post Helpful? 0
  • +
  • -

#6 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: My echo is returning "Array" instead of the intended value

Posted 11 August 2010 - 04:22 PM

Only problem with mysql_fetch_array is you're getting an array with both numeric and associative indexes. You should either pass in a flag for one or the other (MYSQL_ASSOC or MYSQL_NUM) as the second parameter, or switch to mysql_fetch_assoc() for an associative array, or mysql_fetch_row() for a numeric array.

Not a big deal, but sometimes small optimizations where possible add up :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1