mysql如何获得http用get方式传过来的参数

WBOY
Release: 2016-06-13 10:57:02
Original
896 people have browsed it

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!<br>";  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";  }<br><font color="#e78608">------解决方案--------------------</font><br>一步一步来。<br>首先,SQL语句里面的单引号不需要转义;<br>然后,先用echo $_get($username);语句输出看看能否正确获取值;<br>最后,就这样,哈哈<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!