SQL Query无法检索正确的结果

时间:2013-07-19 14:23:40

标签: php mysql

这是我用来确定是否让用户进入的login.php脚本。

<?php

if(isset($_POST['submitted']))
{
 $errors= array();
 $username = ($_POST['username']);
 $pass = ($_POST['pass']);
 $shapass = sha1($pass);
 $_POST['username'] = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
 if ($_POST['username'] == "") {  
   $errors[] = "Please enter your username.";
 }
 if ($_POST['pass'] == "") {  
   $errors[] = "Please enter your password.";
 }

 if(is_array($errors))
 {
    echo '<font color="red"><div align="center" class="error"><span></span><ul>';
    while (list($key,$value) = each($errors))
    {

        echo '<li>'.$value.'</li><br />';
    }echo'</ul></div></font>';
}


if(empty($errors))
{
$user_name = "root";
$password = "";
$database = "rsswebapp";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) 
{

$match = "Select ROWID,EMAIL,PWD from `user` ".
    " where (USERNAME='$username' OR EMAIL='$username') and PWD='$shapass' ";
$qry = mysql_query($match);



/*
$uid = isset($_POST['username']) ? $_POST['username'] : $_SESSION['username'];
$pwd = isset($_POST['pass']) ? $_POST['pass'] : $_SESSION['pass'];
*/
$num_rows = mysql_num_rows($qry); 
if ($num_rows <= 0) { 
//unset($_SESSION['uid']);
//unset($_SESSION['pwd']);
header('location:index.php?msg=' . urlencode(base64_encode("Sorry, there is no username                 $username with the specified password.Try again!")));
} 
else 
{
session_start();
$_SESSION['ROWID'] = $sid ;
header("location:dashboard.php");
}
mysql_close($db_handle);
}
else {

print "Database NOT Found ";
mysql_close($db_handle);
}

}   
}   
?>  

如果登录成功,我会将用户重定向到dashboard.php

<body>
<div class="container">
  <div id="sidebar">
      <ul>
          <li><a href="dashboard.php?p=categories"><center>Categories</center></a></li>
          <li><a href="dashboard.php?p=myfeeds"><center>My Feeds</center>    </a></li>
          <li><a href="dashboard.php?p=managefeeds"><center>Manage Feeds</center></a>    </li>
          <li><a href="dashboard.php?p=myfeed"><center>Account</center></a></li>
          <li><a href="logout.php"><center>Log Out</center></a></li>
      </ul>
  </div>
  <div class="main-content">
      <div class="swipe-area"></div>
      <a href="#" data-toggle=".container" id="sidebar-toggle">
          <span class="bar"></span>
          <span class="bar"></span>
          <span class="bar"></span>
      </a>
      <div class="content">
          <?php
          session_start();
          $sid = $_SESSION['ROWID'];
          if(!$_SESSION){
          header('location:index.php?msg=' .      urlencode(base64_encode("Please login to continue.")));
          }
          else
          {
          $pages_dir = 'pages';
          if(!empty($_GET['p'])){
          $pages = scandir($pages_dir,0); 
          unset($pages[0],$pages[1]);
          $p = $_GET['p'];
          if(in_array($p.'.php',$pages))
          {
            include($pages_dir.'/'.$p.'.php');
          }
          else
          {
            echo "Sorry, page not found.";
          }
          }

          }

           ?>
           <p></p>
      </div>
  </div>
 </div>
</body>

问题出现在myfeed.php中:

<?php 

 $user_name = "root";
 $password = "";
 $database = "rsswebapp";
 $server = "127.0.0.1";
//$db_handle = mysqli_connect($server, $user_name, $password,$database);
$db_handle = new mysqli($server, $user_name, $password,$database);
$db_found = mysqli_select_db( $db_handle,$database);
//session_start();
// $s = mysql_real_escape_string($_SESSION['ROWID']);
//$query = "SELECT  URL,TITLE FROM rssfeeds,user WHERE rssfeeds.USERID=user.ROWID";
// $query = "select GROUP_CONCAT(r.URL) as URL from user as u Left Join rssFeeds as r  On u.ROWID = r.USERID WHERE USERID = $_SESSION";
// $query = "SELECT URL FROM rssfeeds as rss WHERE rss.USERID=$s";
// $query = "SELECT `rssfeeds`.`URL` FROM `rssfeeds` WHERE `rssfeeds`.`USERID`=$_SESSION['ROWID']";
//  $query = "SELECT  URL FROM rssfeeds as rss ,user as userrss WHERE rss.USERID='$sid'";
//$query = "SELECT  rssfeeds.URL FROM rssfeeds LEFT JOIN user ON rssfeeds.USERID=user.ROWID";
 $query = "SELECT URL FROM rssfeeds as rss WHERE rss.USERID='" . $_SESSION["ROWID"] . "'";
