Jump to content
Sign in to follow this  
claude

Dynamisch Array aus MySQL mit PHP auslesen.

Recommended Posts

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?

Share this post


Link to post
Share on other sites

Hallo,


wo liegt nun genau das Problem?


Welchem Befehle Kann ich nutzen?

Es gibt vielleicht einem besseren Design um dass zu verarbeiten.



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 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 explode-Befehl, den String wieder in ein Array umwandeln.


Ich hoffe, dass es das war, was du meintest. :-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. :-)[/edit]


Grüße

Share this post


Link to post
Share on other sites

Well darn! Is it so obvious!!! :( OK OK I am trying to learn PHP, MySQL AND the German language at the same time!! [: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! :wink:

Share this post


Link to post
Share on other sites

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.


Should I save the vitamins in a dynamic Array? Or work with relational database?


I think the database way is a good choice. As i wrote in the last post, use the function 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 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 :-o



Grüße

Share this post


Link to post
Share on other sites

Fantastic! It works! [: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! :wink:

Share this post


Link to post
Share on other sites
Guest
You are commenting as a guest. If you have an account, please sign in.
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.

Sign in to follow this  

×