Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Connect to database with mysqli

Hi, I'm trying to make a working search bar where you can search for products, but I can't get it to work.
Any ideas on how to do it?

Thanks!

«1

Comments

  • Let me look at your PHP version, code and website.
    Send me the link through my social network. I'll be the "System" guy there, send me it.

    ggclouds.66ghz.com

  • It should be pretty straight forward: write your code and use the right credentials.

    If it doesn't work, it would help a lot if you could explain a bit more about what exactly you're trying to do and where you get stuck.

  • Ok,

    so I've made a normal javascript search bar, which sends you to search.php when you click on the submit button.

    SEARCH.PHP

    <?php require_once 'connect.php'; if(isset($_GET['keywords'])){ echo $keywords = $_GET['keywords']; } ?>

    CONNECT.PHP

    <?php
    $db = new mysqli('oilspec.ml', 'root', 'root', 'sql109.epizy.com');

    Now, something should get displayed from the database (don't know what :smile: ).
    My website URL is www.oilspec.ml.

    Thanks!

  • For starters, the correct order of parameters for the MySQLi contructor is:

    $db = new MySQLi($hostname, $username, $password, $database);
    

    Please make sure you are using the right hostname, username, password and database name. All values except for the password (which is your account password, you can find it in the client area) are listed in the MySQL Databases section in your control panel.

  • Thanks,
    I'm pretty new to mysql :smile: .

    But I do still not get any result.
    Thanks!

  • edited February 5

    Nothing works inside this if inside search.php:

    <?php require_once 'connect.php'; if(isset($_GET['keywords'])){ echo $keywords = $_GET['keywords']; } ?>

  • Did you check the mysqli_error output to see if there were any connection errors?

  • @Admin said:
    Did you check the mysqli_error output to see if there were any connection errors?

    No, where do you see that?

  • @Freeranger said:

    @Admin said:
    Did you check the mysqli_error output to see if there were any connection errors?

    No, where do you see that?

    I haven't checked, but you should! If there was an issue with the connection or the query, you'll get a nice error message telling you exactly what went wrong, which is a great start to fixing it.

  • Thanks,

    But I'm not getting anything that I can see (Due to the fact I can't figure out where the error bat is).

  • I also checked the over all error log, where I can't find anything. :'(

  • @Freeranger said:
    Thanks,

    But I'm not getting anything that I can see (Due to the fact I can't figure out where the error bat is).

    By default, no error is logged. You need to use the mysqli_error function to get the error message.

  • I've checked that, and figured out that the problem is here:

    if(isset($_GET['keywords'])){ echo $keywords = $_GET['keywords']; }

    Can you find anything wrong with this code?
    Thanks!

  • @Freeranger said:
    I've checked that, and figured out that the problem is here:

    if(isset($_GET['keywords'])){ echo $keywords = $_GET['keywords']; }

    Can you find anything wrong with this code?
    Thanks!

    The code is syntactically valid. What is it supposed to do? How are you trying to call this code?

  • edited February 8

    INDEX.PHP

    SEARCH.PHP

    <?php >! require_once 'connect.php'; >! >! if(isset($_GET['keywords'])){ >! echo $keywords = $_GET['keywords']; >! }else echo "DANG NOT WORKING"; >! ?>

    CONNECT.PHP
    <?php $hostname = 'sql109.epizy.com'; $username = 'epiz_21490173'; $password = '(hidden)'; $database = 'epiz_21490173_oil'; $db = new MySQLi($hostname, $username, $password, $database);

  • Sorry for the thing over, here is all of it:

    https://goo.gl/RR2ADH

  • Nice new forum design BTW!

  • OK, it looks like a lot of posts were lost here due to the database corruption. If I remember correctly, the issue is this form:

     <form action="search.php" method="post">
             <input type="text" id="searchbar" placeholder="" value="Search..." maxlength="25" autocomplete="off" onMouseDown="active();" onBlur="inactive();"/><input type="submit" id="searchbtn" value="GO!"/>   
          </form>
    

    The issue is that method="post" should be method="get" and the search bar should have the attribute name="keywords". The receiving PHP code looks fine.

  • I do just have to recreate the code, some parts dissappeared... :'(

    I have one more question too:

    If I want to upload for example a minecraft mod, the file size is over 200mb, is there any way I can still make the downloadable?

    Thanks

  • @Freeranger said:
    I do just have to recreate the code, some parts dissappeared... :'(

    I have one more question too:

    If I want to upload for example a minecraft mod, the file size is over 200mb, is there any way I can still make the downloadable?

    Thanks

    Yes, you can upload it to a specialized file hosting service where they allow larger files and share the link on your website. Sharing large files from a website hosting account is always a bad idea.

  • Thanks!

    Back to the normal subject of the question:

    For some reason, this code gives me a HTTP 500 error. Do anyone know why?

    <?php
      require_once 'connect.php';
      if(isset($_GET['keywords'])){
          $keywords = $db->escape_string($_GET['keywords']);
          $query = $db->query("
             SELECT id, title, description
             FROM search_product
             WHERE id LIKE '%{$keywords}%'
             OR title LIKE '%{$keywords}%'
             OR decription LIKE '%{$keywords}%'
          ");
         ?>
          <div class="result-count">
              Found <?php $query->num_rows;?> results.  
          </div>
    
      }
    

    Thanks!

  • @Freeranger said:
    Thanks!

    Back to the normal subject of the question:

    For some reason, this code gives me a HTTP 500 error. Do anyone know why?

    <?php
    >       require_once 'connect.php';
    >       if(isset($_GET['keywords'])){
    >           $keywords = $db->escape_string($_GET['keywords']);
    >           $query = $db->query("
    >              SELECT id, title, description
    >              FROM search_product
    >              WHERE id LIKE '%{$keywords}%'
    >              OR title LIKE '%{$keywords}%'
    >              OR decription LIKE '%{$keywords}%'
    >           ");
    >          ?>
          
    
    > Found <?php $query->num_rows;?> results. >
       
      }
    

    Thanks!

    Did you check your error logs?

    Regarding your code, I see two things:

    • A variable $db for which I can't see what it is.
    • No error checking whatsoever. No exceptions are being caught and no connection errors are being checked.
  • Thanks, but how can i define $db?

  • Edit: It's defined in connect.php

    $db = new MySQLi($hostname, $username, $password, $database);

  • Regardless of what $db is, a 500 error means your script crashes and your error logs should have more information. If they don't, try to enable PHP display errors (can be found under Alter PHP Config in the control panel) to see if you can figure out the actual error.

  • edited February 12

    Ok, so I get this message:

    Parse error: syntax error, unexpected end of file in /home/vol11_3/epizy.com/epiz_21490173/htdocs/search.php on line 19

    Here's the code:

    <?php
      require_once 'connect.php';
    
      if(isset($_GET['keywords'])){
    
          $keywords = $db->escape_string($_GET['keywords']);
          $query = $db->query("
             SELECT id, title, description
             FROM search_product
             WHERE id LIKE '%{$keywords}%'
             OR title LIKE '%{$keywords}%'
             OR decription LIKE '%{$keywords}%'
          ");
         ?>
          <div class="result-count">
              Found <?php $query->num_rows;?> results.   
          </div>
      }
    

    The last row is number 19. I can't figure out what's wrong!

    Thanks

  • when you have an unexpected end of file in the line
    check all the brackets and put space between the code and end ?>

  • Do you know where it can be? At least, I can't find it...

  • sure ;)
    use this https://phpcodechecker.com/
    or other tool online

  • Says the same :(

This discussion has been closed.