0

I'm working on developing a website through Dreamweaver with html, php, and mySQL, and I wanna connect it to the localhost, so I installed easyPHP and added an alias in the local files with the name saadstore and the directory of the folder However, when I run my code it displays an error when the database name is selected! Instead if I run a page that doesn't interact with links and other pages in my site (doesn't need connection) it works just fine! Here's the code:

<?php
$db_host="localhost";
$db_username="root";
$db_pass="";
$db_name="saadstore";

mysql_connect("$db_host","$db_username","$db_pass")or die("Could not connect to mySQL"); 
mysql_select_db("$db_name")or die("no database");
?>

when I save and open the file from the localhost I get "no database" I'm kinda new to this so .. any help?

5
  • mysql_connect($db_host,$db_username,$db_pass) AND mysql_select_db($db_name) Remove the double quotes. Commented Sep 22, 2015 at 1:09
  • why did you tag sql-server-2008? you are connecting to a MySQL database, not SQL Server. The tags "sql-server-2008", "web", and "connection", do not belong here. Commented Sep 22, 2015 at 1:12
  • 1
    @aldrin27 works either way, just as long as they're not single quotes. Commented Sep 22, 2015 at 1:17
  • @AcidReign Again I'm new to this :) can u explain what u just wrote? Commented Sep 22, 2015 at 1:20
  • @MohamedM.Saad do you mean my comment asking why you put irrelevant tags? This question is only about "php" and "mysql" the rest of the tags do not belong on this question. (the tag "connection" is just ambiguous and unhelpful IMO) Commented Sep 22, 2015 at 1:23

4 Answers 4

1

Try out this:-

<?php
$db_host="localhost";
$db_username="root";
$db_pass="";
$db_name="saadstore";

$conn = mysql_connect($db_host, $db_username, $db_pass)or die("Could not connect to mySQL"); 
$selected = mysql_select_db($db_name, ,$conn)or die("no database");
?>

At last

  mysql_close($conn);
Sign up to request clarification or add additional context in comments.

Comments

0

The mysql_* functions are deprecated/obsolete, and will soon be removed from PHP entirely, please don't use them.

Instead you should use PDO: http://php.net/pdo

Example from php.net:

<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

4 Comments

thanks, but it says "Unknown database 'saadstore'" so how do I know the name of my database if it's not the name of the alias I created in the localhost?
I don't know what you mean by "alias I created in the localhost". The "database" name (also called Schema) has to exist in the MySQL server you are connecting to.
Well, I simply installed easyPHP and through it I could enter 127.0.0.1/home and make a database, so if there's another way to put my project in a localhost, what is it?
Well it sounds like you did not actually create a schema in MySQL called "saadstore". I don't know anything about easyPHP, but when it comes to managing my MySQL server I prefer to use MySQL Workbench: mysql.com/products/workbench (it is free)
0

You Can Try "mysqli_connect" Like that way :

$conn = mysqli_connect("localhost","my_user","my_password","my_db");

Comments

0

First of all, you should use AcidReign solution, it's using PDO instead of the deprecated mysql_*. I also don't know anything about easyPHP, but realize you actually don't have a database called saadstore. Then, access on your browser the adress http://localhost/phpmyadmin it's gonna ask you the username and password that according to you the user is root and you haven't any password.

After you log in you will see a screen like the below:

enter image description here

On the left side you can see all your databases, if the name saadstore is not there, then you don't have the database and need to create them. To do so, you must go to DataBase tab and put the name of your database(saadstore) and the Collation, which I recommend to put utf8_general_ci.

enter image description here

Now you have your database and may create your tables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.