//$query = "SELECT URL FROM rssfeeds as rss,user WHERE rss.USERID="$_SESSION['ROWID']"";
// $result = mysqli_query($db_handle,$query);
$result = mysqli_query($db_handle,$query);
// $result = $db_handle->query($query);
 $r = array();
$index = 0;
if($result)
{
echo "BG </br>";
// while($row = mysqli_fetch_assoc($result)) {
  while($row = $result->fetch_array()){
 //  while( ($row = mysql_fetch_assoc($result))!== false){
 //echo "WELL DONE";
 echo "{$row["TITLE"]}  {$row["URL"]} </br>" ;
// $r[$index] = $row["URL"];
// $index++; 
// printf ("%s \n", $row["URL"]);

}
echo "HX";

/*
$yourArray = array(); 
$index=0;
while($row = $result->fetch_array()){
    echo "b";
  //  echo "<a href='".$row["URL"]."'>".$row["TITLE"]."</a>";
   $yourArray[$index] = $row;
 $index++; 
    echo "<br />";print_r($yourArray);
 }
*/  
 }
else
{
 echo "HELLO";
 die(mysql_error());
 }



  ?>

注释掉的语句是我尝试过但不起作用的语句。我无法从数据库表中检索URL并将其显示在页面上。截至目前,我得到输出:

BG HX

我的数据库中有两个表:

  • 用户
  • rssfeeds

'user'包含四列,即:

  • ROWID(自动增量,主要)
  • USERNAME
  • EMAIL
  • PWD

而'rssfeeds'有四个:

  • RSSFEEDID
  • USERID
  • 标题
  • URL

我很确定问题在于$ _SESSION部分myfeed.php中的查询。

3 个答案:

答案 0 :(得分:1)

定义您希望为

显示网址​​的user_id
$user_id = 1;

运行简单的非连接查询

SELECT
    `rssfeeds`.`URL`
FROM
    `rssfeeds`
WHERE
    `rssfeeds`.`USERID`='$user_id'

如果由于某种原因您还需要用户表中的数据,那么您可以运行此查询

SELECT 
    `rssfeeds`.`URL`
FROM
    `rssfeeds`
    LEFT JOIN `user` ON (`rssfeeds`.`USERID`=`user`.`ROWID`)
WHERE
    `user`.`ROWID`='$user_id'

OP:

  

我将数据存储在rssfeeds中(image)。当我在phpmyadmin上运行此查询:SELECT rssfeeds.URL FROM rssfeeds WHERE rssfeeds.USERID=2;时,我得到类似这样的内容(image)。我不明白的是如何使用PHP以单个链接的形式获得此结果。我这样做(image)谢谢。

$query = "SELECT URL, TITLE FROM rssfeeds as rss WHERE rss.USERID='".$_SESSION['ROWID']."'";
$result = mysql_query($query);
if($result){
    while($row = mysql_fetch_assoc($result)){
        echo "<a href='".$row["URL"]."'>".$row["TITLE"]."</a>";
        echo "<br />";
    }
}

答案 1 :(得分:0)

USER TABLE:

