返回我使用Codeigntier運行的查詢是這樣的:不正確的行ID在簡單的選擇查詢
$query = $this->db->query("SELECT users.*, user_profiles.* FROM users, user_profiles WHERE " . "users.id = $user_id AND user_profiles.user_id = $user_id");
$row = $query->row();
我已經把回聲得到最後的查詢$this->db->last_query()
。這表明:
SELECT users.*, user_profiles.* FROM users, user_profiles WHERE users.id = 6850 AND user_profiles.user_id = 6850
它返回的結果是:
object(stdClass)[24]
public 'id' => string '6849' (length=4)
結果顯示id是6849
。我幾乎摔倒在我的椅子上!我在我的MySQL數據庫上運行了相同的查詢,並且爲完全相同的查詢返回的id是6850
。 6850是正確的ID。
我不知道如何調試,因爲這是最簡單的查詢。任何幫助讚賞。
不要爲兩個表使用'$ user_id',請嘗試連接它們。也許這就是搞砸了嗎? ' 「WHERE users.id = $ USER_ID和user_profiles.user_id = users.id」' –
火箭打我衝頭(+1) - '$ SQL =' \t選擇 \t \t用戶*, \t \t user_profiles。* \t FROM \t \t用戶, \t \t user_profiles \t WHERE \t \t users.id = '$ user_ID的。' AND \t \t user_profiles.user_id = users.id ';'這可能不是100%的問題,但這是連接表的更加標準的方式,它不會傷害。 –
另外,'SELECT * ...'通常是不好的做法:)相反,你應該明確地命名你的列。如果表結構意外改變(如不同的開發人員),它會使調試變得更簡單。 –