-
How to write PHP code to select specific column and display rows respectfully??
almost 9 years ago
-
almost 9 years ago
Hi Bharat,
Use below code to get data from your database using mysqli and php:
<?php // Create connection $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE); // Check connection if ($mysqli->connect_error) { die("Connection failed: " . $mysqli->connect_error); } $stmt = $mysqli->query("SELECT * FROM tree where `condition` = 'good'"); if ($stmt->num_rows > 0) { // output data of each row while($tree = $stmt->fetch_assoc()) { $response["error"] = FALSE; $response ["tree"] ["treeid"] = $tree['id']; $response ["tree"] ["treespecies"] = $tree['condition']; $response ["tree"] ["treelatitude"] = $tree['lat']; $response ["tree"] ["treelongitude"] = $tree['long']; echo json_encode($response),'<br>'; } } else { $response["error"] = TRUE; $response["error_msg"] = "Tree list view credentials are wrong. Please try again!"; echo json_encode($response); } $mysqli->close(); ?>
Make sure you use correct credentials for DB_HOST, DB_user, DB_PASSWORD and DB_DATABASE.
I hope this will help.
-
-
almost 9 years ago
Just change your query to:
$stmt = $mysqli->query("SELECT * FROM tree where `condition` IN ( 'good', 'poor', 'Excellent', 'Fair')");
-
-
almost 9 years ago
Thanks Subhash...
What if my condition consists of 4 rows i.e Condition = 'good', 'poor', 'Excellect', 'Fair'...How can I change in code?
-
3 Answer(s)