CREATE TABLE `User` (
  `ROWID` int(11) NOT NULL,
  `USERNAME` varchar(45) DEFAULT NULL,
  `EMAIL` varchar(45) DEFAULT NULL,
  `PWD` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`ROWID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

RssFeeds TABLE:

CREATE TABLE `RssFeeds` (
  `RssfeedId` int(11) NOT NULL,
  `USERID` int(11) NOT NULL,
  `TITLE` varchar(45) DEFAULT NULL,
  `URL` text,
  PRIMARY KEY (`RssfeedId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

用户表中的记录

mysql> select * from User;
+-------+----------+-------+------+
| ROWID | USERNAME | EMAIL | PWD  |
+-------+----------+-------+------+
|     1 | a        | a     | a    |
|     2 | b        | b     | b    |
|     3 | c        | c     | c    |
+-------+----------+-------+------+
3 rows in set (0.00 sec)

在RssFeeds表中记录

mysql> select * from RssFeeds;
+-----------+--------+-------+------+
| RssfeedId | USERID | TITLE | URL  |
+-----------+--------+-------+------+
|         1 |      1 | t1    | u1   |
|         2 |      1 | t2    | u2   |
|         3 |      2 | t3    | u3   |
|         4 |      2 | t4    | u4   |
+-----------+--------+-------+------+
4 rows in set (0.00 sec)

用户的RssFeeds

mysql> select u.*,r.URL from User as u Left Join RssFeeds as r On u.ROWID = r.USERID;
+-------+----------+-------+------+------+
| ROWID | USERNAME | EMAIL | PWD  | URL  |
+-------+----------+-------+------+------+
|     1 | a        | a     | a    | u1   |
|     1 | a        | a     | a    | u2   |
|     2 | b        | b     | b    | u3   |
|     2 | b        | b     | b    | u4   |
|     3 | c        | c     | c    | NULL |
+-------+----------+-------+------+------+
5 rows in set (0.00 sec)

使用GROUP_CONCAT方法

mysql> select u.*,GROUP_CONCAT(r.URL) as URL,GROUP_CONCAT(TITLE) as TITLE from User as u Left Join RssFeeds as r On u.ROWID = r.USERID GROUP BY u.ROWID;
+-------+----------+-------+------+-------+-------+
| ROWID | USERNAME | EMAIL | PWD  | URL   | TITLE |
+-------+----------+-------+------+-------+-------+
|     1 | a        | a     | a    | u1,u2 | t1,t2 |
|     2 | b        | b     | b    | u3,u4 | t3,t4 |
|     3 | c        | c     | c    | NULL  | NULL  |
+-------+----------+-------+------+-------+-------+
3 rows in set (0.01 sec)

特定用户:

mysql> select u.*,GROUP_CONCAT(r.URL) as URL,GROUP_CONCAT(TITLE) as TITLE from User as u Left Join RssFeeds as r On u.ROWID = r.USERID WHERE ROWID = 1;
+-------+----------+-------+------+-------+-------+
| ROWID | USERNAME | EMAIL | PWD  | URL   | TITLE |
+-------+----------+-------+------+-------+-------+
|     1 | a        | a     | a    | u1,u2 | t1,t2 |
+-------+----------+-------+------+-------+-------+
1 row in set (0.00 sec)

答案 2 :(得分:0)

新答案基于OP的新问题:

以下是调试代码时的一些指示以及我如何发现问题:

1)使用以下代码将php错误报告添加到php文件:

ini_set('display_errors',1);
error_reporting(E_ALL);

2)在ifwhile / for循环内进行回音以查看是否符合条件

3)当使用变量查看实际查询的内容时,回显var_dump()查询:

$query = "SELECT TITLE, URL FROM rssfeeds as rss WHERE rss.USERID='" . $_SESSION["ROWID"] . "'";
echo var_dump($query);
$result = mysqli_query($db_handle,$query);

修复方法如下:

$num_rows = mysql_num_rows($qry); 
if ($num_rows <= 0) { 
    //unset($_SESSION['uid']);
    //unset($_SESSION['pwd']);
    header('location:index.php?msg=' . urlencode(base64_encode("Sorry, there is no username                 $username with the specified password.Try again!")));
} else {
    session_start();
    $_SESSION['ROWID'] = $sid ;
    header("location:dashboard.php");
}

应该是以下内容:(您之前没有定义$sid,也没有从数据库ROWID中实际检索$qry

if ($num_rows!==1) { 
    header('location:index.php?msg=' . urlencode(base64_encode("Sorry, there is no username $username with the specified password. Try again!")));
} else {
    $row = mysql_fetch_array($qry);
    session_start();
    $_SESSION['ROWID'] = $row['ROWID'] ;
    header("location:dashboard.php");
}

另外,在myfeed.php中:

$query = "SELECT URL FROM rssfeeds as rss WHERE rss.USERID='" . $_SESSION["ROWID"] . "'";

应该是以下内容:(虽然您试图稍后在脚本中调用它,但您没有检索TITLE字段。)

$query = "SELECT TITLE, URL FROM rssfeeds as rss WHERE rss.USERID='" . $_SESSION["ROWID"] . "'";

最后,您回显结果的语法错误。以下也在myfeed.php中:

if($result)
{
echo "BG </br>";
// while($row = mysqli_fetch_assoc($result)) {
  while($row = $result->fetch_array()){
 //  while( ($row = mysql_fetch_assoc($result))!== false){
 //echo "WELL DONE";
 echo "{$row["TITLE"]}  {$row["URL"]} </br>" ;
// $r[$index] = $row["URL"];
// $index++; 
// printf ("%s \n", $row["URL"]);

}
echo "HX";

应该是:

if($result){
    echo "BG </br>";
    while($row = $result->fetch_array()){
        echo $row["TITLE"]." ".$row["URL"]." </br>" ;
    }
    echo "HX";

最后一点,您在login.php中使用了mysql_*扩展名,但在myfeeds.php中使用了mysqli_*扩展名您应该将已弃用的扩展名的所有实例转换为新的扩展名。大多数/所有注释代码都可以删除,因为它也不需要。测试这些变化,让我知道它是如何工作的。