2018年6月27日 星期三

[GCP]若PHP使用mysqli_connect來連接資料庫


錯誤訊息如下
<br />
<b>Warning</b>:  mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in <b>xxxxxxxxxxxxxx</b> on line <b>18</b><br />
<br />
<b>Warning</b>:  mysqli_query() expects parameter 1 to be mysqli, boolean given in <b>xxxxxxxxxxxxxx</b> on line <b>19</b><br />
<br />
<b>Warning</b>:  mysqli_query() expects parameter 1 to be mysqli, boolean given in <b>xxxxxxxxxxxxxx</b> on line <b>45</b><br />
<br />
<b>Warning</b>:  mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in <b>xxxxxxxxxxxxxx</b> on line <b>46</b><br />
<br />
<b>Warning</b>:  mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in <b>xxxxxxxxxxxxxx</b> on line <b>47</b><br />
{"status":110}



需要在與app.yaml同層的地方
加上一個php.ini檔
並加入以下內容

display_errors=On //打開才知道錯在哪


連接的時候要如以下設定
<?php

$con=mysqli_connect("localhost","root","你的密碼","你的資料庫名稱",0,"/cloudsql/你的connectName");
 //connectName可以從SQL頁面中找到

if (!$con)
{
    die("连接错误: " . mysqli_connect_error());
}
?>

沒有留言:

張貼留言