-2

My sql select query works in phpmyadmin but not working in my php code.Here is my code.Please help me to find a solution.Thank you in advance for your help

    $conn=mysqli_connect("localhost","root","");
    if(!$conn) {
        die(mysqli_error());
    }
    $db=mysqli_select_db($conn,"dbname");
    if(!$db) {
        die(mysqli_error());
    }
    $to_date=$_REQUEST['to_date'];
    $r_date=$_REQUEST['date'];
    $role=$_REQUEST['role'];
    $user_name=$_REQUEST['user'];
    if($_REQUEST['role']='all'||$_REQUEST['role']="") {
        $sql="SELECT * 
                FROM `daily_log` 
                WHERE date BETWEEN '$r_date'AND '$to_date'  
                AND category='Activity'";
    }else{
        $sql="SELECT * 
                FROM `daily_log` 
                WHERE category='Activity' 
                AND assigned_to = '$user_name' 
                AND date BETWEEN '$r_date' AND '$to_date'";
        echo $sql;
    }
    $exe=mysqli_query($conn,$sql);         
    $i=1;
    while($row=mysqli_fetch_array($exe)) {
        ...table rows..
    }
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Dona
  • 1
  • 3
  • 1
    `$_REQUEST['role']='all'||$_REQUEST['role']=""` these are *assignments*, not comparisons. – Qirel Sep 13 '17 at 11:09
  • 1
    You're already using an API that supports **prepared statements** with bounded variable input, you should utilize parameterized queries with placeholders (prepared statements) to protect your database against [SQL-injection](http://stackoverflow.com/q/60174/)! Get started with [`mysqli::prepare()`](http://php.net/mysqli.prepare) and [`mysqli_stmt::bind_param()`](http://php.net/mysqli-stmt.bind-param). – Qirel Sep 13 '17 at 11:09
  • You are expected to do your own debugging and post relevant error-messages. Use [`mysqli_error()`](http://php.net/manual/en/mysqli.error.php) to get any errors from MySQL, and use `error_reporting(E_ALL); ini_set('display_errors', 1);` to find any PHP errors. – Qirel Sep 13 '17 at 11:10
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Sep 13 '17 at 11:11
  • which query is not working? – Ndroid21 Sep 13 '17 at 11:12
  • @programmingArrow See [this comment](https://stackoverflow.com/questions/46195938/my-select-query-works-in-phpmyadmin-but-not-in-my-code#comment79354135_46195938) Issue is with the `IF` test so probably only one query ever gets run – RiggsFolly Sep 13 '17 at 11:14

1 Answers1

0

Your first query has syntax problem near '$r_date'AND Put a space in between '$r_date' and AND

Finally the query will be:

$sql="SELECT *  
        FROM `daily_log` 
        WHERE date BETWEEN '$r_date 'AND '$to_date'  
        AND category='Activity'";

You should include mysql error information in your question.

And fix this if($_REQUEST['role']='all'||$_REQUEST['role']="") { too. You are assigning value to $_REQUEST['role'] not comparing.

alaminio
  • 82
  • 2
  • 12
  • Thankyou everyone for advices and great help.It worked.The assingnment operator was the problem..! – Dona Sep 14 '17 at 04:26