Pregunta

I'm displaying Spotlight post on search page.

When a user enters a keyword to search for a post I like to bring a Spotlight post using that keyword. If there is no post with that keyword then I would want to just bring any Spotlight post from database.

My questions is, can I check this in a MySQL query to see if they will be any results with this keyword, if not then ignore this keyword?

My query

SELECT id, title, desc 
  FROM post 
 WHERE isActive = 1 
   AND title = 'keyword'

but if I'm getting 0 results with this query I would like to ignore this and run this instead

SELECT id, title, desc 
  FROM post 
 WHERE isActive = 1
¿Fue útil?

Solución 2

This is what I have done to solve my problem.

$select = " SELECT id, title, desc ";
$from = ' FROM post';
$where = 'WHERE isActive=1 and title="%$keywords%"';
$sql = $select.$from.$where;

if no result then overwrite

$where = 'WHERE isActive=1';
$sql = $select.$from.$where;

If anyone know anyother way please let me know.

Otros consejos

desc is MySQL's reserved keyword (used for ordering results in a *desc*ending order). To use it as a column name, you need to put it in backticks

$select = " SELECT id, title, `desc` ";
$from = ' FROM post';
$where = 'WHERE isActive=1 and title="%$keywords%"';
$sql = $select.$from.$where;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top