Jump to content

Need help on a query issue


FutonGuy

Recommended Posts

Hi,

 

I am having problem solving my query error, would appreciate if someone can assist me.

 

$val_d = $_GET['val_d'];

$sql = "SELECT *
            FROM device";

$sql .="WHERE device_num LIKE '%$val_d%' ";

 

the error code i had was:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE '%" . urlencode($val_d) . "%'' at line 2"

 

There is no problem if my query is

$sql = "SELECT *
            FROM device
           WHERE device_num LIKE '%$val_d%' ";

 

I did the 1st method is because i need it in my later script.  Appreciate if someone can assist me and guide me along.

Cheers

 

Link to comment
https://forums.phpfreaks.com/topic/186757-need-help-on-a-query-issue/
Share on other sites

The error you provided does not match your code. As it seems you are trying to include a urlencoded string, but you forgot to exit out of the current string.

 

As for the first statement, you need a space before the WHERE or after the FROM device for that to work properly. But yea, show us the proper code to solve your issue.

 

$_GET['val_d'] is an input that came from another php file. This code here is to allow user to save the results which is shown on the web browser. So when user clicked on [save result], $_GET['val_d'] will pass the value to this code here.

 

Here's my code:

 

$val_d = $_GET['val_d'];

$sql = "SELECT *
             FROM device";

$sql .="WHERE device_num LIKE '%$val_d%' ";

$results = mysql_query($sql) or die (mysql_error()); 

$count = mysql_num_fields($results);

for ($i = 0; $i < $count; $i++){
    $header .= mysql_field_name($results, $i)."\t";
} 

while($row = mysql_fetch_row($results)){
  $line = '';
  foreach($row as $value){
    if(!isset($value) || $value == ""){
      $value = "\t";
    }else{
# important to escape any quotes to preserve them in the data.
      $value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
      $value = '"' . $value . '"' . "\t";
    }
    $line .= $value;
  }
  $data .= trim($line)."\n";
}
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
  $data = str_replace("\r", "", $data);


# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
  $data = "\nno matching records found\n";
}

# This line will stream the file to the user rather than spray it across the screen
header("Content-type: application/octet-stream");

# replace excelfile.xls with whatever you want the filename to default to
header("Content-Disposition: attachment; filename=excelfile.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo $header."\n".$data; 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.