Jump to content

Trying to use php variables in a mysql select query


zacron

Recommended Posts

<?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

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.

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";

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.