Solved

Quotation marks PHP / interbase query

Posted on 2013-11-06
6
387 Views
Last Modified: 2013-11-06
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
Comment
Question by:dan_stan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39627081
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39627083
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
 

Author Comment

by:dan_stan
ID: 39627091
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39627099
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
 

Author Comment

by:dan_stan
ID: 39627156
Thanks, that did the trick.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39627169
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

Upcoming Webinar: Securing your MySQL/MariaDB data

Join Percona’s Chief Evangelist, Colin Charles as he presents Securing your MySQL®/MariaDB® data on Tuesday, July 11, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to count occurrences of each item in an array.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question