• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

Quotation marks PHP / interbase query

Hi,

I have a script that selects data from an interbase database using PHP, I am getting an error message when using quotation marks in my query,

I can query the database directly ok.

 Parse error: syntax error, unexpected 'abcde' (T_STRING) in C:\xampp\htdocs\precious\customer.php on line 9

SELECT FIRST 10 CLCTABRE ,CLCTGROUPE ,CLCTVILLIV, CLCTABRE
,case substring(CLCTABRE from 1 for 5)
         when 'abcde' then 'company 1'
         when 'adcdf' then 'company 2'
         else 'Unknown'
       end FROM CLIENT WHERE CLKTSOC = 100

Open in new window

0
dan_stan
Asked:
dan_stan
  • 4
  • 2
1 Solution
 
Ray PaseurCommented:
Try using backticks (lower case under the tilde, to the left of the number one on the top row of the keyboard).

Or post the entire PHP statement that is used with that query string, thanks.
0
 
Ray PaseurCommented:
Actually, it looks like it's a PHP quotes-related issue.  See if this article helps your understanding of the way PHP uses quotes and apostrophes...
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html
0
 
dan_stanAuthor Commented:
Here's the whole code. I'll check the article too.

<?php 
error_reporting(E_ALL);
include_once 'database_connect.php';

// COLLECT SOME DATA FROM IBASE
$dbh  = ibase_connect($host, $username, $password);
$stmt = 'SELECT FIRST 10 CLCTABRE ,CLCTGROUPE ,CLCTVILLIV, CLCTABRE
,case substring(CLCTABRE from 1 for 5)
         when 'abcde' then 'company 1'
         when 'adcdf' then 'company 2'
         else 'Unknown'
       end FROM CLIENT WHERE CLKTSOC = 100';
$sth  = ibase_query($dbh, $stmt);
// CREATE AN ARRAY OF OBJECTS
while ($row = ibase_fetch_object($sth))
{
    $newvalues[] = $row;
}

// THIS MAY NOT BE NEEDED - PHP GARBAGE COLLECTOR WILL HANDLE IT
ibase_free_result($sth);
ibase_close($dbh);

// START MYSQL
$dbh = new PDO('mysql:host=localhost;dbname=swdata', 'root', 'root');

// PREPARE A QUERY
$qry = "INSERT INTO USERDB (keysearch ,firstname,surname,fk_company_id) VALUES (:CLCTABRE, :CLCTGROUPE, :CLCTVILLIV, :COMPANY)";
$stmt = $dbh->prepare($qry);


// USE THE ARRAY OF OBJECTS
foreach ($newvalues as $obj)
{
    // BIND THE OBJECT PROPERTIES INTO THE QUERY
    $stmt->bindParam('CLCTABRE',   $obj->CLCTABRE);
    $stmt->bindParam('CLCTGROUPE', $obj->CLCTGROUPE);
    $stmt->bindParam('CLCTVILLIV', $obj->CLCTVILLIV);
	$stmt->bindParam('COMPANY',    $obj->COMPANY);

    // TRY THE QUERY
    try
    {
        $stmt->execute();
    }

    // IF THERE IS AN EXCEPTION THROWN BY PDO
    catch (PDOException $exc)
    {
        // IF THE EXCEPTION NUMBER IS NOT "DUPLICATE UNIQUE"
        if ($exc->errorInfo[1] != 1062)
        {
            $msg = "FAIL: $qry " . PHP_EOL . $exc->getmessage();
            trigger_error($msg, E_USER_ERROR);
        }
    }
}
                                            
?>

Open in new window

0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
Ray PaseurCommented:
Lines 7-12.  Try with double quotes, like this:
$stmt = "SELECT FIRST 10 CLCTABRE ,CLCTGROUPE ,CLCTVILLIV, CLCTABRE
,case substring(CLCTABRE from 1 for 5)
         when 'abcde' then 'company 1'
         when 'adcdf' then 'company 2'
         else 'Unknown'
       end FROM CLIENT WHERE CLKTSOC = 100";

Open in new window

0
 
dan_stanAuthor Commented:
Thanks, that did the trick.
0
 
Ray PaseurCommented:
Thanks for the points -- it's a great question.  I'm going to update the article to show how the quotes here caused the parse error.  Best regards, ~Ray
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now