Solved

put variable in sql statement  php

Posted on 2016-10-27
3
26 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
3 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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 26

Accepted Solution

by:
skullnobrains earned 250 total points
Comment Utility
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
Comment Utility
Thank you
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now