Re: array in PHP

From: Oli Filth (oli_filth_at_eatspam.coldmail.com)
Date: 12/04/04


Date: Sat, 04 Dec 2004 04:59:07 GMT

This is your exact code? Surely this is giving you loads of run-time errors?
There's still brackets and dollar-signs and quotes missing all over the place.
Not to mention that you're referring to $lijst and $Lijst, which PHP treats as
two entirely separate variables.

Make sure you have display_errors turned on, either by editing php.ini, or by using:
        error_reporting(E_ALL);
at the top of your script. Then try running the script, and it will tell you
where the first syntax error is each time.

Also, you're using MySQL in a very inefficient way. Rather than requesting every
single row in the table and then searching them yourself for a match, you can
get MySQL to do it for you using:

==== BEGIN PHP CODE ====

        $Opdracht = "SELECT * FROM $Tabelnaam WHERE id=$idnr";
        $Resultaat = mysql_db_query($DBNaam, $Opdracht, $Verbinding);

        if ($Rij = mysql_fetch_array($Resultaat))
        {
                $Lijst[$m]["Naam"] = $Rij["Naam"];
                $Lijst[$m]["Vader"] = $Rij["Vader"];
                $Lijst[$m]["Moeder"] = $Rij["Moeder"];
        }

        mysql_close($Verbinding);

===== END PHP CODE =====

Oli

Piet wrote:
> Function MaakLijst ($idnr){
>
> global $Lijst;
>
> $m=1;
>
> $Lijst = Leesnamen($idnr, $m);
>
> print (Slijst[1]["Naam"]; //result: Pete
>
> for ($n=1; $n<=7;$n++){
>
> $Vader=$Lijst[$n]["Vader"];
>
> $Moeder=$Lijst[$n]["Moeder"];
>
> if ($Vader!="1"){
>
> $lijst=Leesnamen($Vader, $n*2);
>
> $Lijst=Leesnamen($Moeder, ($n*2)+1);
>
> }
>
> }
>
> print (Slijst[1]["Naam"]; //result: Empty
>
> print (Slijst[2]["Naam"]; //result: Chris
>
>
>
>
>
> return $Lijst;
>
> }
>
> Function LeesNamen($idnr, $m) {
>
> $x=False; // Variabele stops while lus when record is fount
>
> //variables for acces to database
>
> $Host = "localhost";
>
> $Gebruiker = "User";
>
> $Wachtwoord = "";
>
> $DBNaam = "Parenteel";
>
> $Tabelnaam="familieleden";
>
> $Verbinding=mysql_connect($Host, $Gebruiker, $Wachtwoord);
>
> $Opdracht = "Select * from $Tabelnaam";
>
> $Resultaat =mysql_db_query ($DBNaam, $Opdracht, $Verbinding);
>
> //Haal de resultaten uit de database
>
> while (($Rij = mysql_fetch_array ($Resultaat)) && ($x==FALSE)) {
>
> if ($Rij[id]==$idnr) {
>
> //print("HIT!!<BR>");
>
> $Lijst[$m]["Naam"]=$Rij[Naam];
>
> $Lijst[$m]["Vader"]=$Rij[Vader];
>
> $Lijst[$m]["Moeder"]=$Rij[Moeder];
>
> $x=True;
>
> }
>
> }
>
> $x=False;
>
> mysql_close($Verbinding);
>
> }
>
> return ($Lijst);
>
> }
>
> "d" <d@example.com> schreef in bericht
> news:5c_rd.69942$F7.2426@fe1.news.blueyonder.co.uk...
>
>>Can you copy and paste your exact script here? It seems something very
>>subtle is happening :)
>>
>>"Piet" <p.potjer@chello.nl> wrote in message
>>news:c1_rd.56485$lN.37579@amsnews05.chello.com...
>>
>>>You're right: in my message I did not mention the quotes. I did in my
>>>script. Sorry.
>>>So the problem remains: The content of List[1]["Name"} disappears when I
>>>call the function again.
>>>
>>>"d" <d@example.com> schreef in bericht
>>>news:PqZrd.79502$38.8284@fe2.news.blueyonder.co.uk...
>>>
>>>>For one thing, you should put your textual keys in quotes, so
>>>>$List[1][Name] becomes $List[1]["Name"]. PHP shouldn't support the
>>>>method you use (as, technically, Name should be a constant in your
>>>>script, not a textual key) - it just does to prevent people's code from
>>>>breaking :)
>>>>
>>>>Try changing that and see if it helps.
>>>>
>>>>If not, put some values in your array then put this command at the very
>>>>end of your script:
>>>>
>>>>var_dump($List);
>>>>
>>>>It will display the contents of the $List variable, letting you see if
>>>>things are as they should be.
>>>>
>>>>d
>>>>
>>>>"Piet" <p.potjer@chello.nl> wrote in message
>>>>news:jjZrd.56081$lN.17932@amsnews05.chello.com...
>>>>
>>>>>Hi,
>>>>>
>>>>>I try to use a two-dimension array in a function.
>>>>>It is declared as global (global $List).
>>>>>When it is filled the first time everything seems to be okay:
>>>>>$List[1][Name]="Pete";
>>>>>When I call the function again I fill it like
>>>>>$List[2][Name]="Chris";
>>>>>The result seems good, but the foolish effect is that $List[1][Name] is
>>>>>empty then.
>>>>>What is going wrong? Can anybody help me?
>>>>>(I use PHP 4.3.8.)
>>>>>
>>>>
>>>>
>>>
>>
>
>



Relevant Pages