SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    0 Post(s)
    0 Thread(s)

    Search Engine Box not connecting with mysql database

    Hi all and Happy New Year
    Hope you all had a fantastic time

    I have just joined so Iím hoping someone can help this novice
    I was up till 6am this morning trying to get my search engine box to work.

    I have followed a tutorial in putting a php search engine box on a webpage and have it work.
    I created mysql database with 5 fields etc.., all went well
    I made a search page (index.php) and I made the results page (results.php).
    I uploaded both files into my website inside a folder called testengine ( I haven't connected it to my site, the folder just stands alone for testing)

    My problem is I know I've got it wrong for the results page to connect to the database but can't figure it out.

    I have on the results page the following php code:
    PHP Code:
    I am getting the errors in my lines 36 and 37 which means itís the two lines quoted above.
    I have changed localhost to other names including 'localhost8080', 'localhost:8080', '',
    My username is 'root' and my password is blank just the usual.

    I logged into 'localhost/myphpadmin' and made the database.
    I watched the tutorial and made the pages but I also downloaded the files as the tutorial allowed me to look at the code.
    There were a couple of corrections that I had to make but everything seems fine, itís just connecting.

    Below I will add the full PHP from my results page

    and after that the results that occured.
    p.s. the address I've put down as 'domain' as I've hidden my own address I'm not too sure if I would be allowed to show my own domain on forum post

    ## TEST PAGE CODE ## which includes php code is ##

    PHP Code:
    <h1 style="color:#00F" font-size="36px">SearchEngine</h1>
    <form action="" method="get">
     <input type="text" name="input" size="50" value='<?php echo $_GET['input']; ?>' class="search-field" />
     <input type="submit" value="search" class="search-button" />
     <hr />

    $_GET['input']; //Note to self $input in the name of the search field
    $terms explode(" "$input);
    $query "SELECT * FROM search WHERE ";
            foreach (
    $terms as $each){
                if (
    $i == 1)
    $query .= "keywords LIKE '%$each%' ";
    $query .= "OR keywords LIKE '%$each%' ";
    // connecting to our mysql database
    mysql_select_db("databasefirst");  // database name is databasefirst
    $query mysql_query($query);
    $numrows mysql_num_rows($query);
            if (
    $numrows 0){
                while (
    $row mysql_fetch_assoc($query)){
    $id $row['id'];
    $title $row['title'];
    $description $row['description'];
    $keywords $row['keywords'];
    $link $row['link'];
    "<h2><a href='$link'>$title</a></h2>
    $description<br /><br />";

    "No results found for \"<b>$input</b>\"";
    // disconnect

    Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'http' (1) in /home/545/connery/ on line 33

    Warning: mysql_select_db() [function.mysql-select-db]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/545/connery/ on line 34

    Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/545/connery/ on line 34

    Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/545/connery/ on line 36

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/545/connery/ on line 36

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/545/connery/ on line 37
    No results found for "contact"
    Warning: mysql_close(): no MySQL-Link resource supplied in /home/545/connery/ on line 56

    Can anyone help, I am a novice but slowly getting there, I think !!!



  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    France, deep rural.
    17 Post(s)
    1 Thread(s)
    My username is 'root' and my password is blank just the usual.
    This is normal for a DIY download and install of Mysql on your own development machine (though you are warned not to use root, and to set yourself a password).

    However it is not normal for a hosted web server with a Mysql server running on it -- it would be far too easy to break in and damage the data.

    In the first instance check the admin panel supplied by your hosting supplier, and your welcome emails, else, contact your hosting company and ask what your Mysql credentials are and how best to reference the localhost (domain, localhost, port 3306 etc) in your scripts.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Surrey, UK
    0 Post(s)
    0 Thread(s)
    In most cases the host should be 'localhost' so try this:

    PHP Code:
    and show us the results. I would guess that it's a problem with the username or password but if you get the host right the error should explain all.

    By the way, the reason you're getting the error Unknown MySQL server host 'http' is that anything after the : is taken as being the port. But as long as you're connecting to a database on the same server you shouldn't need to specify an address other than localhost anyway.

  4. #4
    Community Advisor silver trophy

    Join Date
    Nov 2006
    40 Post(s)
    1 Thread(s)
    Probably just as well it isn't connecting, I can see from the errors what your domain is, and from your code you have no protection against mysql injection, it would be trivial for somebody to delete your entire database. Please read up on mysql injection and sanitising user input before you go live with your sites.


Posting Permissions

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