473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

What have I done wrong when creating this array ?

290 100+
I an trying to create arrays from from mysql table and I thought
that I had the code right but I must have done something wrong ...

This is my code:

Expand|Select|Wrap|Line Numbers
  1. $earn = array();
  2. $comm = array();
  3. $sql = "SELECT earn, comm,  FROM main WHERE id = '$Db_prod' ORDER BY mday_no DESC ";
  4. $result  = mysql_query($sql)    or die("could not execute SELECT cb_main". mysql_error());  
  5. $num = mysql_num_rows($result);
  6. if ($num == 0 ) {
  7.    $earn[] = 0;
  8.    $comm[] = 0;
  9.    }
  10. else {
  11.        while( $row = mysql_fetch_assoc($result) ) {  
  12.             $earn[] = $row['earn'];
  13.              $comm[] =  $row['comm'];   
  14.             }
  15.       } 
  16.  

I have tried this code and I am getting a problem with it:

When I display the resulting array with this:
print_r($earn);

I get:

Array ( [0] => Array ( ) [1] => 34.01 [2] => 33.36 [3] => 33.36 [4] => 82.25 [5] => 82.25 )

So I seem to have created a double array somehow

And hence max($earn); just returns "Array" rather than the
maximum value.

What am I doing wrong ?


.
Feb 9 '10 #1
5 1371
Atli
5,058 Expert 4TB
Hey.

I don't see anything in that code that would cause this. Unless the values returned by MySQL are somehow messed up?

If you just print the values, rather than store them in arrays, what do you get?
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT earn, comm,  FROM main WHERE id = '$Db_prod' ORDER BY mday_no DESC ";
  2. $result  = mysql_query($sql) or die("could not execute SELECT cb_main". mysql_error());
  3. if (mysql_num_rows($result) > 0 )
  4. {
  5.     header('content-type: text/plain;');
  6.     while( $row = mysql_fetch_assoc($result) )
  7.     {
  8.         echo "Earn: {$row['earn']} - Comm: {$row['comm']}\n";
  9.     }
Feb 9 '10 #2
jeddiki
290 100+
I'll try that and let you know, I have to go out for a few hours.

thanks ...


.
Feb 9 '10 #3
jeddiki
290 100+
OK,
I tried that.

And I get a normal output.

I added this to my code:


Expand|Select|Wrap|Line Numbers
  1. echo "Earn: {$row['earn']} <br>";  
and I get this output:

Earn: 17.78

So I do not see where this double depth array
comes from !
Feb 9 '10 #4
jeddiki
290 100+
Thanks for the suggestion

That helped me to Fix it - :)




.
Feb 9 '10 #5
Atli
5,058 Expert 4TB
No problem. Glad I could help :)

Out of curiosity, what was wrong?
Feb 9 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
15
by: M.Siler | last post by:
<HTML> <HEAD> <TITLE></TITLE> <SCRIPT> <!-- var factor_val = new Array(8,7) factor_val = 68.8 factor_val = 55
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
140
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
39
by: windandwaves | last post by:
Hi Folk I have to store up to eight boolean bits of information about an item in my database. e.g. with restaurant drive-through facility yellow windows
5
by: Mercy | last post by:
I guess my C++ is pretty darn rusty. I was just looking over sample C++ code for practice... and I'm kind of confused about this code fragment: int sector2; int i = 3; memset(sector2,...
6
by: jason | last post by:
Hello, I have a question about what kind of datastructure to use. I'm reading collumn based data in the form of: 10\t12\t9\t11\n 24\t11\t4\t10\n ..... I now have a structure which allows me...
10
by: timor.super | last post by:
Hi all, Imagine I've an array of int : int anArray = new int; I want to extract all the integer that are superior to 500 I can do :
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.