Jump to content

Site search box and DB search


jav805

Recommended Posts

Hi,

 

I've been trying to learn this for about a week now. I'm trying to make my search box work on my site. If a user searches for something, I want to be able to show them results that match. I cannot for the life of me get my DB to connect. I have a table within the DB and data within that table. I'm about to lose my mind over this.

I've put the .php file on my webhost and when I go to the file I get the following error "Could not select database"

What am I doing wrong?

 

Here's the code:

*I've changed the un, pw, and other private information.

 

<?php

 

if (!$link = mysql_connect('HOST', 'USERNAME', 'PASSWORD')) {

    echo 'Could not connect to mysql';

    exit;

}

 

if (!$link = mysql_select_db('DATABASE')) {

    echo 'Could not select database';

    exit;

}

 

$sql = 'SELECT * FROM `TABLE` LIMIT 0, 30 ';

$result = mysql_query($sql, $link);

 

if (!$result) {

    echo "DB Error, could not query the database\n";

    echo 'MySQL Error: ' . mysql_error();

    exit;

}

 

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

    echo $row['foo'];

}

 

mysql_free_result($result);

 

?>

Link to comment
https://forums.phpfreaks.com/topic/166874-site-search-box-and-db-search/
Share on other sites

Hi there :-)

 

There could be a number of things wrong.

 

Check (and double check) you do not have the wrong database name as an argument to mysql_select_db()

 

It could be that you are assigning the result of mysql_select_db() to $link, which should hold the resource identifier.

It couldn't hurt to specify the resource identifier as an argument either. Try:

 

if (!mysql_select_db('DATABASE', $link)) {
    echo 'Could not select database';
    exit;
}

 

If that still doesn't work, let me know and I'd be glad to help further.

 

 

Hi there,

thanks for the info, however; I put it in the code (what you suggested) and I got the following error:

 

Parse error: syntax error, unexpected T_VARIABLE in /home8/DATABASE/public_html/FOLDER/search.php on line 8

 

I changed DB and Folder for privacy.

 

 

How very odd. Here's my complete edited version:

 

<?php

if (!$link = mysql_connect('HOST', 'USER', 'PASSWORD')) {
    echo 'Could not connect to mysql';
    exit;
}

if (!mysql_select_db('DATABASE', $link)) {
    echo 'Could not select database';
    exit;
}

$sql = 'SELECT * FROM `TABLE` LIMIT 0, 30 ';
$result = mysql_query($sql, $link);

if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

while ($row = mysql_fetch_assoc($result)) {
    echo $row['foo'];
}

mysql_free_result($result);

 

I've run it against one of the tables in one of my databases. I assure you that it works and there are no syntax errors.

 

Does your new code differ from this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.