php操作mysql文法格式出錯

來源:互聯網
上載者:User
1、錯誤查詢語句:$sql='select * from sh_admin where username=$username and password=$password limit 1';2、正確查詢語句:$sql = "select * from sh_admin where username = '{$username}' and password = '{$password}' limit 1";

當PHP操作mysql資料庫時為什麼非得用 '{$username}'的方式才能查詢,PHP文法中雙引號就能解析變數啊 ,為什麼我用第一種會出錯呢?

回複內容:

1、錯誤查詢語句:$sql='select * from sh_admin where username=$username and password=$password limit 1';2、正確查詢語句:$sql = "select * from sh_admin where username = '{$username}' and password = '{$password}' limit 1";

當PHP操作mysql資料庫時為什麼非得用 '{$username}'的方式才能查詢,PHP文法中雙引號就能解析變數啊 ,為什麼我用第一種會出錯呢?

1、變數$username和$password在單引號裡面是不解析的
2、字元型欄位username和password後面的值是需要加引號的。

$sql='select * from sh_admin where username="'.$username.'" and password="'.$password.'" limit 1';

你第一條sql用的是單引號啊 所以出錯
是用

$sql="select * from sh_admin where username=$username and password=$password limit 1";

就沒問題了

同意樓上,字串是要加引號的。

調試一下 將$sql輸出一下,一眼就能看出了!
第一句 單引號裡面的變數不解析,且sql裡面 字串是要引起來的

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.