Jump to content

PHP and MySQL Help


light87

Recommended Posts

Hello:

 

I'm having a problem with my PHP code.  I receive the following two error messages when I try to run the code:

 

Warning: mysqli_query() [function.mysqli-query]: Empty query in /home/content/PRIVATE/html/riskyjobs_riskybusiness_Database_TableSetup_riskyjobs_Table_Chapter9_Page539_search_final.php on line 108

 

 

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/content/PRIVATE/html/riskyjobs_riskybusiness_Database_TableSetup_riskyjobs_Table_Chapter9_Page539_search_final.php on line 111

 

The code is as follows:

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

 

 

<head>

 

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <title>Risky Jobs - Search</title>

  <link rel="stylesheet" type="text/css" href="style_Page539_final.css" />

 

 

</head>

 

 

<body>

 

  <img src="riskyjobs_title.gif" alt="Risky Jobs" />

 

  <img src="riskyjobs_fireman.jpg" alt="Risky Jobs" style="float:right" />

 

  <h3>Risky Jobs - Search Results</h3>

 

 

 

<?php

 

 

// 12,30,09

// Filename:C:,HeadFirst_PHP_MYSQL,Chapter9,

// riskyjobs_riskybusiness_Database_TableSetup_riskyjobs_Table_Chapter9_Page539_search_final.php

// Grab the sort setting and search keywords from the URL using GET

 

  $sort = $_GET['sort'];

  $user_search = $_GET['usersearch'];

 

// Start generating the table of results

echo '<table border="0" cellpadding="2">';

 

// Generate the search result headings

echo '<tr class="heading">';

echo '<td>Job Title</td><td>Description</td><td>State</td><td>Date Posted</td>';

echo '</tr>';

 

// Connect to the database

 

require_once('riskyjobs_riskybusiness_Database_TableSetup_riskyjobs_Table_Chapter9_Page539_connectvars_final.php');

$dbc = mysqli_connect('PRIVATE', 'PRIVATE', 'PRIVATE', 'PRIVATE');

 

 

// Query to get the results

 

 

 

// Beginning of ADDED the following on 12,30,09

 

// Beginning of ADDED the following on 1,1,10

 

function build_query($user_search) {

 

// End of ADDED the following on 1,1,10

 

 

 

$search_query = "SELECT * FROM riskyjobs";

 

  // Extract the search keywords into an array

 

  $clean_search = str_replace(',', ' ', $user_search);

  $search_words = explode(' ', $clean_search);

  $final_search_words = array();

  if (count($search_words) > 0) {

    foreach ($search_words as $word) {

      if (!empty($word)) {

        $final_search_words[] = $word;

      }

    }

  }

 

 

  // Generate a WHERE clause using all of the search keywords

  $where_list = array();

  if (count($final_search_words) > 0) {

    foreach($final_search_words as $word) {

      $where_list[] = "description LIKE '%$word%'";

    }

  }

 

  $where_clause = implode(' OR ', $where_list);

 

  // Add the keyword WHERE clause to the search query

  if (!empty($where_clause)) {

    $search_query .= " WHERE $where_clause";

  }

 

// End of ADDED the following on 12,30,09

 

 

// Beginning of ADDED the following on 1,1,10

 

  return $search_query; }

 

// End of ADDED the following on 1,1,10

 

 

  $result = mysqli_query($dbc, $search_query);

 

 

while ($row = mysqli_fetch_array($result)) {

 

  echo '<tr class="results">';

  echo '<td valign="top" width="20%">' . $row['title'] . '</td>';

 

  // changed $row['description'] . ' in the next line to substr($row['description'],0,100) . ' ...

  // on 12,31,09

  echo '<td valign="top" width="50%">' . substr($row['description'],0,100) . ' ...</td>';

 

 

  echo '<td valign="top" width="10%">' . $row['state'] . '</td>';

 

  // changed $row['date_posted'] in the next line to substr($row['date_posted'],0,10)

  // on 12,31,09

  echo '<td valign="top" width="20%">' . substr($row['date_posted'],0,10) . '</td>';

 

  echo '</tr>';

}

echo '</table>';

 

mysqli_close($dbc);

 

?>

 

 

 

</body>

 

</html>

 

 

I would appreciate your help!!!

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/186992-php-and-mysql-help/
Share on other sites

I cannot see anywhere in your code where the function build_query is being called..

You need to do

$search_query = build_query($user_search); 

Just before your mysqli query call, because at present your $seach_query is not define in the main script, only in the function..

Link to comment
https://forums.phpfreaks.com/topic/186992-php-and-mysql-help/#findComment-987472
Share on other sites

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.