WebMatrix PHP Query Not working

74 views Asked by At

Just a heads up I have been writing PHP now for about 3 days. I am having trouble with a query. When I run the query in the query tool it finds exactly what I am looking for. I have tried every variation of syntax I can think of from using LIKE (how it is right now) and = and i cannot get it to work. I am sure there is a simpler way to do the rest of the code but my main concern right now is the query. Thanks for all the help.

        $Incident_D = $_POST['Incident_D'];
        $Incident_D = strtotime($Incident_D);
        $Incident_T = $_POST['Incident_T'];
        $Incident_T = strtotime($Incident_T);
        $Incident_T = ($Incident_T % 86400);
        $Incident_DT = $Incident_D + $Incident_T; 
        $sql = "SELECT dept.Department From department dept inner join near_miss NM on dept.Dept_Index = NM.Dept_Index Where NM.Incident_DT LIKE '%" . $Incident_DT . "%'";
        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result) > 0) {
            // output data of each row
            while($row = mysqli_fetch_assoc($result)) {
                $result1 = $row['Department'];
            }
            } else {
                $result1 = "No Response";
            }

       $result = $result1;

If i manually type in the value for $Incident_DT it is able to find it. This is why I think the problem is in how the value is being stored in $Incident_DT. also when I echo the value for the variable and paste it into the query tool it works.

2

There are 2 answers

2
Kirk Powell On
$sql = "SELECT dept.Department From `department` dept inner join near_miss NM on dept.Dept_Index = NM.Dept_Index Where NM.Incident_DT LIKE '%" . $Incident_DT . "%'";

The back tick is necessary for PHP on the TABLENAME.

1
Circum On

Try this:

declare a new variable with value:

$filter_var="%".$Incident_DT."%";

Then:

       $sql = "SELECT dept.Department From department dept inner join near_miss NM 
on dept.Dept_Index = NM.Dept_Index Where NM.Incident_DT LIKE '$filter_var'";