php - SQL Query not retrieving proper results -


here's login.php script use determine whether or not let user in.

<?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 username.";  }  if ($_post['pass'] == "") {      $errors[] = "please enter 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 `user` ".     " (username='$username' or email='$username') , 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 no username                 $username 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); }  }    }    ?>   

if login successful, redirect user 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 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> 

the problem occurs here in 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 rssfeeds,user rssfeeds.userid=user.rowid"; // $query = "select group_concat(r.url) url user u left join rssfeeds r  on u.rowid = r.userid userid = $_session"; // $query = "select url rssfeeds rss rss.userid=$s"; // $query = "select `rssfeeds`.`url` `rssfeeds` `rssfeeds`.`userid`=$_session['rowid']"; //  $query = "select  url rssfeeds rss ,user userrss rss.userid='$sid'"; //$query = "select  rssfeeds.url rssfeeds left join user on rssfeeds.userid=user.rowid";  $query = "select url rssfeeds rss rss.userid='" . $_session["rowid"] . "'"; //$query = "select url rssfeeds rss,user 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());  }      ?> 

the commented out statements 1 have tried did not work.i not able retrieve urls database table , display them on page. of output :

bg hx

i have 2 tables in database :

  • user
  • rssfeeds

'user' contains 4 columns namely :

  • rowid (auto-increment,primary)
  • username
  • email
  • pwd

whereas 'rssfeeds' has 4 :

  • rssfeedid
  • userid
  • title
  • url

i quite sure problem lies in query in myfeed.php in $_session part.

new answer based upon new problem op:

here pointers when debugging code , how found problems:

1) add php error reporting php files using following code:

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

2) echo within if , while/for loops see if condition met

3) echo var_dump() queries when using variables see actual query ran so:

$query = "select title, url rssfeeds rss rss.userid='" . $_session["rowid"] . "'"; echo var_dump($query); $result = mysqli_query($db_handle,$query); 

the fix following:

$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 no username                 $username specified password.try again!"))); } else {     session_start();     $_session['rowid'] = $sid ;     header("location:dashboard.php"); } 

should following: (you did not define $sid before nor retrieved rowid database $qry)

if ($num_rows!==1) {      header('location:index.php?msg=' . urlencode(base64_encode("sorry, there no username $username specified password. try again!"))); } else {     $row = mysql_fetch_array($qry);     session_start();     $_session['rowid'] = $row['rowid'] ;     header("location:dashboard.php"); } 

also, in myfeed.php:

$query = "select url rssfeeds rss rss.userid='" . $_session["rowid"] . "'"; 

should following: (your not retrieving title field though trying call later on in script.)

$query = "select title, url rssfeeds rss rss.userid='" . $_session["rowid"] . "'"; 

and lastly, syntax echoing results wrong. following in 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"; 

should be:

if($result){     echo "bg </br>";     while($row = $result->fetch_array()){         echo $row["title"]." ".$row["url"]." </br>" ;     }     echo "hx"; 

one last note, using mysql_* extension in login.php using mysqli_* extensions in myfeeds.php should convert instances of deprecated extension new extensions. most/all of commented code can removed not needed well. test these changes , let me know how works.


Comments