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

  • Create New...