Jump to content

[SOLVED] Name Could be in any field


WebGuyRI

Recommended Posts

Newbee making progress but really stuck on this. Looked all over and could not get anything to work.

 

The database contains a list of names and a little info, each name could be in any one of 6 fields. Looking to have the user type one name in the query and it would give them the full line of info no mater which field the name is in.

 

Now when the person types the search name it only returns the line that has that name in the first place.  Have tried a number of combinations using name1 name2 etc but can not get it working.

 

$yourfield = "name";

$name = "name";

 

if ($_POST['name'] == "" ) {

    echo 'There was an error in the data you submitted. Please go back and fill in all the required fields.';

    die();

}

 

mysql_connect($hostname, $username, $password) or DIE("Unable to connect to MySQL server $hostname");

"  Connected to MySQL server<br>";

$selected = mysql_select_db($dbname) or DIE("Could not select requested db $dbname");

 

print "Connected to database $dbname<br>";

 

$name = mysql_real_escape_string($_POST['name']);

 

$query = "SELECT * FROM $usertable WHERE name='$name'";

 

$result = mysql_query($query) or DIE("Could not Execute Query on table $usertable");

if ($result) {

 

    print "Query successful on table $usertable<br><br>";

    print "Your Query returned:<br>";

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

      print $row['name'].", ".$row['name2'].", ".$row['name3'].", ".$row['name4'].", ".$row['name5'].",".$row['name6'].", ".$row['Misc'].", ".$row['volumn'].", ".$row['city'].", ".$row['state']."<br>";

        }

 

    }

 

mysql_close;

?>

 

Any help would be appreciated

 

Thanks

Link to comment
Share on other sites

There's no easy way to do that...you need to search each field. For example:

 

SELECT * FROM yourtable WHERE field1='somevalue' OR field2 = 'somevalue' ...

 

But, why do you have 6 fields storing names in them? Chances are, there's a better way to store you data.

Link to comment
Share on other sites

Thanks for the reply will give it a try.

 

As for why I have the names all those fields. This is a database of Cemetery headstones and the names are on the headstones. Which is why they are in different places on each line. The persons who created the database put it the way they found it on the stones. Looking at about 55,000 lines of info.

 

Will let yo know after I try..thanks

Link to comment
Share on other sites

Sorry for the delay but tried so many versions did not remember what actually happened.

 

I used this code

$name = mysql_real_escape_string($_POST['name']);

 

$query = "SELECT * FROM $usertable WHERE name='$name'"; (this works for the first field)

 

Tried this

$query = "SELECT * FROM $usertable WHERE name='$name' OR name2='$name'";  which gave me a blank screen

 

$result = mysql_query($query) or DIE("Could not Execute Query on table $usertable");

if ($result) {

 

    print "Query successful on table $usertable<br><br>";

    print "Your Query returned:<br>";

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

      print $row['name'].", ".$row['name2'].", ".$row['name3'].", ".$row['name4'].", ".$row['name5'].",".$row['name6'].", ".$row['name7'].", ".$row['name8'].", ".$row['name9'].", ".$row['name10']."<br>";

        }

 

Very sorry but not really up on PHP Mysql but like it. Thanks for your patience.

Link to comment
Share on other sites

$query = "SELECT CONCAT('',name,name2,name3,name4,name5,name6) as n FROM $usertable HAVING n LIKE '%$name%'";

 

Tried it and got this

Your Query returned:

, , , , ,, , , ,

, , , , ,, , , ,

, , , , ,, , , ,

, , , , ,, , , ,

, , , , ,, , , ,

 

Thanks for jumping in..

 

Would it help if I post the link to see the results and is this allowed?

Link to comment
Share on other sites

okay the reason why you are getting a bunch of commas is because in you didn't change the part of your code where you are outputting the results.  Instead of this:

 

       print $row['name'].", ".$row['name2'].", ".$row['name3'].", ".$row['name4'].", ".$row['name5'].",".$row['name6'].", ".$row['name7'].", ".$row['name8'].", ".$row['name9'].", ".$row['name10']."<br>";

 

do this:

 

  print $row['n'] . "<br/>";

 

Yes, you can post links to your code, but don't ask to have them removed later, because we won't.

Link to comment
Share on other sites

Thank you very much that print code worked great.

One small issue with that code is will I be able to add text in between the results? with the old code for one field I was putting text like Volume Number . Death etc.

 

Also if I can not add text this at lease is a major step forward.

 

Thanks again this is a great site for help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.