1 pièce(s) jointe(s)
Connexion en serveur avec wamp
Salut, je fait une simple connexion avec serveur local wampserveur , je trouve cette probléme
Pièce jointe 224228
code db_config :
Code:
1 2 3 4 5 6 7 8
| <?php
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "appsfactor"); // database name
define('DB_SERVER', "localhost"); // db server
?> |
code db_connect.php :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <?php
/**
* A class file to connect to database
*/
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysqli_connect('localhost', 'root', '','appsfactor') or die(mysql_error());
/* // Selecing database
$db = mysqli_select_db("appsfactor") or die(mysql_error()) or die(mysql_error()); */
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysqli_close($con);
}
}
?> |
code create_product.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <?php
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['nom']) && isset($_POST['email']) && isset($_POST['pass']) && isset($_POST['adresse'])) {
$nom = $_POST['nom'];
$email = $_POST['email'];
$pass = $_POST['pass'];
$adresse = $_POST['adresse'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO products(nom, email, pass,adresse) VALUES('$nom', '$email', '$pass', '$adresse')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?> |
code get_product.php :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| <?php
**
**
// array for JSON response
$response = array();
**
// include db connect class
require_once __DIR__ . '/db_connect.php';
**
// connecting to db
$db = new DB_CONNECT();
**
// check for post data
if (isset($_GET["id"])) {
****$id = $_GET['id'];
**
****// get a product from products table
****$result = mysql_query("SELECT *FROM users WHERE id = $id");
**
****if (!empty($result)) {
********// check for empty result
********if (mysql_num_rows($result) > 0) {
**
************$result = mysql_fetch_array($result);
**
************$user = array();
************$user["id"] = $result["id"];
************$user["nom"] = $result["nom"];
************$user["email"] = $result["email"];
************$user["pass"] = $result["pass"];
************$user["adresse"] = $result["adresse"];
************// success
************$response["success"] = 1;
**
************// user node
************$response["user"] = array();
**
************array_push($response["user"], $user);
**
************// echoing JSON response
************echo json_encode($response);
********} else {
************// no product found
************$response["success"] = 0;
************$response["message"] = "No product found";
**
************// echo no users JSON
************echo json_encode($response);
********}
****} else {
********// no product found
********$response["success"] = 0;
********$response["message"] = "No product found";
**
********// echo no users JSON
********echo json_encode($response);
****}
} else {
****// required field is missing
****$response["success"] = 0;
****$response["message"] = "Required field(s) is missing";
**
****// echoing JSON response
****echo json_encode($response);
}
?> |
code get_all_products:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <?php
**
/*
** Following code will list all the products
**/
**
// array for JSON response
$response = array();
**
// include db connect class
require_once __DIR__ . '/db_connect.php';
**
// connecting to db
$db = new DB_CONNECT();
**
// get all products from products table
$result = mysql_query("SELECT *FROM users") or die(mysql_error());
**
// check for empty result
if (mysql_num_rows($result) > 0) {
****// looping through all results
****// products node
****$response["users"] = array();
**
****while ($row = mysql_fetch_array($result)) {
********// temp user array
************$users = array();
************$users["id"] = $result["id"];
************$users["nom"] = $result["nom"];
************$users["email"] = $result["email"];
************$users["pass"] = $result["pass"];
************$users["adresse"] = $result["adresse"];
**
********// push single product into final response array
********array_push($response["users"], $users);
****}
****// success
****$response["success"] = 1;
**
****// echoing JSON response
****echo json_encode($response);
} else {
****// no products found
****$response["success"] = 0;
****$response["message"] = "No products found";
**
****// echo no users JSON
****echo json_encode($response);
}
?> |
quelle est la solution et merci d'avance