Help - Search - Members - Calendar
Full Version: Basic database to call information
HTMLHelp Forums > Programming > Databases
hausjellp
Hello,

First post here smile.gif

I have built quite a few websites but I just cant work out this:

I would like to have a box where a user enters a number, e.g.. UK2008-10 and clicks search. Once search is pressed set information is then returned and displayed.(from a db which has not yet been built, any ideas what to use?)

Example:

[UK2008-10 ] [Search]

Returned Data:

Name
Address
'Static' Text

Can anyone shed some light on this?

I normally use HTML or CMS's to build websites but Im a quick learner.

Regards,

Piers

ps: sorry if this question if too basic
hausjellp
ok, i have created a database using phpMyAdmin. I have created 1 table called 'userid' with 3 fields. These are called; fldID(primary), fldName and fldAddress.

I have setup a search box. I search from table 'userid' for field 'fldID' to check if its there, if so then it returns the fldID information.

What im stuck on now is how to just search for the fldID but return fldName and fldAddress info too - is this possible?

Example:

[UK2008J ] [SEARCH]

Next page:

User ID: UK2008

-----------------

What i would like to have is:


[UK2008J ] [SEARCH]

Next page:

User ID: UK2008
User Name: Adam Smith
Address: Street, Town, County

-------------------

Would it make more sense to have 3 tables? One for name, one for user id and one for address?
Brian Chandler
Well, you need to write the appropriate MySQL requests. Have you read a basic tutorial about how it works? Show us what you have now, and we may be able to help.
hausjellp
CODE

<?php

  $var = @$_GET['q'];
  $trimmed = trim($var);

$limit=1;

if ($trimmed == "No Data")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

mysql_connect("hidden","hidden","hidden");

mysql_select_db("hidden") or die("Unable to select database");

$query = "select * from userid where fldID like \"%$trimmed%\"  
  order by fldID";

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);


if ($numrows == 3)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";

echo "<p><a href=\"http://www.google.com/search?q="
  . $trimmed . "\" target=\"_blank\" title=\"Look up
  " . $trimmed . " on Google\">Click here</a> to try the
  search on google</p>";
  }

  if (empty($s)) {
  $s=0;
  }

  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

echo "<p>You searched for: "" . $var . ""</p>";

echo "Member Name";


  while ($row= mysql_fetch_array($result)) {
  $title = $row["fldName"];

  echo "$count: $title";
  $count++;
  }

$currPage = (($s/$limit) + 1);

  echo "<br />";

  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
  Prev 10</a>  ";
  }

  $pages=intval($numrows/$limit);


  if ($numrows%$limit) {
  $pages++;
  }

  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  $news=$s+$limit;

  echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }

$a = $s + ($limit);
  if ($a > $numrows) { $a = $numrows; }
  $b = $s + 1;
  echo "<p>Showing results $b to $a of $numrows</p>";

?>

and this html:

CODE

<body>
<form name="form" action="search.php" method="get">
  <input type="text" name="q" />
  <input type="submit" name="Submit" value="Search" />
</form>
</body>
I have tried working with the code above but got all confused. It makes sense but I just cant make sense of it. (not sure if that even makes sense) lol
Brian Chandler
QUOTE
I have tried working with the code above but got all confused. It makes sense but I just cant make sense of it.


What does this mean? What does it mean to "work with" code? Do you understand it, or did you just copy a chunk verbatim from somewhere? Have you read an introduction to SQL / MySQL / databases? Have you found the PHP manual yet? (It's here:
http://jp2.php.net/manual/en/index.php * subject to prejudicial redirection.)


To get you started:

QUOTE
$query = "select * from userid where fldID like \"%$trimmed%\"    order by fldID";


This is an SQL query. Do you understand what the bits are? The * means retrieve all fields - which is what you want.

QUOTE
$numresults=mysql_query($query);


Have you read the explanation of the mysql_query() function in the php manual? It returns a "resource", which mean a sort of tag that just identifies the set of search results. Calling this "numresults" is odd, because it is not the number of results.

QUOTE
$numrows=mysql_num_rows($numresults);


.. but mysql_num_rows() does return the number of results. so that's ok, but...

QUOTE
if ($numrows == 3)


Huh? If there are 3 results, print a message saying there aren't any??? Why?
hausjellp
QUOTE(Brian Chandler @ Apr 29 2008, 08:04 PM) *

QUOTE
I have tried working with the code above but got all confused. It makes sense but I just cant make sense of it.


What does this mean? What does it mean to "work with" code? Do you understand it, or did you just copy a chunk verbatim from somewhere? Have you read an introduction to SQL / MySQL / databases? Have you found the PHP manual yet? (It's here:
http://jp2.php.net/manual/en/index.php * subject to prejudicial redirection.)


To get you started:

QUOTE
$query = "select * from userid where fldID like \"%$trimmed%\"    order by fldID";


This is an SQL query. Do you understand what the bits are? The * means retrieve all fields - which is what you want.

QUOTE
$numresults=mysql_query($query);


Have you read the explanation of the mysql_query() function in the php manual? It returns a "resource", which mean a sort of tag that just identifies the set of search results. Calling this "numresults" is odd, because it is not the number of results.

QUOTE
$numrows=mysql_num_rows($numresults);


.. but mysql_num_rows() does return the number of results. so that's ok, but...

QUOTE
if ($numrows == 3)


Huh? If there are 3 results, print a message saying there aren't any??? Why?



Thank you for your reply. I do understand quite a bit but Im still learning. My method for learning is trial and error. I wrote what I thought made sense but as you have pointed out it doesnt.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2014 Invision Power Services, Inc.