Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have created a database in phpmyadmin but when i am making a connection to that database through php, i am getting a connection error. I have rechecked the code and i don't think there is any problem with the code in php.

What I have tried:

<?php 
define('DB_HOST', 'localhost'); 
define('DB_NAME', 'alabdouli_users'); 
define('DB_USER','safeer123'); 
define('DB_PASSWORD','******'); 
$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysqli_error()); 
$db=mysqli_select_db(DB_NAME,$con) or die("Failed to connect to database: " . mysqli_error()); 
function NewUser() 
{ 
$fullname = $_POST['name']; 
$userName = $_POST['user']; 
$email = $_POST['email'];
$password = $_POST['pass']; 
$query = "INSERT INTO websiteusers (fullname,userName,email,pass) VALUES ('$fullname','$userName','$email','$password')"; 
$data = mysqli_query ($query)or die(mysqli_error()); 
if($data) 
{ 
echo "YOUR REGISTRATION IS COMPLETED..."; 
} } 
function SignUp() 
{ 
if(!empty($_POST['user'])) //checking the 'user' name which is from Sign-Up.html, is it empty or have some text 
{ 
$query = mysqli_query("SELECT * FROM websiteusers WHERE userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysqli_error()); 
if(!$row = mysqil_fetch_array($query) or die(mysqli_error())) { newuser(); 
} 
else 
{ 
echo "SORRY...YOU ARE ALREADY REGISTERED USER..."; 
} } } 
if(isset($_POST['submit'])) 
{ 
SignUp(); 
} 
?>
Posted
Updated 24-Jan-18 2:34am
Comments
[no name] 24-Jan-18 8:24am    
Can you please post what is the error you are getting?
lookwhosback 24-Jan-18 8:27am    
failed to connect to the database
Jochen Arndt 24-Jan-18 8:30am    
Where does it fail and what is the error message?
[no name] 24-Jan-18 8:36am    
lookwhosback,Please don't post issues/problems as solution
[no name] 24-Jan-18 8:37am    
Can you please provide the stack trace.

See PHP: mysqli::select_db - Manual[^]. The first argument is the connection and the second is the name but you are passing them reversed.

It must be
PHP
$db=mysqli_select_db($con,DB_NAME) or die("Failed to connect to database: " . mysqli_error());

[EDIT]
Because you are using the procedural style, you have to use it too for the error function. Note also that mysqli_error() can't be used when connecting fails. You have to use mysqli_connect_error() instead:
PHP
$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysqli_connect_error()); 
$db=mysqli_select_db($con,DB_NAME) or die("Failed to connect to database: " . mysqli_error($con));
[/EDIT]

[EDIT2]
And similar for all other SQL operations where you did not pass the connection/link $con.
[/EDIT2]
 
Share this answer
 
v4
Comments
lookwhosback 24-Jan-18 8:44am    
Thankyou so much for that, this feels so good first time someone solved my problem on the internet :D i really didn't know that was the problem, that fixed the database connectivity but now i don't know why i am not getting any message for adding the user to the database.
Jochen Arndt 24-Jan-18 8:49am    
Thank you for the feedback and accepting my solution.

See my updated answer. It explains why you did not get the PHP error messages and also why the queries did not work: Same reason; you have to use the procedural style passing the link/connection.
lookwhosback 24-Jan-18 8:55am    
Sir Jochen, I got the previous problem solved, that is linking the database with the php file with your help. Still the queries are not executing and the user is not being added to the database
Jochen Arndt 24-Jan-18 9:03am    
See update 2.
You have to use the procedural style everywhere because you decided initially to use that style.
So it must be
$data = mysqli_query($con, $query) or die(mysqli_error($con));
and similar ($con as first/only parameter) for all mysqli function calls.

If then a call fails you will get at least a meaningful error message that helps finding the problem like passing wrong parameters or typos in table or field names.
lookwhosback 24-Jan-18 9:26am    
I have replaced as you said, still the record is not being added and i am not receiving any message. this is how my code looks like after changing it to the procedural style.
<?php 
define('DB_HOST', 'localhost'); 
define('DB_NAME', 'alabdouli_users'); 
define('DB_USER','safeer123'); 
define('DB_PASSWORD','3333333'); 
$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysqli_connect_error()); 
$db=mysqli_select_db($con, DB_NAME) or die("Failed to connect to database: " . mysqli_error($con)); 
function NewUser() 
{ 
$fullname = $_POST['name']; 
$userName = $_POST['user']; 
$email = $_POST['email'];
$password = $_POST['pass']; 
$query = "INSERT INTO websiteusers (fullname,userName,email,pass) VALUES ('$fullname','$userName','$email','$password')"; 
$data = mysqli_query ($con, $query)or die(mysqli_error($con)); 
if($data) 
{ 
echo "YOUR REGISTRATION IS COMPLETED..."; 
} } 
function SignUp() 
{ 
if(!empty($_POST['user'])) //checking the 'user' name which is from Sign-Up.html, is it empty or have some text 
{ 
$query = mysqli_query($con,"SELECT * FROM websiteusers WHERE userName = '$_POST[user]' AND pass = '$_POST[pass]'") or die(mysqli_error($con)); 
if(!$row = mysqil_fetch_array($con, $query) or die(mysqli_error($con))) 
{ 
NewUser(); 
} 
else 
{ 
echo "SORRY...YOU ARE ALREADY REGISTERED USER..."; 
} } } 
if(isset($_POST['submit'])) 
{ 
SignUp(); 
} 
?>
<div id="Sign-Up"> 
<fieldset style="width:30%">
<legend>Registration Form</legend> 
<table border="0"> 
<tr> 
<form method="POST" action="signup.php"> 
<td>Name</td>
<td> <input type="text" name="name"></td> 
</tr> 
<tr> <td>Email</td>
<td> <input type="text" name="email"></td> 
</tr> 
<tr> 
<td>UserName</td>
<td> <input type="text" name="user"></td> 
</tr> 
<tr> <td>Password</td>
<td> <input type="password" name="pass"></td> 
</tr> 
<tr> 
<td>Confirm Password </td>
<td><input type="password" name="cpass"></td> 
</tr> 
<tr> 
<td><input id="button" type="submit" name="submit" value="Sign-Up"></td> 
</tr> 
</form> 
</table> 
</fieldset> 
</div>

this is my html code in which when i press the signup button and it should execute the signup.php, but i recieve an error from my php code saying "Failed to connect to the database"
 
Share this answer
 
Comments
[no name] 24-Jan-18 8:36am    
Please don't post your problems as solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900