Jump to content

Query For Description Doesn't Work/ ID Does


justlukeyou

Recommended Posts

Hi,

 

I have a query based on ID number which works fine.  However, I am now trying to use it to query the description in the same manner.  However, when I use the description it doesn't echo anything at all and produces no errors.

 

This doesn't work.  The table is productdbase and the field is description.

 

<a href="/products/productsqueryresults.php?description=bed" rel="nofollow"  class='articlesfilterlink'>Bed</a>

 

<?php
if (isset($_GET['description']))
$ID = mysql_real_escape_string($_GET['description']);
$sql = "SELECT * FROM productdbase WHERE description = '$description'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res); // no need to loop since you are retrieving only one row
$num_rows = mysql_num_rows($res); // check to see if any results were found, just in case someone puts an ID in the url without clicking on your link
?>

 

This does work.  The table is articles and the field is ID.

 

<?php
if (isset($_GET['ID']))
$ID = mysql_real_escape_string($_GET['ID']);
$sql = "SELECT * FROM articles WHERE ID = '$ID'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res); // no need to loop since you are retrieving only one row
$num_rows = mysql_num_rows($res); // check to see if any results were found, just in case someone puts an ID in the url without clicking on your link
?>

 

I cant see why the ID works and not the description without creating an error.

Link to comment
Share on other sites

Hi, I changed the mistake of the ID to description but it still doesnt work.  The problem is it doesnt create an error, just doesn't echo anything.

 

Any ideas please?

 

<?php
if (isset($_GET['description']))
$description = mysql_real_escape_string($_GET['description']);
$sql = "SELECT * FROM productdbase WHERE description = '$description'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res); // no need to loop since you are retrieving only one row
$num_rows = mysql_num_rows($res); // check to see if any results were found, just in case someone puts an description in the url without 

clicking on your link
?>

Link to comment
Share on other sites

$sql = "SELECT * FROM productdbase WHERE description = '$description'";
echo $sql; // <--- add this line here

 

Now copy and paste what it is output.  Login to your mysql database, like through phpmyadmin.  Go to the database and productdbase table and click on the SQL button and paste that query in and run the query.  Do you get results returned?

Link to comment
Share on other sites

Thanks mate,

 

This is what it echoes:

 

SELECT * FROM productdbase WHERE description = 'lukas'

 

This is what MySQl returns "MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0046 sec )"

 

I find this really strange because it works on the ID and I have a search code which searches the description but querying the description doesn't seem to work.  Very puzzling.

Link to comment
Share on other sites

I really wish you'd stop with the "..but it works with ID!" it's starting to get pretty annoying...  the code is similar but different in many ways, it is NOT the same code.  And on that note, you need to focus here and catch on to the whole debugging process.  Line by line, compare the differences between the two scripts, setup points to test at each difference.  Test by echoing out the relevant variables, etc..

 

So you have queried your database directly and got 0 results.  As spiderwell mentioned, are you querying the right table? do you see a value of 'lukas' in your description column in your table (example, SELECT description FROM productdbase to get a dump of all description values with no where clause)

Link to comment
Share on other sites

Aha, the problem is that if the description is just one word and I use that one word then it will find it.  So 'widget' and 'widget' will work but 'red widget' wont be found.

 

Is there a way to query using one single word and for it to search everything. I tried the following in MySQL that didnt work.

 

SELECT * FROM productdbase WHERE description = '%lukas%'

 

I thought % was way to ignore what was left or right?

Link to comment
Share on other sites

No offense, but neglecting to mention what you are trying to do, what values you are testing is not a coding issue, it's a common sense issue.  You told me you were testing with "test"!  You are not helping me help you!

 

= is an exact match operator in SQL . % only works with regex or 'contains' type operators, for example

 

SELECT * FROM productdbase WHERE description LIKE '%lukas%'

 

What exactly are you wanting to do here, return results for any of the words, like "foo bar" will return

 

"some foo description"

"some bar description"

 

or does it have to contain both words, but can occur anywhere, like

 

"some foo description bar"

Link to comment
Share on other sites

Thanks .josh for your great help.

 

Like foo bar would be fine so it picks up either word.

 

I tried both of the following % inserts but neither returned anything.  Does the link from the previous page need to be changed in anyway?

 

if (isset($_GET['description']))
$description = mysql_real_escape_string($_GET['description']);
$sql = "SELECT * FROM productdbase WHERE description = '%$description%'";

 

if (isset($_GET['description']))
$description = mysql_real_escape_string($_GET['description']);
$sql = "SELECT * FROM productdbase WHERE description = '%$lukas%'";

Link to comment
Share on other sites

if (isset($_GET['description'])) {
  $description = explode(" ",urldecode($_GET['description']));
  $description = array_map('mysql_real_escape_string',$description);
  $description = "'".implode("','",$description)."'";
  $sql = "SELECT * FROM productdbase WHERE description IN ($description)";
  echo $sql;
}

Link to comment
Share on other sites

here is a more complete example:

 

if (isset($_GET['description'])) {
  $description = explode(" ",urldecode($_GET['description']));
  $description = array_map('mysql_real_escape_string',$description);
  $description = "'".implode("','",$description)."'";
  $sql = "SELECT * FROM productdbase WHERE description IN ($description)";
  $res = mysql_query($sql) or die(mysql_error());
  while ($row = mysql_fetch_assoc($res)) {
    echo $row['description'] . "<br/>";
  }
}

Link to comment
Share on other sites

Okay, this will perform a query that searches the description column for a value that contains the value you pass in description query string param.  If the value is more than one word, it will return results for each word.  So for example,

 

yoursite.com/yourscript.php?description=foo%20bar

 

will return

 

some foo description

some bar description

some foo bar description

 

 

if (isset($_GET['description'])) {
  $description = explode(" ",urldecode($_GET['description']));
  $description = array_map('mysql_real_escape_string',$description);
  $description = "'%" . implode("%' OR LIKE '%",$description). "%'";
  $sql = "SELECT * FROM productdbase WHERE description LIKE $description";
  $res = mysql_query($sql) or die(mysql_error());
  while ($row = mysql_fetch_assoc($res)) {
    echo $row['description'] . "<br/>";
  }
}

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.