4 Replies - 290 Views - Last Post: 16 March 2014 - 06:14 AM Rate Topic: -----

#1 H_E_E  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-July 13

Using php GET within SQL query in PHP

Posted 09 March 2014 - 07:59 AM

Hi,I try to make a DB search with the GET value The url is: page/?value=tim
                        if(!empty($_GET['value'])){
			
			
			$query="SELECT*FROM'.$_GET['value'].'";}


I get the following error:

Parse error: syntax error, unexpected ''.$_GET['' (T_CONSTANT_ENCAPSED_STRING)

Only this part gives error, when I cahnge the query to "SELECT*FROM table"
it works
How can I fix it?

I use apache and PHP5.5

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Using php GET within SQL query in PHP

#2 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Using php GET within SQL query in PHP

Posted 09 March 2014 - 08:12 AM

You need to properly use the concatenation operator (.)... It needs to be out of the quotes ("")...

The correct way is,
$query = "SELECT * FROM'" . $_GET["value"] . "'";



Note the quotes " before the first concatenation operator and after the second concatenation operator...

This post has been edited by AKMafia001: 09 March 2014 - 08:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Using php GET within SQL query in PHP

Posted 09 March 2014 - 08:16 AM

Thread moved to the PHP forum. Try to avoid posting language specific questions into generic forums like Web Development.


The problem there is extremely obvious. So much so that it's pretty clear that you don't really understand how to use SELECT queries. That is basic stuff you need to know if you are to have any hope of working with a SQL database. Without it, you are doomed to fail.

My suggestion would be that you study the basic SQL commands. Learn how to use them first, and then try to use them.

I wrote a tutorial on the basics of the SQL queries. Should help you: MySQL Basics
Was This Post Helpful? 0
  • +
  • -

#4 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: Using php GET within SQL query in PHP

Posted 09 March 2014 - 04:17 PM

Its very simple.
Change:
$query="SELECT*FROM'.$_GET['value'].'";

To:
$query="SELECT * FROM ".$_GET['value'].";


You confused the code when you added quotation marks with single-quotation marks.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,343
  • Joined: 08-August 08

Re: Using php GET within SQL query in PHP

Posted 16 March 2014 - 06:14 AM

Using GET or POST values in a query is asking for trouble. You should be using prepared statements, in which case you never use any data directly in a query. Certainly not data from GET or POST.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1