Jump to content

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]]");


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.


$childtotal = 0;
        {//================= 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.";
        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;}


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.


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.

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.

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.

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>');


Link to post
Share on other sites

Join the conversation

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

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...