各位大大晚安
事情是這樣的~
我在做一個php的登入介面
可以怎麼找都找不出錯誤在哪QQ
求指點

下面是我的整個製作過程....
怎麼看都看不出哪裡錯
從http://localhost:8012/phpmyadmin/
新增資料庫
建立資料表



做連線用的conDB.php
<?php
	class conDB
	{
		static $conn=NULL;
		
		function getConnection()
		{
			$serverName ="localhost:8012";
			$databaseName="account";
			$dbuser="root";
			$dbpassword="";
			
			if(!isset($conn))
			{
				try
				{
					$conn=new PDO("mysql:host=serverName; dbname=$databaseName",$dbuser,$dbpassword);
					$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
					$conn->exec("set names utf8");
					
					
				}
				
				catch(Exception $e)
				{
					echo $e->getMessage();
					error_log($e->getMessage(),0);
				}
			}	
			return $conn;
		}
		
	}
?>

註冊畫面:開一個html檔
從http://localhost:8012/register.html
<html>
<head>
<meta charset="UTF-8">
<style>
body
{
font-family:微軟正黑體;
}
</style>
</head>
<body>
<table style="text-align:center;padding:5;border:1px solid black">
<tr>
<td style="min-width:100px">帳號</td>
<td><input type="text" name="account"></td>
</tr>
<tr>
<td style="min-width:100px">密碼</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td style="min-width:100px">姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td style="min-width:100px">性別</td>
<td><input type="text" name="sex"></td>
</tr>
<tr>
<td style="min-width:100px">系碼</td>
<td><input type="text" name="coursecode"></td>
</tr>
<tr>
<td><input type="reset" value="重設"></td>
<td><input type="submit" value="送出"></td>
</tr>
</table>
</body>
</html>


加入連結跳轉的from
<html>
<head>
<meta charset="UTF-8">
<style>
body
{
font-family:微軟正黑體;
}
</style>
</head>
<body>
<form method="post" action="register.php">
<table style="text-align:center;padding:5;border:1px solid black">
<tr>
<td style="min-width:100px">帳號</td>
<td><input type="text" name="account"></td>
</tr>
<tr>
<td style="min-width:100px">密碼</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td style="min-width:100px">姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td style="min-width:100px">性別</td>
<td><input type="text" name="sex"></td>
</tr>
<tr>
<td style="min-width:100px">系碼</td>
<td><input type="text" name="coursecode"></td>
</tr>
<tr>
<td><input type="reset" value="重設"></td>
<td><input type="submit" value="送出"></td>
</tr>
</form>
</table>
</body>
</html>
寫跳轉過去的register.php
判斷帳號是否存在:存在0不存在1


現在是按送出就不行
第一張圖裡就有寫了
出錯的是 conDB.php 第 30 列$conn 未定義
由於 conDB.php 沒有成功回傳 $conn
以致 register.php 第 9 列無法呼叫 prepare function在 ($conn=)null 值上呼叫 prepare function
另外
你的 Web(HTTP) 已經是 localhost:8012
你的 DB(MySQL) 不該是 localhost:8012,應該是另一個 port
大大早安
在 ($conn=)null 值上呼叫 prepare function
這句沒有很懂?
所以code要改?
你的 DB(MySQL) 不該是 localhost:8012
所以是你的 DB(MySQL) 是 localhos還是我再try
謝謝大大?
port 8012 已經給HTTP用了
所以DB的port 不會是8012而是其他的
伺服器預設 DB給的port 應該是 3306
上班不能
Try好痛苦
一直在思考出錯的是 conDB.php 第 30 列
$conn 未定義
由於 conDB.php 沒有成功回傳 $conn
以致 register.php 第 9 列無法呼叫 prepare function
在 ($conn=)null 值上呼叫 prepare function
的意思
剛剛還想到我可以裡面先建一筆資料讓它不要空
海綿大的意思 應該是
$conn=null上 去呼叫prepare
另外 $conn未定義的問題 應該是你連接資料庫錯誤
$serverName ="localhost";
如果你沒有去改 DB PORT 改成這樣跑看看
回家先把你 conDB.php 裡的 getConnection 裡的$serverName ="localhost:8012";
改成$serverName ="localhost:3307";
試試看
剛剛還想到我可以裡面先建一筆資料讓它不要空
現在是連資料庫都還連不上
你去裡面建一筆資料馬洗沒有用...
$serverName ="localhost:3307";
還是不行





