CRUD Read operation PHP - php

I am creating an Android Application that requires information to be retrieved from a MySQL database on a MAMP server. I have written PHP code to try retrieve the information from the database however there is no information being retrieved from the database. I have checked the code using PHP code checker and there is no issues found. Can anyone find any issues or provide any links to help. Thank you in advance.
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once('connect.php');
// connecting to db
//$db = new db_name();
// get all products from products table
$result = mysqli_query("SELECT * FROM tbl_book");
// check for empty result
if (mysqli_num_rows($result) > 0) {
// looping through all results
// book node
$response["books"] = array();
while ($row = mysqli_fetch_array($result)) {
// temp user array
$book = array();
$book["id"] = $row["id"];
$book["title"] = $row["title"];
$book["description"] = $row["description"];
$book["bookID"] = $row["bookID"];
// push single book into final response array
array_push($response["books"], $book);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No book found";
// echo no users JSON
echo json_encode($response);
}
?>

Related

Although I use the same services in 2 different places, I cannot pull data from the database

While I can do anything I want in the database on the free https://volkanguder.000webhostapp.com/envanterler/tum_envanterler.php site, I cannot pull the data on the paid https://volkanguder.info/envanterler/tum_envanterler.php site. When I explained the problem to the relevant place, they stated that there was a problem in the codes I wrote, but the problem was still not solved. Please help.
My Php Service Code;
<?php
// array for JSON response
$response = array();
//DB_SERVER,DB_USER,DB_PASSWORD,DB_DATABASE değişkenleri alınır.
require_once __DIR__ . '/db_config.php';
// Bağlantı oluşturuluyor.
$baglanti = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
// Bağlanti kontrolü yapılır.
if (!$baglanti) {
die("Hatalı bağlantı : " . mysqli_connect_error());
}
$sqlsorgu = "SELECT * FROM envanterler";
$result = mysqli_query($baglanti, $sqlsorgu);
// result kontrolü yap
if (mysqli_num_rows($result) > 0) {
$response["envanterler"] = array();
while ($row = mysqli_fetch_assoc($result)) {
// temp user array
$envanterler = array();
$envanterler["envanterId"] = $row["envanterId"];
$envanterler["envanterKategori"] = $row["envanterKategori"];
$envanterler["envanterAdi"] = $row["envanterAdi"];
$envanterler["envanterMarka"] = $row["envanterMarka"];
$envanterler["envanterModel"] = $row["envanterModel"];
$envanterler["envanterSerino"] = $row["envanterSerino"];
$envanterler["envanterRfid"] = $row["envanterRfid"];
$envanterler["envanterMahal"] = $row["envanterMahal"];
// push single product into final response array
array_push($response["envanterler"], $envanterler);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No data found";
// echo no users JSON
echo json_encode($response);
}
//bağlantı koparılır.
mysqli_close($baglanti);
?>
MySQL JSON ;
[
{"type":"header","version":"4.9.7","comment":"Export to JSON plugin for PHPMyAdmin"},
{"type":"database","name":"volkangu_envatool"},
{"type":"table","name":"envanterler","database":"volkangu_envatool","data":
[
{"envanterId":"1","envanterKategori":"Bilgisayar ve Çevre Ekipmanları","envanterAdi":"Bilgisayar","envanterMarka":"Dell","envanterModel":"Optiplex 3050 MicroPc","envanterSerino":"12345","envanterRfid":"VG458250","envanterMahal":"MH0102030004"},
{"envanterId":"2","envanterKategori":"Pc ve Donanımlar","envanterAdi":"Monitör","envanterMarka":"Dell","envanterModel":"E2218S","envanterSerino":"12345","envanterRfid":"VG458251","envanterMahal":"MH0102030005"},
{"envanterId":"3","envanterKategori":"Bilgisayar ve Çevre Ekipmanları","envanterAdi":"Klavye","envanterMarka":"Dell","envanterModel":"KB-124-TUR","envanterSerino":"123456789","envanterRfid":"VG458252","envanterMahal":"MH0102030006"}
]
}
]
Free Demo site Service (Working) ;
enter image description here
Hostinger Main site (Not Working) ;
enter image description here
All Hostinger Customer service recommendations

Android: Get details from php script with htaccess and htpasswd

I have a php script with this output:
And then I display this data in Android. But now, I put a .htaccess and .htpasswd file in the directory and I have to enter a password, if I want to read the data. My question is how can I display the data in Android, now with the files (.htaccess and .htpasswd). Is there a way to enter the password in Android to show the data because now I cant display the data..
Thanks
EDIT:
This is my php script:
<?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["feed"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$feed = array();
$feed["uid"] = $row["uid"];
$feed["unique_id"] = $row["unique_id"];
$feed["name"] = $row["name"];
$feed["full_name"] = $row["full_name"];
$feed["email"] = $row["email"];
$feed["age"] = $row["age"];
$feed["about_the_customer"] = $row["about_the_customer"];
$feed["encrypted_password"] = $row["encrypted_password"];
$feed["salt"] = $row["salt"];
$feed["created_at"] = strtotime($row["created_at"]);
$feed["updated_at"] = $row["updated_at"];
$feed["imageName"] = $row["imageName"];
// push single product into final response array
array_push($response["feed"], $feed);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No feed found";
// echo no users JSON
echo json_encode($response);
}
?>

Php web service to get data in json format

I have 2 database tables
main_menu which has 3 columns id, item_name, img
sub_menu which has columns id, sub_item_name, item_name, add_price, img
Now I am trying to get all sub menu under master menu when user clicks on master menu
eg.
select * from sub_menu where item_name="Cafe"
it should display all sub menu under Cafe.
And here is the web service I wrote to do that and get data in json format
<?php
$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["item_name"])) {
// get a product from products table
$result = mysql_query("SELECT * FROM sub_menu WHERE item_name ='".$_GET["item_name"]."'");
echo $result;
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$sub_menu = array();
$sub_menu["id"] = $result["id"];
$sub_menu["item_name"] = $result["item_name"];
$sub_menu["sub_item_name"] = $result["sub_item_name"];
$sub_menu["add_price"] = $result["add_price"];
$sub_menu["img"] = $result["img"];
// success
$response["success"] = 1;
// user node
$response["sub_menu"] = array();
array_push($response["sub_menu"], $sub_menu);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No sub menu found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No menu 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);
}
?>
Now the problem is I am only getting first data only, it should display all data
Query is working fine in mysql, I checked that. The problem is somewhere in my php service.
You have to call mysql_fetch_array() in a loop, and add all the rows to an array. You can just copy the whole row of results in a single assignment, since you were using the same field names in your JSON as in the database.
if (mysql_num_rows($result) > 0) {
$submenu = array();
while ($result = mysql_fetch_array($result);
$submenu[] = $result;
}
$response["success"] = 1;
// user node
$response["sub_menu"] = $all_results;
// echoing JSON response
echo json_encode($response);

Android hindi font not displaying

In my android app.. a result is taken from server and displayed in app.. where two languages are there for user to select.. English and Hindi.. The english part is working fine and displaying correctly. .but for hindi text ,... ony '???????' is displaying insted of fonts... But when I saved hindi fonts in server ..it is displaying as hindi fonts olnly ... I am using php code for connecting with the server..do we need to change it to utf8.. do we need to change any thing in php file.. I am giving my php code and sql code below.. please check and if there any error pls help..
php
<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// 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["pinnum"]))
{
$pinnum = $_GET['pinnum'];
// get a product from products table
$result = mysql_query("SELECT *FROM pin1h WHERE pinnum = $pinnum");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["pid"] = $result["pid"];
$product["pinnum"] = $result["pinnum"];
$product["pinnacle"] = $result["pinnacle"];
$product["created_at"] = $result["created_at"];
$product["updated_at"] = $result["updated_at"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// 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);
}
?>
sql code
CREATE TABLE pin1h(
pid int(11) primary key auto_increment,
pinnum int(11),
pinnacle text,
created_at timestamp default now(),
updated_at timestamp
);
Just a tought but i think you should check the encoding on the server to see if what you send is alright, because i had the same problem one time and it turned out the server was sending the bad string.
<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
mysql_query("SET NAMES 'utf8'");
// check for post data
if (isset($_GET["pinnum"]))
{
$pinnum = $_GET['pinnum'];
// get a product from products table
$result = mysql_query("SELECT *FROM pin1h WHERE pinnum = $pinnum");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["pid"] =$result["pid"];
$product["pinnum"] = $result["pinnum"];
$product["pinnacle"] = $result["pinnacle"];
$product["created_at"] = $result["created_at"];
$product["updated_at"] = $result["updated_at"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// 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);
}
?>

Unable to get data in proper format

I am unable to parse data in PHP from MySQL.
Here's my code
<?php
header('Content-Type: text/html; charset=utf-8');
// array for JSON response
$response = array();
echo 'भगवान';
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("SELECT *FROM create_event") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["create_event"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["id"] = $row["id"];
$product["desc"] = $row["desc"];
$text;
array_push($response["create_event"], $product);
}
// success
$response["success"] = 1;
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
It's my database, I have set all the collation in utf-8 ci format but still not working:
I have tried all possible solutions and help online and have gone through popular posts and answers and also have set browsers settings to support Hindi lang but still displaying ? marks in output. Here is the output format
भगवान{"id":"1","desc":"???? ??????"}
mysql_query('SET character_set_results=utf8');
Use this after connecting to database or before using any select statement.

Categories