Reputation: 3
I'm using xampp v3.1.0 3.1.0 for database and php. I'm trying to make a login website. In the following php code I'm trying to fetch rows of "admin" table which is already created in the "swan" database" to check if user exits. But I'm unable to select the database using mysql_select_db() and error no 1044 is displayed. What's wrong with the code??
Additional info: I've changed apache server port to 8081 instead of 80 as it was blocked earlier.Has this anything to do with the error?
<?php
$empid=$_POST["empid"];
$deptt=$_POST["deptt"];
$password=$_POST["pwd"];
$con=mysql_connect("localhost","","","");
if(!$con) { die("Error in connection"); }
$db=mysql_select_db("swan",$con); //unable to select database here
if(!$db) { echo mysql_errno($con); }
$query="SELECT empid,password FROM $deptt " ;
$result=mysql_query($query,$con);
if(!$result) { die("error running sql"); }
while($row=mysql_fetch_array($result))
{
if($row['empid']==$empid && $row['password']==$password)
{ echo "exists"; }
else
{ echo "doesn't exist";
}
}
?>
Upvotes: 0
Views: 9095
Reputation: 101
PHP 5 and later can work with a MySQL database using:
MySQLi extension (the "i" stands for improved) PDO (PHP Data Objects) Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012. Should I Use MySQLi or PDO? If you need a short answer, it would be "Whatever you like".
Both MySQLi and PDO have their advantages:
PDO will work on 12 different database systems, where as MySQLi will only work with MySQL databases.
So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included.
Both are object-oriented, but MySQLi also offers a procedural API.
Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very important for web application security.
Example (MySQLi Procedural)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
source : http://www.w3schools.com/php/php_mysql_connect.asp for more information
Upvotes: 0
Reputation: 171
<?php
function Create_Connection(){
$conn = mysql_connect("yourhost","yourusername","yourpassword");
if(!$conn){
die("Cannot create connection");
}else{
$this->getConn = $conn;
}
return $this->getConn;
}
function Close_Connection(){
mysql_close($this->getConn);
}
?>
Upvotes: 0
Reputation: 1732
$con = mysql_connect('localhost', '', '');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
Make sure your username and password are correct
Upvotes: 0
Reputation: 1629
1044 is access denied error. Probably because you did not select a user in mysql_connect
default for xampp is:
$con=mysql_connect("localhost","root","");
and by the way mysql_* functions are deprecated.
Upvotes: 0
Reputation: 2540
use $con=mysql_connect("localhost","root","");
in place of
$con=mysql_connect("localhost","","","");
"root" is the username of your mysql phpmyadmin
mysql_connect accept 3 parameters
1) Hostname
2) username
3) password
here
1) Hostname = localhost
2) username = root
3) password
Upvotes: 1