douwu7563 2014-10-06 17:35
浏览 40
已采纳

基本的PHP SQL - 从URL查询多个东西

So, I wanted to use two params on the url to query from my server but I'm not being able to do it.

The url is : localhost/database/getTable?sLang=English&sId=1

The php Script is:

<?php



if(isset($_REQUEST['sLang']))
if(isset($_REQUEST['sId'])) 
    {

    $con = mysql_connect("blabla","blablabla","blablabla");
    mysql_query("SET NAMES UTF8");
        if (!$con)
            {
            die('Could not connect: ' . mysql_error());
            }
        mysql_select_db("database", $con);

        $sLang = $_REQUEST['sLang']; //valor customer ID = valor requisitado
        SdID = $_REQUEST['sID']
        $result = mysql_query("SELECT * FROM table WHERE language = '$sLang' AND id = '$sId" ) or die('Errant Query:'); //escolher da tabela comments onde valor da coluna id = Valor reuisitado

        while($row = mysql_fetch_assoc($result))
            {
        $output[]=$row;
            }

        header('content-type: application/json; charset=utf-8');

        print(json_encode($output, JSON_UNESCAPED_UNICODE));

        mysql_close($con);

        }
        else
        {
        $output = "not found";
        print(json_encode($output));
        }


?>

I'm just returning the sLang Values and the url changes to : localhost/database/getTable?sLang=English

How can I query the database using the two params sLang and sId?

Best regards

  • 写回答

2条回答 默认 最新

  • dsrw29618 2014-10-06 17:39
    关注

    You have a missing $ and missing semi-colon for SdID = $_REQUEST['sID']

    Plus, SdID has a typo.

    which should most likely read as

    $sId = $_REQUEST['sID'];
    

    which you're using in conjunction with AND id = '$sId"

    Also AND id = '$sId" needs a quote AND id = '$sId'" or remove the erroneous quote.

    SdID is presently being evaluated as a constant.

    Another thing I noticed is the word table in ("SELECT * FROM table WHERE

    An insight: If that is the DB table's actual name, it needs to be wrapped in backticks:

    ("SELECT * FROM `table` WHERE ...
    

    Also, instead of:

    if(isset($_REQUEST['sLang']))
    if(isset($_REQUEST['sId'])) 
    

    it's best to use

    if(isset($_REQUEST['sLang']) && isset($_REQUEST['sId']))
    
     {
    
     // code
    
     }
    

    or

    if(isset($_GET['sLang']) && isset($_GET['sId']))
    
     {
    
     // code
    
     }
    

    Using error checking would have and should have thrown you errors, as well as mysql_error() in your query, if that is your actual code that you have posted initially, should there be an edit made on your part afterwards.


    • Sidenote: Do take the comments posted under your question seriously.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