MySQL connect via PHP issue

Hello everyone,

as the thread name already says, I have an issue with connecting to my MySQL database via PHP.


mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

The actual application is a MySQL database driven search engine, programmed in PHP. Well, since I’ve tried connecting to my friend’s database (the PHP code, and database is of course on his server), though it didn’t work. My MySQL’s server is actually “localhost”, and the database name “search”, though my friend’s looks like “localhost:3306”, and database “1_search”. I have no idea why it won’t work. Besides the MySQL issue, the PHP part runs smoothly without bugs.

I hope someone’s helpful, I’m not an expert in MySQL.

Phyxas

Can you post the error the code mentioned outputs?

3306 is the default port for Mysql, so that shouldn’t mean anything.

No error is displaying. Whenever I try to submit my search engine, it only loads and stops, as nothing happens.

Run this code, replacing the required (host, username, password, database) values, and let us know what you get.


<?php
error_reporting(-1);
ini_set('display_errors', true);

$con = mysql_connect('host', 'username', 'password');

if(false === is_resource($con)){
    trigger_error(
        'Cannot connect to database:- ' . mysql_error(),
        E_USER_ERROR
    );
}

if(false === mysql_select_db('database', $con)){
    trigger_error(
        'Cannot select database:- ' . mysql_error(),
        E_USER_ERROR
    );
}
?>

Original:

<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Ergebnisse</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>Bitte gebe ein Kennwort, Buchstabe, oder Namen ein um nach Mitgliedern zu suchen.";
exit;
}

// Otherwise we connect to our Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'");

//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['fname'];
echo " ";
echo $result['lname'];
echo "<br>";
echo $result['info'];
echo "<br>";
echo "<br>";
}

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Fehler: Keine Vor- Nachname konnten deiner Angabe nach gefunden werden.<br><br>";
}

//And we remind them what they searched for
echo "<b>Gesucht nach:</b> " .$find;
}
?> 

With the error message:

<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Ergebnisse</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>Bitte gebe ein Kennwort, Buchstabe, oder Namen ein um nach Mitgliedern zu suchen.";
exit;
}

error_reporting(-1);
ini_set('display_errors', true);

$con = mysql_connect('localhost', 'username', 'password');

if(false === is_resource($con)){
    trigger_error(
        'Cannot connect to database:- ' . mysql_error(),
        E_USER_ERROR
    );
}

if(false === mysql_select_db('database', $con)){
    trigger_error(
        'Cannot select database:- ' . mysql_error(),
        E_USER_ERROR
    );
}
// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'");

//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['fname'];
echo " ";
echo $result['lname'];
echo "<br>";
echo $result['info'];
echo "<br>";
echo "<br>";
}

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Fehler: Keine Vor- Nachname konnten deiner Angabe nach gefunden werden.<br><br>";
}

//And we remind them what they searched for
echo "<b>Gesucht nach:</b> " .$find;
}
?> 

Link to my friend’s website: knuddels-hosting.de/php/suche.php

It looks like you’re relying on register globals, don’t, read why.

Try this, but to be fair, unless you have at least a basic understanding of php, you’re going to struggle to build what you require.


<?php
error_reporting(-1);
ini_set('display_errors', true);

if('POST' === $_SERVER['REQUEST_METHOD']){
    
    $con = mysql_connect('host', 'username', 'password');

    if(false === is_resource($con)){
        trigger_error(
            'Cannot connect to database:- ' . mysql_error(),
            E_USER_ERROR
        );
    }
    
    if(false === mysql_select_db('database', $con)){
        trigger_error(
            'Cannot select database:- ' . mysql_error(),
            E_USER_ERROR
        );
    }
    
    if(false === in_array($_POST['field'], array('info' ,'fname'))){
        trigger_error(
            'Invalid field',
            E_USER_ERROR
        );
    }

    $sql = sprintf(
        "SELECT * FROM users WHERE &#37;s LIKE '%%%s%%'",
        strtoupper($_POST['field']),
        mysql_real_escape_string(
            strtoupper(
                trim($_POST['find'])
            )
        )
    );
    
    $result = mysql_query($sql);
    
    if(false === is_resource($result)){
        trigger_error(
            'Cannot query database:- ' . mysql_error(),
            E_USER_ERROR
        );
    }
    
    while($user = mysql_fetch_assoc($result)){
        printf(
            '%s<br />%s<br />%s<br />',
            $user['fname'],
            $user['lname'],
            $user['info']
        );
    }
    
}
?>

Works. Thanks a lot, and for your compliment.

I’ve discovered my talent in Web-Design (HTML, CSS, XML) and graphic design, so PHP is still completely undiscovered for me. I work on it though.

Some people think it’s strange that a 16 year old boy is actually able to build a whole website in a day, but well…

Greets,

Phyxas

I’m glad you’ve got it working, keep it up. :wink:

By the way, is it actually possible to display an error message in case someone didn’t enter a keyword, letter?

In my case, it was the variable $find.

If this is your way of getting attention, then you fail, sir.

And even able to choose proper forum section for his question.
And even thinking about what you’re actually writing, if your post is Off-Topic.

just wondering, are u setting it up on your local com or a server online?

php hides errors by default and you have to edit php.ini file to make them display. look for something like display_errors…

besides that, you gota do a phpinfo() to check that mysql is actually activated for your system.

Everything works properly now. Thanks for to all of you. :slight_smile: