Cant connect to mysql

Hey guys!!

im trying to build an android app and i tried using this php to connect to mysql and return json. but the server doesnt return anything. is there something wrong!?

[php] <?php
if($_SERVER[“REQUEST_METHOD”]==“POST”){
include ‘connection.php’;
getInsurances();
}
function getInsurances()
{
global $connect;

$query = " Select * FROM insurance; ";

$result = mysqli_query($connect, $query);
$number_of_rows = mysqli_num_rows($result);

$temp_array  = array();

if($number_of_rows > 0) {
	while ($row = mysqli_fetch_assoc($result)) {
		$temp_array[] = $row;
	}
}

header('Content-Type: application/json');
echo json_encode($temp_array);
mysqli_close($connect);

} [/php]

my connection.php is this but with the right data
[php]<?php
define(‘hostname’, ‘xxxxxxxx’);
define(‘user’, ‘xxxxxxxx’);
define(‘password’, ‘xxxxxxxx’);
define(‘databaseName’, ‘xxxxxxx’);
$connect = mysqli_connect(hostname, user, password, databaseName);
?> [/php]

it should return a json with a list of insurance companies.

i need help thanks in advance!!

Well, you have a list of several mysqli_??? functions, but, not one error checking or error capturing code attached to each.
Therefore, it is impossible to know where it is breaking down. Each function should have an error check attached to it.
This means that these three lines:

$result = mysqli_query($connect, $query);
$connect = mysqli_connect(hostname, user, password, databaseName);
if($number_of_rows > 0) {

All need extra code included to catch errors. The last is easy. It checks for number of rows and displays something. But, if the number of rows is zero, it needs to display an message like, “no data found”. Or something that would go back to the other end of your system. The first two need either a “try-catch” section or at least a “or-die” section so that if they fail some error message is sent out to the other end of the system. To build a phone app, you would need to have the message sent back to be very very generic so the user does not get upset. Something like, "try again later’ or “contact us about error#321” or something else simple.
Here is a page that shows how one person did it with OR-DIE and how to do the same with TRY-CATCH. You need to add these so that you can send more info so the user gets the errors. Make sure you word each message differently enough so you can tell where it is failing. Hope this helps!
http://stackoverflow.com/questions/9836693/how-to-use-throw-exception-in-mysql-database-connect

The more people that have issues with mysqli, the more I like PDO.

You are attempting to return json, whether you have anything or not in the dataset. You likely want to return false or an error string if there is nothing to return.

WEELLP… the code was correct from the beginning. but the server didnt allow ‘localhost’ as the hostname. and i didn’t realize this because it worked for me before.

thank you for your answers!

You need to enable error reporting when debugging.

Add this to the top of your page.
[php]error_reporting(-1);
ini_set(‘display_errors’, ‘1’);[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service