Dynamisch Array aus MySQL mit PHP auslesen.

Begonnen von claude, 26. August 2007, 17:12:56

Vorheriges Thema - Nächstes Thema

claude

Guten Tag zusammen.
Ich möchte eine dynamische Array mit PHP auslesen. Die Array ist in MySQL gespeichert. Welche Befehl kann ich nutzen?
$variable1 ist eine Normal String.
$variable2 ist eine Dynamisch Array.
Eine oder mehrere \"Option\" über $variable1 werden in $variable2 gespeichert.
EINGABE Formular:
 
 


\';
     $sql = \'INSERT INTO tabelle (variable1, variable2) VALUES (?, ?)\';
     $kommando = $db->prepare($sql) ;
     $kommando->bind_param(\'ss\',  $_POST[\'variable1\'], $_POST[\'variable2\']);
     $kommando->execute();
     echo \'SQL geschickt.\';
     $db->close();
     echo \'Verbindung zu.\';
    } catch (Exception $e) {
 echo \'Fehler: \' . htmlspecialchars($e->getMessage());
 }
}
?>
 
Variable1:
Variable2:
Option1
Option2
Option3
Option4
Option5
 
 

 
 
Mehrere Option von eine Select List werden durch eine Eingabe Formular in MySQL gespeichert.
Danach wird diesem Info von einer Abfrage Formular abgerufen (in Kombination mit anderer Tabelle).
 
ABFRAGE Formular:
 


