Mysql查询问题与搜索

时间:2015-02-08 12:43:40

标签: php mysql search

使用我之前的个人资料视图,它会抓取搜索到的ID并使用此查询显示它:

        $dn = mysql_query('select firstname, lastname, age, id, background from users where id="'.$id.'"');

然而,我目前通过搜索到的网址查看活动页面。所以网址是socialnetwk所以

        $dn = mysql_query('select eventname, about, url, comment, post, msg, member_id, author_id, id from events where url="'.$id.'"');

搜索那个的将是: http://www.socialnetwk.com/aHjrhuykLKJbBhjlHJKlkefuhoiughasoiHBOIuyhbgfDilhub/event.php?id=socialnetwk

我不确定如何解决此问题,因为我已经使用续集专业版,而且似乎我需要' '围绕网址名称。我怎么还没知道如何在查询中包含这个

网址是一列,而非实际网址

以下是代码:

<?php
        //We check if the users ID is defined
        if(isset($_GET['id']))
        { 
                $id = intval($_GET['id']);
                //We check if the user exists
                $dn = mysql_query('select eventname, about, url, comment, post, msg, member_id, author_id, id from events where url="'.$id.'"');
                if(mysql_num_rows($dn)>0)
                {
                        $dnn = mysql_fetch_array($dn);
                        //We display the user datas
        if($dnn['id']!='')
        {
        }
        else
        {
                echo 'This user dont have an avatar.';
        }
        ?>

1 个答案:

答案 0 :(得分:0)

已更新以符合您编辑的代码:

在使用预备陈述的情况下:

<?php
        //We check if the users ID is defined
        if(isset($_GET['id'])){
                $id = intval($_GET['id']);
                //We check if the user exists
                $dn = 'select eventname, about, comment, post, msg, 
                       member_id, author_id, id from events where url=?';
                if($stmt=$dbc->prepare($dn)){
                $stmt->bind_param('s',$id); //your URL is a string
                $stmt->execute();  //returns false if fails
                $stmt->bind_result($eventname, $about, $comment, $post, 
                       $msg, $member_id, $author_id, $id); //don't need to 
                                 //bind the url, since you already know it
                $stmt->fetch();
                $stmt->free_result();

                if($stmt->num_rows>0) {
                        //We display the user datas
                    echo "$eventname, $about, $comment ..."; // the bound results
                }
          $stmt->close();
          $dbc->close();
         }
        if($dnn['id']!=''){
              // do something here
        } else {
                echo 'This user dont have an avatar.';
        }

?>

这假定$dbc是您的数据库连接。

*注意,您将$ _GET [&#39; id&#39;]值更改为整数,但它也是一个URL(字符串)。这需要进行协调,以使您的代码能够正常运行*