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):
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
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);
}
}
Matt