?
Solved

put variable in sql statement  php

Posted on 2016-10-27
3
Medium Priority
?
41 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

770 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