Connecting PHP to Cloud SQL

I'm able to connect to my Cloud SQL through Desktop MySQL software, but now I'm trying to connect to the Database via PHP.

I have a PHP subdomain which does the server requests to MySQL, the PHP is live on a web accessible website whilst I work on an AngularJS project locally via localhost:9000

define("DB_HOST", "[my ip]:3306");
define("DB_NAME", "db");
define("DB_USER", "user");
define("DB_PASS", "pass");

$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if (!$db) {
    die('Could not connect: ' . mysql_error());
} else {
    echo 'Connected';
}

So if I check my PHP page I see that page displays Connected but my MySQLi queries are failing.

I have a simple GET which returns empty

$q = $db->query("SELECT * FROM items");

        $items = array();
        while($r = mysqli_fetch_array($q)) {
            $item = array(
                'id' => $r['id'],
                'item_id' => $r['item_id'],
                'name' => $r['name'],
                'json' => $r['json']
            );
            array_push($items, $item);
        }

Answers


Usually you need to set your PHP with the first line with this:

header('Content-Type: text/html; charset=utf-8');

Need Your Help

perfect square algorithm - explanation for the implementation

algorithm integer square-root perfect-square

This question is a follow-up on the post here: Fastest way to determine if an integer's square root is an integer, What's a good algorithm to determine if an input is a perfect square?.