所有的建議都做了還是不行QQ
port 用 3307
接著改 conDB.php
<?php
	class conDB
	{
		public $conn;
		
		function getConnection()
		{
			$serverName ="localhost:8012";
			$databaseName="account";
			$dbuser="root";
			$dbpassword="";
			
			if(!isset(this->$conn))
			{
				try
				{
					this->$conn=new PDO("mysql:host=serverName; dbname=$databaseName",$dbuser,$dbpassword);
					this->$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
					this->$conn->exec("set names utf8");
					
					
				}
				
				catch(Exception $e)
				{
					echo $e->getMessage();
					error_log($e->getMessage(),0);
				}
			}	
            return this->$conn;
		}
		
	}
?>
DEAR 大大們~終於過了12點
我可以又有發言權限了
剛剛去找了其他學php的學習發法..(所以之後會有新的問題?)
發現我在$pdo=new PDO('mysql:host=localhost:3307;dbname=shop;charset=utf8',
'帳號', 'password');
是可以的~
回到這裡又不行~


現在變成是這個錯誤
確認一下樓上大大說的
我所認知的...
變數->方法
PDO->query('SQL指令')

this->$conn全部改成$conn試看看
$this->conn 應該是這樣吧@@




還是無解~
我把}去掉改;也不行
增加;也不行
第30列最後面要加一個分號;
return this->$conn;


加了之後又變回錯這個
看來這題要永遠無解了
其實我這個是跟著在udemy的老師做的
只是老師不願意提供程式碼
奇怪~跟著老師做居然做不出來?
$this->conn
你上面都寫$this->conn
30行怎又變成 this->$conn


還是不行 
你這份 code 的 serverName 沒有加上$符號,細心一點吧
$this->conn = new PDO("mysql:host=$serverName; dbname=$databaseName", $dbuser, $dbpassword);
加了之後還是跳出這個QQ
Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\conDB.php:13 Stack trace: #0 C:\xampp\htdocs\register.php(8): conDB::getConnection() #1 {main} thrown in C:\xampp\htdocs\conDB.php on line 13
雖然最後還是沒有解決..但是謝謝大大幫我找報port...所以先結案吧
mysql的port預設值因該是3306。
理論上你的db host只要設定localhost就好。
如果你並未去修改my.ini的話。
你在 Class 內宣告了 static $conn,但你的呼叫方式卻是定義 function 內的 $conn,呼叫 Class 內的成員為 $this->conn
<?php
	class conDB
	{
		$this->conn;
		
		function getConnection()
		{
			$serverName ="localhost:3307";
			$databaseName="account";
			$dbuser="root";
			$dbpassword="";
			
			if(!isset($conn))
			{
				try
				{
					$conn=new PDO("mysql:host=serverName; dbname=$databaseName",$dbuser,$dbpassword);
					$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
					$conn->exec("set names utf8");
					
					
				}
				
				catch(Exception $e)
				{
					echo $e->getMessage();
					error_log($e->getMessage(),0);
				}
			}	
			return $conn;
		}
		
	}
?>
改成這樣變成錯在


權限這裡看也是沒有問題
關於物件導向的概念你得找時間再去查查喔
<?php
class conDB
{
    protected $conn = null;
    function getConnection()
    {
        $serverName ="localhost:3307";
        $databaseName = "account";
        $dbuser = "root";
        $dbpassword = "";
        if ($this->conn !== null) {
            try {
                $this->conn = new PDO("mysql:host=$serverName; dbname=$databaseName", $dbuser, $dbpassword);
                $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $this->conn->exec("set names utf8");
            } catch (Exception $e) {
                echo $e->getMessage();
                error_log($e->getMessage(), 0);
            }
        }
        return $this->conn;
    }
}