Last active
June 22, 2017 11:44
-
-
Save michaeldeboeve/6ea062b15bcbe55f8f42b4dd28827d72 to your computer and use it in GitHub Desktop.
mySQL db connect for use in Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api.php | |
<?php | |
// Connect to the Database | |
include 'db_connect.php'; | |
// Query from Database | |
include 'db_main_query.php'; | |
// Insert into the Database | |
// include 'db_insert.php'; | |
mysqli_close($conn); | |
?> | |
// db_connect.php | |
<?php | |
$servername = "localhost"; | |
$username = "root"; | |
$password = "root"; | |
$dbname = "myDB"; | |
$conn = mysqli_connect($servername, $username, $password, $dbname); | |
if(!$conn){ | |
die("Connection failed: " .mysqli_connecet_error()); | |
} | |
?> | |
// db_main_query.php | |
<?php | |
$query = "SELECT * FROM table"; | |
$data = array(); | |
$result = mysqli_query($conn, $query); | |
if(mysqli_num_rows($result) > 0){ | |
while($row = mysqli_fetch_assoc($result)){ | |
$data[] = $row; | |
} | |
} else { | |
echo "0 results"; | |
}; | |
print json_encode($data); | |
?> | |
// db_insert.php | |
<?php | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment