Jump to content
mickeymouse

Getting data from DB -Array within Array

Recommended Posts

After a query which is iterative depending on a variable count (like $childcnt between 1 & 10), I place the data into an array $Cild[$childcnt]

But then I can't access the data within each array,

Is my code not correct?

$Child[$childcnt] = mysql_fetch_row($Results);
            print("$Child[$childcnt[1]] - $Child[$childcnt[2]]");

Tks

Share this post


Link to post
Share on other sites

Is there any other code?  You're not changing the counter so it's going to keep overwriting the same element.  You can also just push the results onto the end of the array without a counter.

Share this post


Link to post
Share on other sites

Sorry I didn't want to bother you with all the code (now below) and sorry I didn't explain the problem properly.

My problem is not with the logic but with the syntax.

What happens is I get  Parse error: syntax error, unexpected '[', expecting ']'

If I don't have a solution to this, then I can try it as you suggest - just push results to the end.

But I would prefer to have a solution to my syntax problem.

Tks.

$childcnt=1;
$childdisp=11;
$childtotal = 0;
while($childcnt<10)
    {if($Row[$childdisp]<>"")
        {//================= GET CHILD =========================================
        $query = "SELECT * FROM  names WHERE `Ref`='$Row[$childdisp]' ";
        $Results = mysql_query($query, $link) or die ('Error = '. mysql_error());
        $AffectedRows = mysql_affected_rows($link);
        if($AffectedRows < 1)
            {$Child[$childcnt[1]]=="$Row[$childdisp] Not in the Database.";
            $Child[$childcnt[2]]=="B";}
        else {$Child[$childcnt] = mysql_fetch_row($Results);
            print("$Child[$childcnt[1]] - $Child[$childcnt[2]]");}  <-------SYNTAX  PROBLEM????
        $childcnt = $childcnt + 1;
        $childdisp = $childdisp + 1;
        $childtotal = $childtotal + 1;}
    else {$childcnt = 10;}
}

 

Share this post


Link to post
Share on other sites

Since you have that in double quotes, it's trying to replace the variable names with their values, but it sounds like it's getting confused.  If you want to print those values then don't put them inside a quoted string, or use curly brackets to make everything explicit.

http://php.net/manual/en/language.types.string.php#language.types.string.parsing

I'm not sure what you're trying to print there, because $childcnt is a number but you're trying to access it like an array.

Share this post


Link to post
Share on other sites

I must put double quotes or printing doesn't work.

e.g.  Print(ABC<br>)  gives me error:  unexpected <

I am trying to print the 1st item in the array $Child[$childcnt]

I am not trying to print $childcnt.   $childcnt is indeed a number.  It is the nth child and it is used to define which $Child array of the many I could have

and as created by the earlier code:  $Child[$childcnt] = mysql_fetch_row($Results);

I expect to end up with Array $Child[1] containing an Array of child info

followed by Array $Child[2] containing an Array of child 2 info, and so on.

Then I'm trying to print the info from each array.

Share this post


Link to post
Share on other sites

OK.  I got around the problem with the following code.

$Child = mysql_fetch_row($Results);
            $SavedName[$childcnt] = $Child[1];
            $SavedSex[$childcnt] = $Child[2];}

print("$SavedName[$childcnt], $SavedSex[$childcnt]<br>");

(The print actually occurs later on in my code as it has to fit into a form after some other printing).

Many thanks for your help.

Share this post


Link to post
Share on other sites

I must put double quotes or printing doesn't work.

I don't know what you mean by that.

print('The value of $Child[$childcnt] is ' . $Child[$childcnt] . ', and the value of $Child[$childcnt - 1] is ' . $Child[$childcnt - 1] . '<br>');

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×