$db = new MySQLi(\'localhost\', \'root\', \'\', \'datenbank\');  
$sql = \'SELECT tabelle.variable1, tabelle.variable2 FROM tabelle\';  
$kommando = $db->prepare($sql);  
$kommando->execute();  
$kommando->bind_result($variable1, $variable2);  
While ($kommando->fetch())  
{  
printf(\'%s%s\',  
$variable1, $variable2);  
}  
$db->close();  

 
Welchem Befehle Kann ich nutzen?
Es gibt vielleicht einem besseren Design um dass zu verarbeiten.
Vorschläge?

k00ni

Hallo,
wo liegt nun genau das Problem?
Welchem Befehle Kann ich nutzen?Es gibt vielleicht einem besseren Design um dass zu verarbeiten.
[/quote]
Ich nehme mal an, dass dein Array sauber eingelesen () und übertragen wird. Dann würde ich das nicht \"direkt\" in der Datenbank ablegen, sondern daraus per http://de2.php.net/manual/de/function.implode.php\" rel=\"external nofollow\">implode-Befehl  einen String machen und dann diesen speichern. Ob man direkt PHP-Arrays speichern kann, weiß ich nicht. Es wäre da beispielsweise per Serialisierung möglich, für deinen Fall aber scheinbar zu aufwendig und übertrieben.
Hast du dann den Array in der Tabelle, so kannst du bei der Abfrage mit dem http://de2.php.net/manual/de/function.explode.php\" rel=\"external nofollow\">explode-Befehl, den String wieder in ein Array umwandeln.
Ich hoffe, dass es das war, was du meintest.  /uploads/emoticons/icon_e_biggrin.gif.1a84f5257b36e14b36d04985314f877f.gif\" alt=\":-D\" />
[edit]Wenn du Probleme mit der deutschen Sprache hast, dann kann ich dir das auch in englisch schreiben. Fiel mir nur grad auf beim nochmaligen Lesen deines Beitrages. /uploads/emoticons/icon_e_smile.gif.f7ec63a2b1c3d90a9415e40455642502.gif\" alt=\":-)\" />[/edit]
Grüße

claude

Well darn! Is it so obvious!!!  /uploads/emoticons/icon_e_sad.gif.cc8ba2b6b966c5e020020efa47702aab.gif\" alt=\":(\" />  OK OK I am trying to learn PHP,  MySQL AND the German language at the same time!!    [/uploads/emoticons/icon_e_surprised.gif.a005678239f11b45b64b526b2c82e9a1.gif\" alt=\":o\" />]
 not so easy!  sometime I feel I am going  :gaga:
I am also learning to use Forum for the first time, someone realized that I posted my problem on various Forum and they brought to my attention a few rules about cross- and multi-postings. I apologize if I did something wrong here. I only meant good.
Anyhow, from what I gathered, to read an Array is not so easy and might not be the right plan for what I am building.
I am building a health database:
variable1 => Symptom (ex.: headache)
variable2 => Nutrients needed to cure (ex.: Vitamin A, C, E)
 
One symptom has sometime 0 vitamin, some other time 1 or more vitamins.
Clients will enter the needed vitamin(s) for a symptom from an online PHP entry form with  to choose from. This information will be saved in a MySQL database. Later on this information will be retrieved online from a query form.
This is the simple overview of the project, later on there will be different queries with other kind of related informations inserted in the database.
So now that you have a better idea on what I am working on, I will ask this question:
Should I save the vitamins in a dynamic Array? Or work with relational database?
Thank you very much for your help and your offer on the english support!   /uploads/emoticons/icon_e_wink.gif.c059000ae48ff64afa53be0962c021f2.gif\" alt=\":wink:\" />

k00ni

Hey,
sorry for late posting.
Clients will enter the needed vitamin(s) for a symptom from an online PHP entry form with  to choose from. This information will be saved in a MySQL database. Later on this information will be retrieved online from a query form.[/quote]
Should I save the vitamins in a dynamic Array? Or work with relational database?[/quote]
I think the database way is a good choice. As i wrote in the last post, use the function http://de2.php.net/manual/en/function.implode.php\" rel=\"external nofollow\">implode. After that save the convert string (variable2) in the table.
Here the example from php.net:
 


$array is your \"variable2[]\" and will convert to a string with \",\" as seperators. It\'s easy to take this string to an array back, with the function http://de2.php.net/manual/de/function.explode.php\" rel=\"external nofollow\">explode. I\'ll try to give you same example-code: Here is the input-form. I modify your code a little bit, that it can convert the variable2-array into a string.
\';  
     $sql = \'INSERT INTO tabelle (variable1, variable2) VALUES (?, ?)\';  
     $kommando = $db->prepare($sql) ;  
     $kommando->bind_param(\'ss\',  $_POST[\'variable1\'], $_POST[\'variable2\']);  
     $kommando->execute();  
     echo \'SQL abgesendet\';  
     $db->close();  
     echo \'Verbindung geschlossen.\';  
    }
catch (Exception $e) {  
 echo \'Fehler: \' . htmlspecialchars($e->getMessage());  
 }
?>


 
 
And now the query-form:
 

prepare($sql);    
$kommando->execute();    
$kommando->bind_result($variable1, $variable2);    
While ($kommando->fetch())    
{    
printf(\'%s%s\',  
$variable1, $variable2);    
}    
$db->close();
?>

 
 
I dont modify the code. It should print line per line, the value of variable1 and the list (variable2, e.g. vitamin1, vitamin2,...)
If you need to work with the string as an array, than use function explode. And modify the while-loop:
 

fetch())    
{    
$A_vitamine_array [$I_counter] = explode (\',\', $variable2);
$I_counter++;
printf(\'%s%s\',  
$variable1, $variable2);    
}
?>

 
 
I hope this helps. Please forgive my poor english. Maybe it sounds like i translate with google translation from german to english to turkish to frensh and back /uploads/emoticons/icon_e_surprised.gif.a8707b3f35a569cb4cfe563fc72ef78d.gif\" alt=\":-o\" />
 
Grüße

claude

Fantastic! It works!  [/uploads/emoticons/icon_e_surprised.gif.a005678239f11b45b64b526b2c82e9a1.gif\" alt=\":o\" />]
I tried it before with \"implode\" but I didn\'t write the code properly, now wit your help I can finally read the whole Array in my Query. I can read the Array with or without the \"explode\", both ways work.
Since I just need to read the Array in the Query I will use the simpler form without the code \"explode\".
Thank you very much for giving me the right way to write the Code.   :H:
My next challenge is to use the vitamins listed in the array and connect them to another table wich will list different kind of foods containing these vitamins.
I guess I need to work with MySQL in order to build the relations between the tables.
If you have any suggestions for this second step of my programming I would be very thankful.
I thank you very much for the help you already provided, it was clear, simply and perfectly explained and the presentation of the properly written code did the trick!
Thanks again!
PS: Your english is perfect!  /uploads/emoticons/icon_e_wink.gif.c059000ae48ff64afa53be0962c021f2.gif\" alt=\":wink:\" />

all your base are belong to us / Discord