PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 5th, 2005, 11:24 AM
jumpstartpro jumpstartpro is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 2 jumpstartpro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Custom WHERE and LIKE tags

I am trying to create the search feature where the user can type in a number into a text box. It will then search the database row for that number, then list certain information for the resort. This is for a vacation website, where the number is the Ad number and the search results page will display information like location, resort name, etc.

I have been working with a few books I purchased which has helped me get the database created and am able to enter information. However here is the example from one of my books, which I cannot get to work or even display anything from the database.

Search Page

<form action="results.php" method="post">
Choose Search Type:<br />
<select name="searchtype">
<option value="ad_number">Ad Number</option>
<option value="resort_location">State</option>
<option value="bed">Bedrooms</option>

</select>
<br />
Enter Search Term:<br />
<input name="searchterm" type="text">
<br />
<input type="submit" value="Search">
</form>


Results.php

<?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', 'user', 'password', 'dbname');

if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

$query = "select ad_number, resort_location, resort_name, bed, bath, sales_price from ads where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);

$num_results = $result->num_rows;

echo '<p>Number of Resorts found: '.$num_results.'</p>';

for ($i=0; $i <$num_results; $i++)
{
$row = $result->fetch_assoc();
echo '<p><strong>'.($i+1).'. Ad Number: ';
echo htmlspecialchars(stripslashes($row['ad_number']));
echo '</strong><br />Resort Location: ';
echo stripslashes($row['resort_location']);
echo '<br />Resort Name: ';
echo stripslashes($row['resort_name']);
echo '<br />Price: ';
echo stripslashes($row['sales_price']);
echo '</p>';
}

$result->free();
$db->close();

?>

As a test I even put in the wrong database login information, and I do not even get any error message. I would think that the examples from a book would at least work.

Reply With Quote
  #2  
Old January 5th, 2005, 03:11 PM
Faylaricia Faylaricia is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Houston, TX
Posts: 12 Faylaricia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Cool

Ah, another bookworm ;-)

This may sound stupid, but where are the HTML tags? I would think you require those simply because you have HTML tags like the form tag and all.

Reply With Quote
  #3  
Old January 5th, 2005, 03:57 PM
jumpstartpro jumpstartpro is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 2 jumpstartpro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Faylaricia
Ah, another bookworm ;-)

This may sound stupid, but where are the HTML tags? I would think you require those simply because you have HTML tags like the form tag and all.


Well when it comes to learning a new proraming language, I prefer a book or two. Anyhow, the HTML tags are done, and I figured they were not needed to be shown here because they work and would take up to much of the article. It was the php that was giving me the problem, not the html.

Reply With Quote
  #4  
Old January 6th, 2005, 09:06 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
In the future, or even now (edit your post), use surround your code and/or PHP markup with [code] and/or [php]... it will colourize the php code, and both will section the code off with scroll bars so it doesn't take up too much forum real estate =)

Also, I'd advise against using actual column names in your <select> box (as it appears to me that you've done)... many would consider this a security flaw...

Are you using Pear's DB class? or something odd? I ask because some of your code deviates from what I usually see from new members =)

This line here seems odd: @ $db = new mysqli('localhost', 'user', 'password', 'dbname');

perhaps add or die('Error connecting to database :: '.mysql_error()); to the end of that line...
that mysql_error() might cause an error itself... look into the DB class that you're using...

Reply With Quote
  #5  
Old January 6th, 2005, 01:26 PM
Faylaricia Faylaricia is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Houston, TX
Posts: 12 Faylaricia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by jumpstartpro
Well when it comes to learning a new proraming language, I prefer a book or two. Anyhow, the HTML tags are done, and I figured they were not needed to be shown here because they work and would take up to much of the article. It was the php that was giving me the problem, not the html.

I didn't mean it like that, I am just glad I am not the only one. :-)

Reply With Quote
  #6  
Old February 5th, 2005, 09:02 AM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
Quote:
Originally Posted by MadCowDzz
This line here seems odd: @ $db = new mysqli('localhost', 'user', 'password', 'dbname');

perhaps add or die('Error connecting to database :: '.mysql_error()); to the end of that line...
that mysql_error() might cause an error itself... look into the DB class that you're using...


he's using php5 which I am also having a problem with. I am updating all of my scripts to the php5 and I am also having a problem with the code. I know it connects to the database but for some reason it doesn't like the query with the select statement using the where clause. The query will not return any results. The query will return results if the where clause isn't there but that would be pointless. So if anyone else has ran into the same problem but figured out the solution please let us know.

Thanks

Reply With Quote
  #7  
Old February 5th, 2005, 12:47 PM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
PHP Code:
 $query "select ad_number, resort_location, resort_name, bed, bath, sales_price from ads where ".$searchtype." like '%".$searchterm."%'";
$result $db->query($query); 


try this code instead and see if it works for you

PHP Code:
 $result $db->query("SELECT ad_number, resort_location, resort_name, bed, bath, sales_price FROM ads WHERE '$searchtype' like '%$searchterm%'"); 


just copy and paste that over the code in the code block and try again.. let me know what happens

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Custom WHERE and LIKE tags


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT