Solved

put variable in sql statement  php

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

919 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

20 Experts available now in Live!

Get 1:1 Help Now