简体   繁体   中英

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);
        }

通常,您需要在第一行设置PHP:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM