Jump to content

Just can't figure this out.


-Karl-

Recommended Posts

I have a script to search a database and return any finds.

 

However, I receive this error:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /***/dosearch.php  on line 17

 

This is line 17:

   $query = "SELECT * FROM `news` WHERE (`id` LIKE '%$_POST['search']%' OR `title` LIKE '%$_POST['search']%' OR `thumb` LIKE '%$_POST['search']%' OR `news` LIKE '%$_POST['search']%' OR `postedby` LIKE '%$_POST['search']%')";

 

I just can't find what's wrong.

Link to comment
https://forums.phpfreaks.com/topic/195918-just-cant-figure-this-out/
Share on other sites

<?php
if (isset($_GET['search'])) {

         echo <<<HTML
<center>You searched for {$_GET['search']}</center>
HTML;

$con = mysql_connect("localhost","****","*****");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("****", $con);
   
   // Collect guides
$query = "SELECT * FROM `news` WHERE (`id` LIKE '%$_POST['search']%' OR `title` LIKE '%$_POST['search']%' OR `thumb` LIKE '%$_POST['search']%' OR `news` LIKE '%$_POST['search']%' OR `postedby` LIKE '%$_POST['search']%')";
   $run = mysql_query($query);
   if($run){
      while($arr = mysql_fetch_assoc($run)){
         echo <<<HTML
<font color="blue">{$arr['title']}</font><br/>
<font color="white">{$arr['news']}</font><br/>
<a href="#">{$arr['id']}</a><br/><br/><br/>
HTML;
      }
   }else{
      die("Unable to connect to database.");
   }
}
?>

 

There's the full code.

try this:

 

<?php
if (isset($_GET['search'])) {

         echo <<<HTML
<center>You searched for {$_GET['search']}</center>
HTML;

$con = mysql_connect("localhost","****","*****");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("****", $con);
   
   // Collect guides
$query = "SELECT * FROM `news` WHERE (`id` LIKE $_POST['search'] OR `title` LIKE $_POST['search'] OR `thumb` LIKE $_POST['search'] OR `news` LIKE $_POST['search'] OR `postedby` LIKE $_POST['search'])";
   $run = mysql_query($query);
   if($run){
      while($arr = mysql_fetch_assoc($run)){
         echo <<<HTML
<font color="blue">{$arr['title']}</font><br/>
<font color="white">{$arr['news']}</font><br/>
<a href="#">{$arr['id']}</a><br/><br/><br/>
HTML;
      }
   }else{
      die("Unable to connect to database.");
   }
}
?>

<?php
if (isset($_GET['search'])) {

         echo <<<HTML
<center>You searched for {$_GET['search']}</center>
HTML;

$con = mysql_connect("localhost","****","*****");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("****", $con);
   
   // Collect guides
$query = "SELECT * FROM `news` WHERE (`id` LIKE $_POST['search'] OR `title` LIKE $_POST['search'] OR `thumb` LIKE $_POST['search'] OR `news` LIKE $_POST['search'] OR `postedby` LIKE $_POST['search'])";
   $run = mysql_query($query) or die((mysql_error());
   if($run){
      while($arr = mysql_fetch_assoc($run)){
         echo <<<HTML
<font color="blue">{$arr['title']}</font><br/>
<font color="white">{$arr['news']}</font><br/>
<a href="#">{$arr['id']}</a><br/><br/><br/>
HTML;
      }
   }else{
      die("Unable to connect to database.");
   }
}
?>

 

That should give another error. Give me that one

<?php
if (isset($_GET['search'])) {

         echo <<<HTML
<center>You searched for {$_GET['search']}</center>
HTML;

$con = mysql_connect("localhost","****","****");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("****", $con);
   
   // Collect guides
   $query = "SELECT * FROM `news` WHERE (`id` LIKE '%{$_GET['search']}%' OR `title` LIKE '%{$_GET['search']}%' OR `thumb` LIKE '%{$_GET['search']}%' OR `news` LIKE '%{$_GET['search']}%' OR `postedby` LIKE '%{$_GET['search']}%')";
   $run = mysql_query($query);
   if($run){
      while($arr = mysql_fetch_assoc($run)){
         echo <<<HTML
<font color="blue">{$arr['title']}</font><br/>
<font color="white">{$arr['news']}</font><br/>
<a href="#">{$arr['id']}</a><br/><br/><br/>
HTML;
      }
   }else{
      die("Unable to connect to database.");
   }
}
?>

 

That's my code now, how could I change it to display a "no results found" if nothing was found from the query. And how could I join different tables and search each of the rows in there?

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.