Results 1 to 11 of 11

Thread: Searching a database

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    5

    Searching a database

    This code puts out what I search for fine:



    $result = mysql_query("SELECT * FROM mytable WHERE id = $variable");

    while($row = mysql_fetch_array($result)){

    echo $row["id"];
    echo "<br>";
    echo $row["name"];
    echo "<br>";
    }


    What I can't figure out is if there is no id $variable in the table, how do I get it to come back and:

    echo "nothing found";

    I'd guess its some sort of 'if' statement, but I don't know exactly how to construct it in this case.

  2. #2
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    PHP Code:

    if ($variable!=NULL) {

    $result mysql_query("SELECT * FROM mytable WHERE id = $variable"); 

    while(
    $row mysql_fetch_array($result)){ 

    echo 
    $row["id"]; 
    echo 
    "<br>"
    echo 
    $row["name"]; 
    echo 
    "<br>"

    }
    Else {

    echo (
    "nothing found");



  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    First off, this isn't searching, this is just pulling out records with matching IDs. Second, there shouldn't be matching IDs.

    You would probably also want to do it this way to find out whether or not any records are found. What was posted above will just tell you if there was no variable:

    PHP Code:
    if ($variable!=NULL) {

      
    $result mysql_query("SELECT * FROM mytable WHERE id = $variable"); 

      [
    b]if ($row mysql_fetch_array($result)) {[/b]
        do {
          echo 
    $row["id"];
          echo 
    "<br>"
          echo 
    $row["name"]; 
          echo 
    "<br>"
        } while(
    $row mysql_fetch_array($result));
      [
    b]} else {[/b]
        echo 
    "No records found!";
      }
    } else {
      echo 
    "No search variable (id?) defined!";

    Last edited by The Hobo; Jul 6th, 2002 at 09:43 AM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    or you could do

    PHP Code:
    $result mysql_query("SELECT * FROM mytable WHERE id = $variable"); 

    if(!
    $result){
        echo 
    'Not found<br>';
    } else{
        while(
    $row mysql_fetch_array($result)){ 
            echo 
    $row['id'] . "<br>" $row['name'] . "<br>";
        }

    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I'm still thinking this isn't searching...

    and also, why can't we use [b] tags in the [php] tag anymore?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    Originally posted by The Hobo
    [B]I'm still thinking this isn't searching...

    and also, why can't we use tags in the [php] tag anymore?
    Yah that isnt searching, but o well.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    5
    Thanks to everyone who replied. As to weather this is considered searching or not, I wouldn't know. I know very little about any of this. The id isn't actually what I'm looking for. That's just what I put in the example. What I am actually doing is taking the id from one table and checking to see if there are any instances of it in a related field in a couple of other tables. At any rate it seems to work for my purposes. Anyone who wants to take the time to show me a better way to go about this would be appreciated.

    Thanks
    ED

  8. #8
    ricmitch_uk
    Guest
    Originally posted by The Hobo
    [B]I'm still thinking this isn't searching...

    and also, why can't we use tags in the [php] tag anymore?
    Do you have enhanced mode on? see the left on your post reply page.

  9. #9
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by ricmitch_uk

    Do you have enhanced mode on? see the left on your post reply page.
    No, but I can't see why that would have anything to do with it?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    PHP Code:
    if ($variable!=NULL) {

      
    $result mysql_query("SELECT * FROM mytable WHERE id = $variable"); 

      [
    b]if ($row mysql_fetch_array($result)) {[/b]
        do {
          echo 
    $row["id"];
          echo 
    "<br>"
          echo 
    $row["name"]; 
          echo 
    "<br>"
        } while(
    $row mysql_fetch_array($result));
      [
    b]} else {[/b]
        echo 
    "No records found!";
      }
    } else {
      echo 
    "No search variable (id?) defined!";

    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Still doesn't work with "Enhanced Mode."

    I swear we used to be able to do it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width