Link to home
Start Free TrialLog in
Avatar of lawrence_dev
lawrence_dev

asked on

How do I fix this error with quotation marks?

How do I correctly structure thsee quotation marks in this string??   ' . $supplier['table'] . 'Cats''     I have attempted several different ways with single and double quotes and still get errors.

         

LEFT JOIN ' . $supplier['table'] . 'Cats'' USING (Category)');                

 

Open in new window


Thanks!
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Your string does not make sense - I am providing the options based on what you provided but I cannot tell what you are trying to do: there is a lone ' in the middle of it - what is the output you are aiming for?
Option 1
Escape with \
LEFT JOIN ' . $supplier['table'] . 'Cats\'\' USING (Category)');

Open in new window

     

Option 2
Use double quotes for the string
somefunction ("SELECT ...LEFT JOIN " . $supplier['table'] . "Cats' USING (Category)");

Open in new window

Option 2a
Use double quotes for the string and embed var with { }
somefunction ("SELECT ...LEFT JOIN {$supplier['table']} Cats' USING (Category)");

Open in new window

Option 3 [PREFERRED]
Use HEREDOC. Allows use of both single and double quotes without escaping and embedding of variables
$query = <<< QUERY
SELECT ...
LEFT JOIN {$supplier['table']} Cats' USING (Category)
QUERY;

Open in new window

Avatar of lawrence_dev
lawrence_dev

ASKER

Julian,

I am unable to get the code to work with the above examples.  Here is the full string for the Left Join.  
$query3 = $conn2->query("SELECT DISTINCT SubCategory, Category, SubCatID FROM " . $supplier['table'] . ".SubCats LEFT JOIN " . $supplier['table'] . ".Cats USING (Category)");

Open in new window


Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You gave us this (double single quotes - was that supposed to be single double quotes?)
LEFT JOIN ' . $supplier['table'] . 'Cats'' USING (Category)');

Open in new window

Now you give this
$query3 = $conn2->query("SELECT DISTINCT SubCategory, Category, SubCatID FROM " . $supplier['table'] . ".SubCats LEFT JOIN " . $supplier['table'] . ".Cats USING (Category)");

Open in new window

That's not the same code
If you used the suggestion above and put {} around your strings and not use concatenation you would have got this
$query3 = $conn2->query("SELECT DISTINCT SubCategory, Category, SubCatID FROM {$supplier['table']}.SubCats LEFT JOIN {$supplier['table']}.Cats USING (Category)");

Open in new window

My preference would have been using HEREDOC as follows
$sql = <<< QUERY
SELECT DISTINCT
  SubCategory, 
  Category, 
  SubCatID 
FROM 
   `{$supplier['table']}.SubCats` LEFT JOIN `{$supplier['table']}.Cats`
USING
  (Category)
QUERY;
$query3 = $conn2->query($sql);

Open in new window

Much easier to see what is going on.

BTW $supplier['table'] is misleading - is that meant to be a database reference with .cats and .subcats being the actual tables?