This is probably just a stupid mistake somewhere but i cant connect to mysql. And it have been bugging me for days now!
Im trying to create a database for a web shop tutorial am doing. And this is done thru Dreamweaver and cPanel. Im trying to connect to my mysql database from this code:
-----connect_my_sql.php-----
<?php
$db_host = "hostip";
$db_username = "build";
$db_pass = "*****";
$db_name = "myonlinestore";
$myConnection = mysqli_connect("$db_host","$db_username","$db_pass", "$db_name") or die ("could not connect to mysql".mysql_error());
?>
(password and hostip is changed) Where username is connected to the database and psw is the right one. And i dont get any error from .mysql_error()
all this is done in a subfolder on my main domain on godaddy.
Have tryed to do a admin page and i cannot connect to my database to use the log in i have created in mysql. witch is the following code:
<?php
session_start();
if (isset($_SESSION["manager"])) {
header("location: index.php");
exit();
}
?>
<?php
if (isset($_POST["username"]) && isset($_POST["password"])) {
$manager = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]); // filter everything but numbers and letters
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]); // filter everything but numbers and letters
// Connect to the MySQL database
include "../storescripts/connect_to_mysql.php";
$sql = mysql_query("SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1"); // query the person
$existCount = mysql_num_rows($sql); // count the row nums
if ($existCount == 1) { // evaluate the count
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
}
$_SESSION["id"] = $id;
$_SESSION["manager"] = $manager;
$_SESSION["password"] = $password;
header("location: index.php");
exit();
} else {
echo 'That information is incorrect, try again <a href="index.php">Click Here</a>';
exit();
}
}
?>
(i removed all the styling in the code)
and all i get is that the information is incorrect, but i have created a admin table and user in my database. But i cant log on.
All i get from this is that there is some issue when im trying to connect. and it has to be from my end becouse godaddy cant see any errors or that i cant connect on there logs.
I will be very greatfull if you could help me with this problem.
Thanks
You can't connect to database because you opened connection with mysqli
api and after you use mysql_query
that actually belongs to mysql
api and doesn't have any connection to database
$sql = mysql_query("SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1"); // query the person
//^ here you use the wrong api
Just change this with mysqli query command, learn more here
The top file is called 'connect_my_sql.php' file being included is 'connect_to_mysql.php";
Also, use 'require_once' since the file is required for the script to function.
Additionally to what the answers of @DoctorDerp and @Fabio, this is wrong too
$myConnection = mysqli_connect("$db_host","$db_username","$db_pass", "$db_name") or die ("could not connect to mysql".mysql_error());
remove everything after the or, and then on the line under add an if
to test connection like so the code become like this
$myConnection = mysqli_connect("$db_host","$db_username","$db_pass", "$db_name");
if(!$myConnection){
die("Cannot connect to database");
}
i use a simpler sign-in script i created.
$dbc=mysqli_connect("host","username","password","database") or die ("Error in Connecting to database");
$email=$_POST["idamuid"];
$password=$_POST['idamupassword'];
$email=htmlspecialchars(mysql_escape_string($email));
$password=htmlspecialchars(mysql_escape_string($password));
$query="select member_email from members where member_email='$email'
and member_password=sha('$password') ";
$result=mysqli_query($dbc,$query);
if(mysqli_num_rows($result)==1){
session_start();
$_SESSION['userid']=$email;
header("location:index.php");
mysqli_close($dbc);
}
else{
echo "Login Unsuccessful";
echo '<p><a href="signin.php">Please Try Again</a></p>';