Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't fetch data from db

this is my Db.php
PHP
<?php


class DbFoodFunctions {

	private $conn;
	private function openDB() {

		$servername = "localhost";
		$username = "root";
		$password = "";
		$dbname = "blood";
		$this -> conn = new mysqli($servername, $username, $password, $dbname);
		if ($this -> conn -> connect_error) {
			die("Connection failed: " . $conn -> connect_error);
		}

	}

	private function closeDB() {
		$this -> conn -> close();
	}
	
	
	public function searchFood($money)
	{
		$this -> openDB();
		$stmt = $this -> conn -> prepare("Select * from FOOD where amount >= ?"); 
		$stmt -> bind_param("i", $money);
		$stmt -> execute();
		$result = $stmt -> get_result();
		$numrows = $result -> num_rows;
		$this -> closeDB();
		$res = array();
		$res['numrows'] = $numrows;
		while ($myrow = $result -> fetch_object()) {
			$res[] = $myrow;

		}
		return $res;
	}
	
}
?>



Index.php

PHP
<?php

require_once 'db/db.php';

$dbObj = new DbFoodFunctions();
$action = '';
$result = array();

if ($action == 'searchFood') {
	$money = '';
		if (isset($_REQUEST['money'])) {
			$money = trim($_REQUEST['money']);
		}
		$res = $dbObj -> searchFood($money);
		$result['success'] = 1;
		$result['data'] = $res;
		echo json_encode($result);
}else {
	$result['error'] = 1;
	$result['message'] = "You are not authorized";
	echo json_encode($result);
}
?


What I have tried:

When i try to run the program it always show You are not authorized
Posted
Updated 21-Feb-16 20:59pm
Comments
Kornfeld Eliyahu Peter 21-Feb-16 13:57pm    
On what line?
Do your root has no password?
C0DE_007 21-Feb-16 14:16pm    
yes sir no password
Kornfeld Eliyahu Peter 21-Feb-16 15:20pm    
Can you answer: on what line?
C0DE_007 21-Feb-16 14:17pm    
i don't know why it showing You are not authorized msg .. code are fine !
Afzaal Ahmad Zeeshan 21-Feb-16 16:14pm    
Perhaps the error is self-explanatory. :-)

1 solution

Your action variable is empty. And will always be empty before the if condition. Therefore, the code will always show else content (which is You're not authorized)

I suggest you implement better error catching schema because as-is, the code handles any error with same You're not authorized error regardless of real problem (for example, the network might be down or you could have made typing error in db file or whatever).
 
Share this answer
 
Comments
C0DE_007 22-Feb-16 4:33am    
actually i am making server side android app so through in android asynctask Pair pairAction = new Pair<>("action", "searchFood"); it will send action name then it will run like if ($action == 'searchFood') {
Mohibur Rashid 22-Feb-16 5:14am    
You are copy pasting same text to every comment. Have you tried to read what we are saying?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900