Jump to content

Trying to use php variables in a mysql select query


zacron

Recommended Posts

$var = @$_GET['q'] ;

$trimmed = trim($var);

$table = @$_GET['field'];

$query="SELECT * FROM contacts WHERE @'table' contains @'trimmed' order by id";

$result=mysql_query($query);

$num=mysql_numrows($result);

 

 

Why wont this work?

 

Zacron

Link to comment
Share on other sites

<?php

include("dbinfo.inc.php");

mysql_connect(localhost,$username,$password);

@mysql_select_db(sibbaldreports) or die( "error 101... uh oh, call tech support!");

$var = @$_GET['q'] ;

$trimmed = trim($var);

$tab = @$_GET['field'];

$query="SELECT * FROM contacts WHERE @'tab' contains @'trimmed' order by id";

 

 

 

 

// get results

  $query .= " limit $s,$limit";

  $result = mysql_query($query) or die("Couldnt execute query");

 

 

I'm getting "couldn't execute query"?

 

I dunno

Link to comment
Share on other sites

I found you can't use "table" as any part of your variable if you plan to use it in your query, try changing it to $tab and see if it works

 

Yes you can. Even if the table's name was `table`, as long as it is enclosed in backticks, it's fine.

Link to comment
Share on other sites

hmm every time I used it even with the ' I would get an sql syntax error until I changed '$table' to '$tab' in which it worked fine.

 

Sorry to get off topic but what is "contains" in the query? I've never used or seen it for that matter, are you trying to get something that is just similar to the input? if so try:

 

$query="SELECT * FROM contacts WHERE '$tab' LIKE '%$trimmed%' order by id";

otherwise try:

$query="SELECT * FROM contacts WHERE '$tab' = '$trimmed' order by id";

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.