Jump to content

Recommended Posts

Can someone review this piece of code and advise what's missing that's preventing the SQL query from working?

Issue: no results being echoed out from $results.

 

Note: I confirmed the db connection  established (only the query not working - no results pulled from db and displayed via echo statements).

 


$db = mysql_connect($host,$user,$pw)
        or die("Cannot connect to MySQL.");

mysql_select_db($database,$db)
        or die("Cannot connect to database.");

echo "Success!  Connected to database ".$database."<br /><br />";

// $jsearch is variable sent in from form "title" name
$jsearch = $_POST['jobsearch'];

// I confirmed that all the field names are correct

  //-query  the database table
  $sql="SELECT id,title,details,status FROM jobs WHERE title = '$jsearch';";
  //-run  the query against the mysql query function
  $result=mysql_query($sql) or die("<br>Query string: $result<br>Returned error: " . mysql_error() );
  //-create  while loop and loop through result set
  while($row=mysql_fetch_array($result)){
          $title  =$row['title'];
	  echo title."<br />";
          $details=$row['details'];
	  echo $details."<br />";
	  $status=$row['status'];
	  echo $status."<br />";
          $ID=$row['id'];

Link to comment
https://forums.phpfreaks.com/topic/221130-query-not-working/
Share on other sites

I have acouple of entries in the "jobs" table and I'm performing the search - i.e. "Customer Service Agents" - and no results being displayed from the script.

 

I posted the script here as I've racked my brain looking fo syntax errors but didn't find any...

So are you saying the code looks good to you?

 

I'm just trying to isolate the problem - thx

Link to comment
https://forums.phpfreaks.com/topic/221130-query-not-working/#findComment-1144974
Share on other sites

I added a closing statement to the while loop, not sure if this was your complete code or not.

 

$db = mysql_connect($host,$user,$pw)
        or die("Cannot connect to MySQL.");

mysql_select_db($database,$db)
        or die("Cannot connect to database.");

echo "Success!  Connected to database ".$database."<br /><br />";

// $jsearch is variable sent in from form "title" name
$jsearch = $_POST['jobsearch'];

// I confirmed that all the field names are correct

  //-query  the database table
  $sql="SELECT id,title,details,status FROM jobs WHERE title = '$jsearch';";
  //-run  the query against the mysql query function
  $result=mysql_query($sql) or die("<br>Query string: $result<br>Returned error: " . mysql_error() );
  //-create  while loop and loop through result set
  while($row=mysql_fetch_array($result)){
          $title  =$row['title'];





  echo title."<br />";
          $details=$row['details'];





  echo $details."<br />";





  $status=$row['status'];





  echo $status."<br />";
          $ID=$row['id'];
}

Link to comment
https://forums.phpfreaks.com/topic/221130-query-not-working/#findComment-1144978
Share on other sites

Hi,

 

My original problem is resolved - the query works now.

However, I need to figure out the best way to perforrm the query based on the captured $jsearch field.

 

For example, If the string "Customer Service Rep" is defined for $jsearch, then the query should search for any of the three words to match the title db field. I thought about STRTOK() function but wanted to know if anyone had a better suggestion.

 

Any input much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/221130-query-not-working/#findComment-1145152
Share on other sites

Ok, I went a different direction and used the EXPLODE functiont to break apart the string to search on the individual words (1st one for now...) See code below:

 


// Break up string to search individual words
$search_array = explode(" ", $jsearch);
$word = $search_array[0];

  //-query  the database table
  $sql="SELECT id,title,details,cat FROM jobs WHERE title LIKE '$word%';";

 

My issue now is that I'm not matching on certain words where Case (upper or lower) between the $jsearch variable and what's defined in the table.

 

I read about BINARY option but need some guidance on implementing this feature in the search query.

 

Again, I just want to be able to have any of the words - found in $jsearch  which is passed to the script from a form - parsed and if ANY of the words match ANY word within that "jobs" table, that record is displayed.

Link to comment
https://forums.phpfreaks.com/topic/221130-query-not-working/#findComment-1145172
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.