[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Simple array quesion

HI,
I did the following

  $result=mysql_query($sqlString);
  $rv = array();
  while($ret=mysql_fetch_array($result,MYSQL_ASSOC)){
          $rv[]=$ret;
 print_r($rv);
  }

The result is:
Array ( [0] => Array ( [Char_ID] => 274 [Char_Pos_X] => -5.96249 [Char_Pos_Y] => 3.91121 [Char_Pos_Z] => -34.7117 [Char_Dir_X] => -0.49003 [Char_Dir_Y] => 1e-006 [Char_Dir_Z] => -0.871706 ) )

but when i want to retrieve the first value
I use

$rv[0]->Char_ID;

It cannot show the result i.e 274 in my example.
what's wrong?
0
gorexy
Asked:
gorexy
  • 10
  • 6
1 Solution
 
BogoJokerCommented:
Hi gorexy,

Why not just:
$firstValue = $rv[0]?
Do you need the arrow format?

Joe P
0
 
gorexyAuthor Commented:
ok let me try first
Ijust need to arrange the result in a row no special format
0
 
gorexyAuthor Commented:
sorry how to use your soluation in my example?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
BogoJokerCommented:
$charid = $rv[0];  // Now $charid is 274

Joe P
0
 
gorexyAuthor Commented:
Sorry fail

I put

$charid = $rv[0];
print $charid;

it said

ARRAY
0
 
BogoJokerCommented:
How about:
$array = $rv[0];
$charid = $array['Char_ID'];

If that works you might beable to use:
$charid = $rv[0]['Char_ID'];
0
 
gorexyAuthor Commented:
sorry same result

only display ARRAY
0
 
BogoJokerCommented:
If that suggestion doesnt work then I'm afraid I don't know.  Try this debugging.
$array = $rv[0];
print_r($array);
$charid = $array['Char_ID'];

That is what I had above, only now I'm printing $array.  Show me what that prints, it should print out:
Array ( [Char_ID] => 274 [Char_Pos_X] => -5.96249 [Char_Pos_Y] => 3.91121 [Char_Pos_Z] => -34.7117 [Char_Dir_X] => -0.49003 [Char_Dir_Y] => 1e-006 [Char_Dir_Z] => -0.871706 )

I have no clue why it wouldn't.
Joe P
0
 
gorexyAuthor Commented:
$array = $rv[0];
print_r($array);


it shows ARRAY only

nothign else
0
 
gorexyAuthor Commented:
it is funny

How about:
$array = $rv[0];
$charid = $array['Char_ID'];

I use your code to print but fail

but now use this as you suggested, it works

$charid = $rv[0]['Char_ID'];

$charid=274
0
 
gorexyAuthor Commented:
I just wonder why cannot use  '->' to retrieve value
0
 
BogoJokerCommented:
I am going to push the impossibility of this out of my head =)
Do you need the other indexs, the Char_Pos_X,Y,Z and so on.

If Not you can do this instead of what your current code is:
  $result=mysql_query($sqlString);
  $rv = array();
  while($ret=mysql_fetch_array($result,MYSQL_ASSOC)){
          $rv[]=$ret['Char_ID'];
 print_r($rv);
  }
  $charid = $rv[0];

I am also going to point out that there is only 1 row, if there are more you can change this in the future but for now:
  $result=mysql_query($sqlString);
  $rv = array();
  while($ret=mysql_fetch_array($result,MYSQL_ASSOC))
  {
    $charid = $ret['Char_ID'];
    $charposx = $ret['Char_Pos_X'];    
    $charposy = $ret['Char_Pos_Y'];
    $charposz = $ret['Char_Pos_Z'];
    // and so on
  }

For multiple rows you can make them arrays, then do $charid[] = $ret['Char_ID'] and so on.
Is that okay?

I am really baffled as to why that doesnt work.  I doubt it makes a difference but i fyou did:
$array = array();
Then those other lines... I don't get it =(

Joe P
0
 
gorexyAuthor Commented:
anyway for your further sugestions!
0
 
gorexyAuthor Commented:
Thanks!
0
 
BogoJokerCommented:
Did you get it working or did you use the alternate solutions?  =)
Joe P
0
 
gorexyAuthor Commented:
it works now
 $charid = $rv[0]['Char_ID'];

but i face another problem..let see can I fix it first
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now