1

i am having difficulty with connecting my database to mysql, i have tried many different ways of trying to get it to connect to my database, but none these methods seem to work. I am only new to php and i could be missing something, but i dont know what it is. This is for a search engine, i have the form created. I would think that my problem is coming from this line of code, mysql_connect("localhost", "root", "") or die("could not connect");?! I was wondering if i could be told what is the problem and how to fix it, thank you so much. here is my code below.

<?php
//connect


mysql_connect("localhost", "root", "") or die("could not connect");
mysql_select_db("search") or die("could not find database");

$output = '';

if (isset($_POST['search'])) {
    $searchq = $_POST['search'];
    $searchq = preg_replace("#[^0-9a-z]#i","", $searchq);


   $query = mysql_query("SELECT * FROM  gp management system WHERE Title LIKE '%$searchq%' OR Description LIKE '%$searchq%' OR Keyword LIKE '%$searchq%'") or die("could not search");
   $count = mysql_num_rows($query);
   if($count == 0) {
       $output = 'There was no search results!';
   }
   else{
        while($row = mysql_fetch_array($query)) {
            $Title = $row['Title'];
            $Description = $row['Description'];
            $Keyword = $row['Keyword'];

            $output .= '<div> ' .$Keyword. ' </div> ';
        }
   }
}

?>

6
  • for one thing, this is an issue FROM gp management system and a major one at that. Commented Apr 20, 2015 at 14:53
  • Are you sure you have a database named search ? Commented Apr 20, 2015 at 15:04
  • Hello, it was my database name having space's and not in single quotes. Thank you so much for your feed back it was great. Commented Apr 20, 2015 at 15:06
  • 1
    WARNING: If you're just learning PHP, please, do not learn the obsolete mysql_query interface. It's awful and is being removed in future versions of PHP. A modern replacement like PDO is not hard to learn. A guide like PHP The Right Way can help explain best practices. Always be absolutely sure your user parameters are properly escaped or you will have severe SQL injection bugs. Commented Apr 20, 2015 at 15:06
  • that isn't the database that contains spaces but your table(s). Commented Apr 20, 2015 at 15:12

4 Answers 4

2

SQL errors:

SELECT * FROM  gp management system WHERE Title...
               ^^-- table name
                  ^^^^^^^^^^ aliasing 'gp' as 'management'
                             ^^^^^^-- extra unknown garbage

Table names shouldn't have spaces to begin with, but if you insist on having them, then you need proper quoting:

SELECT * FROM `gp management system` etc...
              ^--------------------^

Never output a fixed/useless "could not search" error. Have the DB tell you what went wrong:

$result = mysql_query($sql) or die(mysql_error());
                           ^^^^^^^^^^^^^^^^^^^^^^^
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for this feedback, im only new and i am doing stupid things thank you, its working now!
@Ruairi92 you can tick the tick next to MarcB's answer to mark it as the answer to your problem. Please do this so that others who have a similar problem can find his asnswer. Cheers
1

As your only new, you really should stop using mysql_ and change to mysqli_ or (my preference) PDO(). Your message in your title seems to be coming from your selectdb line, so you're actually connecting to the database fine, it's just not able to locate the schema that you are trying to use (i.e. "search" does not exist as a schema name in your DB environment). Unless that's not the error that you are getting, in which case it's a flaw in your actual SQL query. Without an accurate statement of what you are getting back on the screen when you try to run the script then not much can be done to help.

Comments

0

I would rather do somehow like I did ages ago, see below:

function db_connect($host, $username, $passwd, $db_name) {
    $db = mysql_connect($host, $username, $passwd);

    if (!$db) {
        echo "Login to database failed: ";
        echo mysql_error();
        return false;
    }

    $result=mysql_select_db($db_name); //Select the database

    if (!$result) {
        echo "Cannot find the database: ".$db_name;
        echo mysql_error();
        return false;
    }

    return $db; //Database descriptor
}

However you shouldnt rely on the old MYSQL extension, but use MYSQLi.

EDIT: as @tadman pointed out there were a @ sign infront the mysql_connect call, however there is an if statement for checking and a call to error output.

1 Comment

Adding the @ to add error suppression is hardly helping anything here.
0

As others have said, your code is something of a mess - you seem to be new to programming, not just in PHP. But everyone has to start somewhere, so....

You might want to start by learning a bit about how to ask questions - specifically you have included a lot of code which is not relevant to the problem you are asking about - which is about connecting to MySQL. OTOH you have omitted lots of important information like the operating system this is running on and what diagnostics you have attempted. See here for a better (though far from exemplary example).

Do read your question carefully - the title implies an issue with mysql_select_db() while in the body of the content you say you think the problerm is with mysql_connect(). If you had provided the actual output of the script we would have been able to tell for ourselves.

You should be checking that MySQL is actually running - how you do that depends on your OS. You should try connecting using a different client (the mysql command line client, sqldeveloper, Tora, PHPMyAdmin....there are lots to choose from).

You should also check (again this is operating system specific) that your MySQL instance is running on the same port as is configured in your php.ini (or overridden in your mysql_connect() call).

MySQL treats 'localhost' as something different from 127.0.0.1 on Unix/Linux systems. If this applies to you try 127.0.0.1 in place of localhost.

or die("could not connect");

Does not help in diagnosing the problem. If instead you...

or die(mysql_error());

You will get a more meaningful response.

BTW: while writing code using the mysql extension rather than mysqli or PDO is forgivable, I am somewhat alarmed to see what appears to be medical information managed with no real security.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.