Solved

put variable in sql statement  php

Posted on 2016-10-27
3
39 Views
Last Modified: 2016-10-27
This code is work if I change $table to  gel in line 12     how can I change $table to correct way

[list=1]


$table = "gel";

echo "$table";

$sql ='INSERT INTO pong8_maintable ("timestamp8","Last","TradePrice", "TradeVolume" , "BestBid") 
SELECT 
cast(public.gel."Timestamp" as timestamp) ,

cast(public.$table. "Last" as numeric)  ,

cast(public.gel."Trade Price" as numeric)  ,  
[/list]

Open in new window




Error
pg_query(): Query failed: ERROR: syntax error at or near "$" LINE 8: cast(public.$table. "Last" as numeric) , ^
Error
Error while accessing the database:
ERROR: syntax error at or near "$" LINE 8: cast(public.$table. "Last" as numeric) ,
0
Comment
Question by:teera
[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
3 Comments
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 41862035
I think you may want to change your quote marks to allow variable substitution.
https://www.experts-exchange.com/articles/12241/Quotation-Marks-in-PHP.html
1
 
LVL 27

Accepted Solution

by:
skullnobrains earned 250 total points
ID: 41862772
this works

$whatever = "foo $table bar";

this works as well

$whatever = 'foo '.$table.' bar';

this does not

$whatever = 'foo $table bar'; // substitution does not work inside single quotes

this is suited for user input and MySQL

$query = 'whatever = '.var_export($variable,true).' rest of query'; // and lets var_export handle the quoting if required

in postgres you additionally need to be careful with variable types. given the fact this is a table name use either of the 2 first samples. with the first, you need to backslash the existing double-quotes in the string or use single quotes instead.
0
 

Author Closing Comment

by:teera
ID: 41863295
Thank you
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

738 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