Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Something is wrong with my PHP MySql

Status
Not open for further replies.

MJAZ

Programmer
Aug 1, 2006
73
US
Hello! My PHP script is not connecting to the MySql database. Every time I run it, it says COuld not Connect: and then nothing. I am using mysql_error() and error checking. Here is the code (database usernames and passwords removed):

Code:
function user_check($user) {
    $con = mysql_connect("localhost:3306","username","password");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("userinfo", $con);
    
    $result = mysql_query('SELECT * FROM user_data WHERE username="$user"');
    
    $rows = 0;
    
    while(mysql_fetch_array($result))
    {
        
        $rows++;   
    }
    if($rows > 0) {
        return false;
    }
    else {
        return true;
    }
    mysql_close($con);
}

function insert($usr,$pss,$eml,$mon,$day,$yr,$ip,$submitted) {
    $con = mysql_connect("localhost:3306","username","password");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    
        mysql_select_db("userinfo", $con);
    
    $query = 'INSERT INTO user_data (username,password,email,bmon,bday,byr,ip,submitted_date)
                        VALUES("$usr","$pss","$eml","$mon","$day","$yr","$ip","$submitted")';
    
    if(!mysql_query($query,$con)) {
        die('Query failed: ' . mysql_error());
    }
    else {
        mysql_close($con);
    }
}
The user_check() function checks whether a username has been taken already. The insert function inserts the values into the database. Like I said, one of these functions is not connecting to the database. Also, I occaisionally get the error "No database selected." Any Ideas?



Matt
 
3306 is the default MySQL con port

Try just to rem " :3306 "

 
if you're not getting anything back from the mysql_error() call then chances are that there is no server instance available.

can you access mysql over the command line?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top