SunQuest
           Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old September 5th, 2003, 05:25 PM
pjwraith pjwraith is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: uk
Posts: 13 pjwraith User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Where did I go wrong? Simple stuff, but problems

I have just started PHP this week after preparing a MYSQL database on a hosting site, forgive me but its all new to me.

The basic premise is a simple database, 8 fields, membership no as a primary field and town/postcode fields are indexed.


I have created a form called input1.PHP which allows a user to enter a word for searching in either the town or postcode fields. The code is detailed below and seems to work ok.

php Code:
Original - php Code
  1. <?php
  2.  
  3. echo '
  4. <html><body>
  5. <form method="post" action="search.php3">
  6. Enter the first part of your postcode, <strong>OR</strong> your town/city
  7. <input type="text" name="seek"></input>
  8. <input type="submit" name="submit" value="Submit for search"></input>
  9. </form>
  10. </body></html>
  11. ';
  12. ?> 


Thsi should pass a variable, $seek, to a further page search.php for a query and results listing.Originally the page was as below, it is missing some code to make it display the results I think and it was suggested I change to the second lot of code below.

php Code:
Original - php Code
  1. <?
  2. mysql_connect ("localhost", "ppreen_webuser", "web") or die ('I cannot connect to the database because: ' . mysql_error());
  3. mysql_select_db ("ppreen_therpist")
  4. or die ("Could not select database");
  5.  
  6.  
  7. // PHP Search Script
  8.  
  9. $sql = mysql_query("select * from aromaol where town = '".$seek."' or postcode = '". $seek ."'")  or die (mysql_error());
  10.  
  11. while(list($seek)=mysql_fetch_array($sql)){
  12. }
  13.  
  14. ?> 


now changed to

php Code:
Original - php Code
  1. <?
  2. $conn = mysql_connect ("localhost", "ppreen_webuser", "web") or die ('I cannot connect to the database because: ' . mysql_error());
  3. $selected = mysql_select_db ("ppreen_therpist")
  4. or die ("Could not select database");
  5.  
  6.  
  7. // PHP Search Script
  8.  
  9. $sql = mysql_query("select * from aromaol where town = '".$seek."' or postcode = '". $seek ."'")  or die (mysql_error());
  10. $result = mysql_query($sql,$conn);
  11.  
  12. while ($row = mysql_fetch_assoc($result)){
  13.  
  14. $forename = $row["forename"];
  15. $surname = $row["surname"];
  16. $telno = $row["telno"];
  17. $mobno = $row["mobno"];
  18. $weblink = $row["weblink"];
  19. $town = $row["town"];
  20.  
  21. }
  22.  
  23. ?> 



However I now get the error "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/ppreen/public_html/search.php3 on line 12" and still no results

Can someone tell me where I went wrong?

Many thanks in advance

Reply With Quote
  #2  
Old September 5th, 2003, 05:53 PM
Blindeddie Blindeddie is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: NJ - USA
Posts: 2,152 Blindeddie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
RE: Where did I go wrong? Simple stuff, but problems

Register globals is probably set to off in the php.ini file. This would require you to use the $_POST superglobal. Change $seek to $_POST['seek'] in the query and it should work fine.

Reply With Quote
  #3  
Old September 5th, 2003, 07:26 PM
pjwraith pjwraith is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: uk
Posts: 13 pjwraith User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Where did I go wrong? Simple stuff, but problems

no joy I'm afraid

Reply With Quote
  #4  
Old September 5th, 2003, 07:38 PM
sliver's Avatar
sliver sliver is offline
Moderator
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: WI, USA
Posts: 888 sliver User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 16 m 42 sec
Reputation Power: 2
Send a message via AIM to sliver Send a message via XFire to sliver
RE: Where did I go wrong? Simple stuff, but problems

Change: $result = mysql_query($sql,$conn); to $result = mysql_query($sql,$conn) or die(mysql_error()); and see what happens.

Reply With Quote
  #5  
Old September 5th, 2003, 07:54 PM
Blindeddie Blindeddie is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: NJ - USA
Posts: 2,152 Blindeddie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
RE: Where did I go wrong? Simple stuff, but problems

What version of PHP are you using. also, post the code that you are using after you made the mods I suggested.

Reply With Quote
  #6  
Old September 5th, 2003, 07:59 PM
Blindeddie Blindeddie is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: NJ - USA
Posts: 2,152 Blindeddie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
RE: Where did I go wrong? Simple stuff, but problems

I see a potential problem,

php Code:
Original - php Code
  1.  
  2. <?
  3. $sql = mysql_query("select * from aromaol where town = '".$seek."' or postcode = '". $seek ."'")  or die (mysql_error());
  4. $result = mysql_query($sql,$conn);


should be
php Code:
Original - php Code
  1.  
  2. <?
  3. $sql = "select * from aromaol where town = '".$_POST['seek']."' or postcode = '". $_POST['seek'] ."'";
  4. $result = mysql_query($sql,$conn)or die (mysql_error())


you were using mysql_query improperly in the the first one.



Reply With Quote
  #7  
Old September 5th, 2003, 09:23 PM
pjwraith pjwraith is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: uk
Posts: 13 pjwraith User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Where did I go wrong? Simple stuff, but problems

I have changed the code to read as follows:

php Code:
Original - php Code
  1. <?
  2. $conn = mysql_connect ("localhost", "ppreen_webuser", "web") or die ('I cannot connect to the database because: ' . mysql_error());
  3. $selected = mysql_select_db ("ppreen_therpist")
  4. or die ("Could not select database");
  5.  
  6.  
  7. // PHP Search Script
  8.  
  9. $sql = "select * from aromaol where town = '".$_POST['seek']."' or postcode = '". $_POST['seek'] ."'";
  10. $result = mysql_query($sql,$conn)or die (mysql_error())
  11.  
  12.  
  13. while ($row = mysql_fetch_assoc($result)){
  14.  
  15. $forename = $row["forename"];
  16. $surname = $row["surname"];
  17. $telno = $row["telno"];
  18. $mobno = $row["mobno"];
  19. $weblink = $row["weblink"];
  20. $town = $row["town"];
  21.  
  22. }
  23.  
  24. ?> 


I dont get any errors now, unfortunately I dont get any output at all.

I am using php 4.3.2 and mysql 4.0.14 standard.

the actual page is here at http://www.gsv.tsohost.co.uk/input1.php3

Reply With Quote
  #8  
Old September 5th, 2003, 09:25 PM
pjwraith pjwraith is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: uk
Posts: 13 pjwraith User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Where did I go wrong? Simple stuff, but problems

Grateful for any ideas or alternative sources of simple form and search code ,

thanks for your time, really appreciate it.

Reply With Quote
  #9  
Old September 5th, 2003, 11:22 PM
Blindeddie Blindeddie is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: NJ - USA
Posts: 2,152 Blindeddie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
RE: Where did I go wrong? Simple stuff, but problems

you are not getting any output with the script because you are not echoing anything to the browser. What you need to do is take your variables and output them to the browser.

start by doing something simple like this...

php Code:
Original - php Code
  1.  
  2. <?
  3. if (mysql_num_rows($result)==0){
  4. echo "No Match Found"
  5. }else{
  6. while ($row = mysql_fetch_assoc($result)){
  7. echo "Name: " .$row['forename']." ".$row["surname"]."<br>"
  8. echo "Phone #: ".$row['telno']."<br>";
  9. echo "MOB #: ".$row["mobno"]."<br>";
  10. echo "Link: ".$row["weblink"]."<br>";
  11. echo "Town: ".$row["town"]."<br><br>";
  12. }