Solved

put variable in sql statement  php

Posted on 2016-10-27
3
40 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

695 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