Jump to content

Recommended Posts

I am very new to PHP and for a school project i am just trying to have a basic php script to connect to MYSQL database. The search engine i took from a tutorial. I get an error on line 26 of search.php ... something about  " expects parameter 1 to be resource, boolean given in "

 

here is the soucre code for both... (took out a few things)

 

 

<form method="get" action="search.php">
<label>Search For: </label><input type="text" name="query" />
<input type="submit" value="Start Search" />
<input type="reset" value="Reset"
</form>

<?php
$query=$_GET['query'];

// Change the fields below as per the requirements
$db_host="
$db_username=
$db_password=
$db_name="beer";
$db_tb_name="beer";
$db_tb_atr_name="attribute(beer) of table in which search action is to be performed";

//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");

$query_for_result=mysql_query("SELECT * FROM $tb_name WHERE $db_tb_atr_name like '%".$query."%'");

while(mysql_fetch_array($query_for_result))
{
    echo "<p>";
    echo $data_fetch['table_attribute'];
    echo "</p>";
}

mysql_close();
?>

the other one is..

<form method="get" action="search.php">
<label>Search For: </label><input type="text" name="query" />
<input type="submit" value="Start Search" />
<input type="reset" value="Reset"
</form>

<?php

// Connects to your Database
mysql_connect("") or
die(mysql_error());
mysql_select_db(") or die(mysql_error());
$query = "SELECT * FROM beer";
if(isset($_GET['orderby'])){
if($_GET['orderby'] == 'name') $query = "SELECT * FROM beer ORDER BY name";
if($_GET['orderby'] == 'country') $query = "SELECT * FROM beer ORDER BY country";
if($_GET['orderby'] == 'rating') $query = "SELECT * FROM beer ORDER BY rating";
if($_GET['orderby'] == 'price') $query = "SELECT * FROM beer ORDER BY price";
}
// Collects data from "beer" table
$data = mysql_query($query) or die(mysql_error());

print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))  
{
echo "<tr>";          
echo "<th>name:</th> <td>".$info['name'] . "</td> ";
echo "<th>rating:</th> <td>".$info['rating'] . " </td>";
echo "<th>price:</th> <td>".$info['price'] . " </td>";
echo "<th>country:<th><td>".$info['country'] . "</td></tr>";

}
echo "</table>";

?>
Sort by:
<th><a href=" <?php echo $_SERVER['PHP_SELF'] ?>/?orderby=name">name</a></th> 
<th><a href=" <?php echo $_SERVER['PHP_SELF'] ?>/?orderby=country">country</a></th>
<th><a href=" <?php echo $_SERVER['PHP_SELF'] ?>/?orderby=price">price</a></th>
<th><a href=" <?php echo $_SERVER['PHP_SELF'] ?>/?orderby=rating">rating</a></th>


Link to comment
https://forums.phpfreaks.com/topic/232206-new-need-help/
Share on other sites

That kind of error usually means your query is failing for some reason.

 

If the query fails it causes mysql_query to result false (which is a bool) so when you pass this to the fetch function it causes that error.

 

If you add

 

echo mysql_error();

 

after the failing query it should tell you what the problem is.

Link to comment
https://forums.phpfreaks.com/topic/232206-new-need-help/#findComment-1194494
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.