0

My problem is this, I am trying to create a simple session that allows me to log in using a Username and Password.

This is the session-

$username = $_POST['username'];
$password = $_POST['password'];

if ($username&&$password)
{
$connect = mysql_connect("localhost","root","root") or die ('Couldnt Connect!!');
mysql_select_db("AppDatabase") or die ('Couldnt find DB');
}
else 
die ('You have not entered username or password');
?>

It is a simple session, I understand that this will return nothing just an empty screen. The problem is that I am unable to connect to the database I receive this error,

Warning: mysql_connect(): No such file or directory in /Users/JJoSmith/Sites/login.php

I have had this problem before where this has happened this is due to the port that the database is on. I am currently using MAMP to host my database. I have been using this as my database file to connect to the database.

<?php 
$servername = "127.0.0.1";
$username = "root";
$password = "root";
$conString = "mysql:host=$servername;port=8889;dbname=AppDatabase";

    try {
        $conn = new PDO($conString, $username, $password);    // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        }
    catch(PDOException $e)
        {
            echo "Connection failed: " . $e->getMessage();
        };

?> 

This allows the connection to the database and is working fine, for the session though Im just testing how it works and trying to implement it slowly before using my connection file. This is to see what isn't working and to test the session to make sure that it works correctly. As shown it is working, I hope someone can help me with my problem.

Jonck
  • 1,587
  • 1
  • 10
  • 15
  • 5
    You are providing us with wrong code, your error says that you are working with `mysql_connect()` while I obviously see PDO in your code. – Jordy Apr 07 '15 at 16:20
  • Sorry Ive edited the code to add the session code in. Hopefully you can see it now. – Jonck Apr 07 '15 at 16:28
  • How can the session have so much code? A session is just to store data most likely retreived from the database. I seriously don't know what you are doing here. You are also mixing PDO and Mysql together. Why not use PDO for both files? Please explain what you mean with the session you are talking about here. – Jordy Apr 07 '15 at 16:32
  • Which seems to imply that your code would be fine if you change "localhost" in the "session code" to "127.0.0.1" - as you have in your PDO code. – James Apr 07 '15 at 16:35
  • Thank you James found out what was wrong, I changed localhost to 127.0.0.1:8889 and its working now. Sorry for the trouble I'm new to using PHP and using PDO. – Jonck Apr 07 '15 at 20:29

0 Answers0