Jump to content

MySQL Search Script


Birch

Recommended Posts

Hello PHP Gurus! (:D)

 

I've made a db that stores information about issues of a comics magazine. After having a hard time writing and fixing the select and view script *phew* I started writing the search script. Each issue has 5 parameters, ID (number of the issue, auto-increasing and primary key), cover story, pages, date and price. So, I wanted 2 ways of searching, one by using the ID, and one by using cover story or date. However, I get php errors everytime I change something to fix the previous error, so I cannot remember which error started it all  :-[. Please tell me what's wrong. I am using WAMP for the testing.

 

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

<title>KomixDB</title>
</head>
<body>
  <h1>KomixDB Search Results</h1>
<?php
  // create short variable names
  $searchtype=$_POST['searchtype'];
  $searchterm=$_POST['searchterm'];

  $searchterm= trim($searchterm);

  if (!$searchtype || !$searchterm)
  {
     echo 'You have not entered search details.  Please go back and try again.';
     exit;
  }
  
  if (!get_magic_quotes_gpc())
  {
    $searchtype = addslashes($searchtype);
    $searchterm = addslashes($searchterm);
  }


  @ $db = new mysqli("localhost", "root", "password", "komixdb") or die(mysql_error());;
  if (mysqli_connect_errno()) 
  {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }

if ($searchtype == 'id')
{	mysqli_query($db, "set names utf8");
  $query = "select * from issues where id == '%".$searchterm."%'";
  $result = $db->query($query);
  


$row =  mysqli_query($result, MYSQLI_NUM)
  or die("Error: ".mysqli_error($result));
     echo '<p><strong> Ιστορία Eξώφυλλου: ';
     echo htmlspecialchars(stripslashes($row['coverstory']));
     echo '</strong><br />Νούμερο: ';
     echo stripslashes($row['id']);
     echo '<br />Ημερομηνία: ';
     echo stripslashes($row['issuedate']);
     echo '<br />Σελίδες: ';
     echo stripslashes($row['pages']);
     echo '<br />Tιμή: ';
     echo stripslashes($row['price']);
     echo '</p>';

  $result->free();
  $db->close();
}
  else
{
  mysqli_query($db, "set names utf8");
  $query = "select * from issues where ".$searchtype." like '%".$searchterm."%'";
  $searchResult = $db->query($query);

  $num_results = $searchResult->num_rows;

         while ($row = mysqli_fetch_assoc($searchResult)) {
echo '<p>Number of issues found: '.$num_results.'</p>';

  for ($i=0; $i <$num_results; $i++)
  {
$row = mysqli_fetch_assoc($searchResult);
     echo '<p><strong>'.($i+1).'. Ιστορία Eξώφυλλου: ';
     echo htmlspecialchars(stripslashes($row['coverstory']));
     echo '</strong><br />Νούμερο: ';
     echo stripslashes($row['id']);
     echo '<br />Ημερομηνία: ';
     echo stripslashes($row['issuedate']);
     echo '<br />Σελίδες: ';
     echo stripslashes($row['pages']);
     echo '<br />Tιμή: ';
     echo stripslashes($row['price']);
     echo '</p>';
         }
      } 


  
  $searchResult->free();
  $db->close(); }
?>
</body>
</html>

Link to comment
Share on other sites

ok, that reduced the error levels when searching for ID but there are still 2 msgs

 

Warning: mysqli_query() expects parameter 1 to be mysqli, object given in C:\wamp\www\results.php on line 47

Warning: mysqli_error() expects parameter 1 to be mysqli, object given in C:\wamp\www\results.php on line 48
Error: 

 

And forgot to post the problem for cover story and date searching:

 

KomixDB Search Results

 

Number of issues found: 1

 

1. Ιστορία Eξώφυλλου:    (coverstory was supposed to be there)

Νούμερο: ID

Ημερομηνία: Date

Σελίδες:  Pages

Tιμή:  Price

 

That's all I get, when the row that matches is only one. When there are more, only the last result is empty

Link to comment
Share on other sites

I don't read Greek and I have never used mysqli myself but the problem is that you send the $result object and not the mysqli to the mysqli_query and mysqli_error functions. :P They only accept an instance of mysqli.

 

Since you have already executed the query you do not wish to execute another one but operate on the result class instead as you do in the other places of your code. There are plenty of ways you can do this. Look here.

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.