mardi 31 mars 2015

MEAN with mySQL

I'm trying to learn angularjs and I want to use mySQL instead of MongoDB in my MEAN stack. Coming from a LAMP background, I am having trouble understanding the organizational/functional structure of the MEAN stack. I have a result query I would like to obtain from a mySQL database and display it is a filterable table with user-re-sizable columns on my webpage, which will appear below a header and navigation bar. From examples I've seen, this is my current folder structure.



├── app/
│ ├── controllers/
│ ├── models/
│ ├── routes/
│ └── views/
├── config/
│ └── env/
├── mysql.conf
├── package.json
├── public/
│ ├── css/
│ ├── index.html
│ └── js/
└── Server.js


I know this isn't much to work with. In PHP, I basically had all my code in one .php file. # mySQL connection info $con=mysqli_connect("$host","$user","$pw","myDatabase"); if (mysqli_connect_errno($con)){ echo "Failed to conenct to MySQL: " . mysqli_connecterror(); } $sqlSelect = "SELECT * FROM myTable"; $queryResult = mysqli_query($con, "$sqlSelect") or die(mysqli_error); $queryRowNum = mysqli_num_rows($queryResult);



$columnNameArr = [];
$i = 0;
while($nameField = mysqli_fetch_field($queryResult)){
$columnNameArr[$i] = $nameField->name;
$i++;
}
?>


Then I could use the $queryRowNum variable to print out the rows.



<table>
<?php //populate data rows
foreach ($queryResult as $row){
echo "<tr>";
foreach ($row as $col){
echo "<td>$col</td>";
}
echo "</tr>";
}
?>
</table>


So the data fetching and HTML output is all done in the same file. How do I make this more modular and more MEAN compliant?


Aucun commentaire:

Enregistrer un commentaire