Welcome to Dream.In.Code
Getting Help is Easy!

Join 118,887 Programmers for FREE! Ask your question and get quick answers from experts. There are 2,054 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Having problems searching my table (mySQL)

 
Reply to this topicStart new topic

Having problems searching my table (mySQL), how come I can not search info from the table in my database?

firers_3
post 21 Jun, 2008 - 11:20 AM
Post #1


New D.I.C Head

*
Joined: 21 Jun, 2008
Posts: 2

I am recently building a website using database. I purchased domain from hostmonster (Apache based server) and set up a database and a TABLE through its control panel . I am sure my php code can access the database but I just can not use the code to search and display results from the TABLE content in the database. Anyone can help see whether the php code is wrong or possibly my table is not invalid. Thanks.

Here is the code for result.php

CODE

<?
  $hostname = "localhost"; // Our DB server.
  $username = "xxx"; // The username you created for this database.
  $password = "xxx"; // The password you created for the username.
  $usertable = "cust"; // The name of the table you made.
  $dbName = "xxx"; // This is the name of the database you made.
  
  MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
  @mysql_select_db( "$dbName") or die( "Unable to select database");
  ?>
  
  <?
  
  //error message (not found message)begains
  $XX = "No Record Found, to search again please close this window";
  //query  table begins
  $query = mysql_query("SELECT * FROM cust WHERE [color=#FF0000]the table[/color] LIKE '%$search%' LIMIT 0, 30");
  while ($row = @mysql_fetch_array($query))

$variable1=$row["id"];
$variable2=$row["name"];
$variable3=$row["telephone"];

//table layout for results

print ("<tr>");
print ("<td>$variable1</td>");
print ("<td>$variable2</td>");
print ("<td>$variable3</td>");
print ("</tr>");

  
  //below this is the function for no record!!
  
  //end
  
  ?>


Here is the code for search form:

CODE


<form method="post" action="http://www.domainname/result.php" target="_blank">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td bordercolor="#000000">
<p align="center">
<select name="name" size="1">
<option value="name">Name</option>
<option value="telephone">Telephone</option>
<option value="birthday">Birthday</option>
</select> <input type="text" name="search" size="25">  <br>
Search database: <input type="submit" value="Go!!" name="Go"></p>
</td>
</tr>
</table>
</div>
</form>



Many thanks icon_up.gif icon_up.gif
User is offlineProfile CardPM

Go to the top of the page


pertheusual
post 22 Jun, 2008 - 10:52 AM
Post #2


D.I.C Head

**
Joined: 26 Jan, 2008
Posts: 230



Thanked 2 times
My Contributions


php

<?
$hostname = "localhost"; // Our DB server.
$username = "xxx"; // The username you created for this database.
$password = "xxx"; // The password you created for the username.
$usertable = "cust"; // The name of the table you made.
$dbName = "xxx"; // This is the name of the database you made.

//lower case. this might work, but it's super ugly
MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");

//Don't need the quotes around $dbName, though it doesn't actually matter
@mysql_select_db( "$dbName") or die( "Unable to select database");
?>

<?

//Why put the error message in a string instead of just printing it properly?
//error message (not found message)begains
$XX = "No Record Found, to search again please close this window";
//query table begins

//A few errors here. You use $search, but that's not a variable, the search variable from your form is $_POST['search']
//also, add "or die(mysql_error());" That way it will tell you if the query fails.
//also, what is "the table"? There should be an actual column id in there. I'm guessing $_POST['name']
$query = mysql_query("SELECT * FROM cust WHERE the table LIKE '%$search%' LIMIT 0, 30") or die(mysql_error());


//You forgot to put curly brackets around the while loop, so even if you did get results, it wouldn't work
//Also, since you use $row['id'] and such, you need to use mysql_fetch_assoc
while ($row = @mysql_fetch_assoc($query)){

$variable1=$row["id"];
$variable2=$row["name"];
$variable3=$row["telephone"];

//table layout for results


//no real reason to bother assigning the $variable variables. you can just use $row['id'] and such right in the print statements.
print ("<tr>");
print ("<td>$variable1</td>");
print ("<td>$variable2</td>");
print ("<td>$variable3</td>");
print ("</tr>");
}

//below this is the function for no record!!

//end

?>


I've made comments in your code about a bunch of things. I just want to comment that if you are just starting with PHP and SQL, make sure you read up on mysql injections and such. I have no idea what you plan on doing in the future, but figured I'd mention it.

Good luck,

Per
User is offlineProfile CardPM

Go to the top of the page

firers_3
post 4 Jul, 2008 - 08:49 AM
Post #3


New D.I.C Head

*
Joined: 21 Jun, 2008
Posts: 2

Thanks alot. it helps biggrin.gif biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 03:12AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month