# How can I search something from a database?

**URL:** <https://forum.wixstudio.com/t/how-can-i-search-something-from-a-database/28917>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [November 4, 2020, 6:27am UTC](https://forum.wixstudio.com/t/how-can-i-search-something-from-a-database/28917 "2020-11-04T06:27:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![albertocaballerodiez](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/albertocaballerodiez/32/5569_2.png) [@albertocaballerodiez](https://forum.wixstudio.com/u/albertocaballerodiez)\
**Post date:** [November 4, 2020, 6:27am UTC](https://forum.wixstudio.com/t/how-can-i-search-something-from-a-database/28917/1 "2020-11-04T06:27:19Z")

</div>

This is driving crazy. I’m just trying to create a box where I type a text, I click on a button, and a related text from a database shows up. It’s been impossible after so many hours.

Can anybody please help?

My database is in Sandbox live and has 2 columns with thousands of raws. I would like the user to just type any text of column 1, press a button, and the corresponding text of column 2 shows up.

The name of my dataset is ‘dataset’ , the name of column 1 ‘planet’ and the name of column 2 is ‘esi’

---

<div class="post-metadata">

**Author:** ![ajithkrr](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/ajithkrr/32/20520_2.png) [@ajithkrr](https://forum.wixstudio.com/u/ajithkrr)\
**Post date:** [November 4, 2020, 8:49am UTC](https://forum.wixstudio.com/t/how-can-i-search-something-from-a-database/28917/2 "2020-11-04T08:49:14Z")

</div>

You should your **database** instead of dataset…

#input1 - input where user types  
#button1 - the button to be pressed  
#text1 - the text where the result should be shown…

```javascript
$w.onReady(function () {
 $w('#button1').onClick((event) => {
  wixData.query("DatabaseId") //Database
  .contains("planet", $w('#input1').value)
  .find()
  .then((res) => {
    if(res.items.length > 0) {
      let items = res.items;
      let field2 = items[0].esi;
      $w('#text1').text = field2.toString();

    } else {
      // handle case where no matching items found
    }
   });
  });
 });

```

For more reference → wixData.query()

---

<div class="post-metadata">

**Author:** ![albertocaballerodiez](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/albertocaballerodiez/32/5569_2.png) [@albertocaballerodiez](https://forum.wixstudio.com/u/albertocaballerodiez)\
**Post date:** [November 4, 2020, 2:22pm UTC](https://forum.wixstudio.com/t/how-can-i-search-something-from-a-database/28917/3 "2020-11-04T14:22:03Z")

</div>

Thank you,  
Where should I put that code?  
And why ‘onClick (( event )’? my database is a table in the content manager.

I created an input box and linked it to ‘input’ but i cannot even write in that box.

When I create the button, the option ‘search data’ cannot be pressed. The button doesn’t work.

And when I create a small dinamic window where the text1 is supposed to appear, nothing appear.

---

<div class="post-metadata">

**Author:** ![albertocaballerodiez](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/albertocaballerodiez/32/5569_2.png) [@albertocaballerodiez](https://forum.wixstudio.com/u/albertocaballerodiez)\
**Post date:** [November 4, 2020, 4:20pm UTC](https://forum.wixstudio.com/t/how-can-i-search-something-from-a-database/28917/4 "2020-11-04T16:20:23Z")

</div>

Also, I get this error The dataset1 collection does not exist. You cannot work with a collection using the Data API before it is created in the Editor.

---

<div class="post-metadata">

**Author:** ![ajithkrr](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/ajithkrr/32/20520_2.png) [@ajithkrr](https://forum.wixstudio.com/u/ajithkrr)\
**Post date:** [November 4, 2020, 4:43pm UTC](https://forum.wixstudio.com/t/how-can-i-search-something-from-a-database/28917/5 "2020-11-04T16:43:39Z")

</div>

For this I already said - "You should use your **database’s id** not dataset’s

---

<div class="post-metadata">

**Author:** ![albertocaballerodiez](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/albertocaballerodiez/32/5569_2.png) [@albertocaballerodiez](https://forum.wixstudio.com/u/albertocaballerodiez)\
**Post date:** [November 4, 2020, 5:03pm UTC](https://forum.wixstudio.com/t/how-can-i-search-something-from-a-database/28917/6 "2020-11-04T17:03:02Z")

</div>

Thank you.

The problem is that when I connect input1 to the database, I cannot enter any input in the box later.

---

<div class="post-metadata">

**Author:** ![ajithkrr](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/ajithkrr/32/20520_2.png) [@ajithkrr](https://forum.wixstudio.com/u/ajithkrr)\
**Post date:** [November 5, 2020, 3:59am UTC](https://forum.wixstudio.com/t/how-can-i-search-something-from-a-database/28917/7 "2020-11-05T03:59:04Z")

</div>

For the code to work, you should **not** connect any of the elements to the dataset…

The code →

```javascript
$w.onReady(function () {
 $w('#button1').onClick((event) => { //this is the button to be clicked for searching- 
                                         //the database
 
   wixData.query("DatabaseId") //Database
  .contains("planet", $w('#input1').value)   
  .find()
  .then((res) => {
    if(res.items.length > 0) {
      let items = res.items;
      let field2 = items[0].esi;
      $w('#text1').text = field2.toString(); // #text1 is the text where the result 
                                                  //is shown

    } else {
      $w('#text1').text = "No matches"; //if there is no matching result
    }
   });
  });
 });

```
