mysql如何獲得http用get方式傳過來的參數

來源:互聯網
上載者:User
mysql怎麼獲得http用get方式傳過來的參數?
各位英雄,本人剛接觸php,現有一問題請教:
我在網路中傳參數過來(get方式):http:[email protected]&password=123456

現在我想完成這樣的功能:在我的資料庫表中查詢這個username存在與否,如果不存在就添加進資料庫表格。
以下是我的php實現代碼(資料庫已經串連成功):


$result = mysql_query("SELECT * FROM app_t WHERE username=\’$username\’"); //username就是通過url傳來的參數

if($row = mysql_fetch_array($result))
{
echo "This username has existed!
";
echo $row[0];
}

else
{
$sql="INSERT INTO app_t(username ,password ) VALUES(\’$username\’, \’$password\’)";
//如果表中沒有這條username記錄,將傳過來的參數寫進資料庫表
if (!mysql_query($sql,$connection))
{
die('Error: ' . mysql_error());
}
else
echo "1 record added";
}


以下是運行後的報錯:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Apache2.2\htdocs\connect.php on line 29
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\’\’, \’\’)' at line 1

請問下我該如何在mysql中使用web傳過來的參數進行查詢,或者把這些參數插入資料表中呢,謝謝!
另,我用這種方式也不行:$result = mysql_query("SELECT * FROM app_t WHERE username=$_GET["username"]");
再次謝謝大家,希望知者能不辭辛苦,給我回複,做個好心人。本人分不多,sorry!

------解決方案--------------------
PHP code
$result = mysql_query("SELECT * FROM app_t WHERE username='$username'"); //username就是通過url傳來的參數if($row = mysql_fetch_array($result))  {  echo "This username has existed!
"; echo $row[0]; } else { $sql="INSERT INTO app_t(username ,password ) VALUES('$username', '$password')"; //如果表中沒有這條username記錄,將傳過來的參數寫進資料庫表 if (!mysql_query($sql,$connection)) { die('Error: ' . mysql_error()); } else echo "1 record added"; }
------解決方案--------------------
一步一步來。
首先,SQL語句裡面的單引號不需要轉義;
然後,先用echo $_get($username);語句輸出看看能否正確擷取值;
最後,就這樣,哈哈
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.