Mysql connect = No database selected? then Access denied - php

I am working on a php mysql connect script.
I wanted it to use functions so I can keep a track what is what, so Mysql connect got one function.
When I run it, I get first "No database selected" and when I specify it manually, it says "Access denied for # localhost".
Code
<?php
/* Mysql Data */
$MySqlUser = "root";
$MySqlPass = "**********";
$MySqlHost = "localhost";
$MySqlDataBase = "serveradmin";
/* End Mysql Data */
function MySqlConnect() {
$Connect = mysql_connect($MySqlHost, $MySqlUser, $MySqlPass);
$Database = mysql_select_db($MySqlDataBase);
if (!$Connect | !$Database) {
die("Cannot connect ".mysql_error());
}
}
MySqlConnect()
?>
So the problem, what causes this? I want the script to be nice and clean, and not sure if function() causes it.

This will fix it, but please look into PDO or mysqli
<?php
/* Mysql Data */
$MySqlUser = "root";
$MySqlPass = "**********";
$MySqlHost = "localhost";
$MySqlDataBase = "serveradmin";
/* End Mysql Data */
function MySqlConnect($MySqlUser, $MySqlPass, $MySqlHost, $MySqlDataBase ) {
$Connect = mysql_connect($MySqlHost, $MySqlUser, $MySqlPass);
$Database = mysql_select_db($MySqlDataBase);
if (!$Connect | !$Database) {
die("Cannot connect ".mysql_error());
}
return $Database;
}
$Database = MySqlConnect($MySqlUser, $MySqlPass, $MySqlHost, $MySqlDataBase );
?>

Related

How to connect MYSQL with PHP

I am stuck after writing all the code, the Website just doesn't connect with mysql at all!
I think this code will help
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
?>
write this code in html file and save it as .php
I hope it works
Create a database in phpmyadmin and then write down below code in a file and save it with .php extension
<?php
$db_username = "db_username"; // Your database login username
$db_password = "db_password"; // Your database login password
$db_name = "db_name"; // The name of the database you wish to use
$db_host = "db_host"; // The address of the database. Often this is localhost, but may be for example db.yoursite.com
$conn = new mysqli($db_host, $db_username, $db_password, $db_name);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
set those 4 variable as per your db details. mysqli_connect_error() will give you error if the connection will not establish

Error Connecting to Database PHP MySQL

I am having problems connecting with a database using a php form. I have created a separate file for php database parameters.
<?
$db_username = "username";
$db_password = "password";
$db_name = "db";
$db_host = "host";
?>
Include statement
include "db_data.php";
This is my connection String
$dblink = $Database->connect($db_username, $db_password, $db_name, $db_host);
This is my database class
<?php
class Database{
var $dblink;
var $query_error;
var $username;
var $password;
var $host;
var $database;
function connect ($username, $password, $dbname, $host) {
$this->dblink = mysql_connect($host, $username, $password);
mysql_select_db($dbname);
if($this->dblink == ""){
echo "Error connecting to database $database<Br>";
return -1;
}else{
return $this->dblink;
}
}
function disconnect(){
mysql_close($this->dblink);
}
}
?>
I haven't changed any of my connection parameters. Will starting again mysql server solve the problem ??
Thanks Jose
A better way to connect to a database is this
mysqli_connect("database ip", "database name", "username for database", "password");
Try that and place it in a file named connection.php and put the code above in a var like such :
$conn = mysqli_connect("database ip", "database name", "username for database", "password");
Then include the file in your PHP files so there's no need to re-write this in every file
Include("connection.php");

Not able to connect to mysql php/xampp

my I am trying to make login registration page in php.
apache and mysql is up and running.
I am executing the following code
<?php
$db_host = "localhost:777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
I am using localhost:777 because port 80 is being used by skype.
phpMyAdmin is up and running.
The output i am getting is nothing but the above code only.
can anyone help me on this issue?
Use MySQLi or PDO not MySQL.
You're passing the variables as a string using " " in the mysql_connect function
The syntax is as follows: mysqli_connect(host,username,password,dbname,port,socket);
<?php
$db_host = "localhost";
$db_port = "777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
$conn = mysqli_connect($db_host,$db_username,$db_pass,$db_name,$db_port);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try that and it should work. Please make sure you understand the code above and the error you made. Do not simply copy and paste please you will not learn from it.

connection to db4free with php

i'm new here and doing some project,
how can i connect to my database in db4free.net from php,
i have tried this
<?php
$host = "db4free.net/mainbookstore";
$user = "***";
$pass = "****";
$database = "mainbookstore";
$conn = mysql_connect($host, $user, $pass);
if ($conn) {
$buka = mysql_select_db ($database);
if (!$buka) {
die ("Database tidak dapat dibuka");
}
} else {
die ("Server MySQL tidak terhubung");
}
?>
but get error Such no host, but in my java use :
dataSource.setUrl("jdbc:mysql://db4free.net:3306/mainbookstore");
i can connect to my database.
Thanks before :)
Most likely, it is the missing port. Try
$host = "db4free.net:3306/mainbookstore";
and it should work.

change Mysqli to sqlsrv

I am new to PHP and need to modify some code in order to compile with my Microsoft SQL Server. The original code is like this. I downloaded it from usercake
<?php
/*
UserCake Version: 2.0.2
http://usercake.com
*/
//Database Information
$db_host = "localhost"; //Host address (most likely localhost)
$db_name = "202"; //Name of Database
$db_user = "202"; //Name of database user
$db_pass = "password"; //Password for database user
$db_table_prefix = "uc_";
GLOBAL $errors;
GLOBAL $successes;
$errors = array();
$successes = array();
/* Create a new mysqli object with database connection parameters */
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
GLOBAL $mysqli;
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
//Direct to install directory, if it exists
if(is_dir("install/"))
{
header("Location: install/");
die();
}
?>
I already installed sqlsrv and tested the link. It works with my database. Then I changed the code to this:
<?php
//Database Information
$server = "servername";
$connectionInfo = array("Database"=>"databasename","UID"=>"xxxxxx", "PWD"=>"xxxxxx" );
$db_table_prefix = "uc_";
GLOBAL $errors;
GLOBAL $successes;
$errors = array();
$successes = array();
/* Create a new sqlsrv object with database connection parameters */
$mssqlsrv = new sqlsrv($server, $connectionInfo);
GLOBAL $mssqlsrv;
if(sqlsrv_connect_errno()) {
echo "Connection Failed: " . sqlsrv_connect_errno();
exit();
}
//Direct to install directory, if it exists
if(is_dir("install/"))
{
header("Location: install/");
die();
}
?>
I get the following error message:
Fatal error: Class 'mssql' not found in
I think this line is the problem:
$mssqlsrv = new sqlsrv($server, $connectionInfo);
But I do not know how to fix this.
I would use PDO in this case: http://www.php.net/manual/en/pdo.construct.php
You can create a DSN connection to SQL Server
$dsn = "sqlsrv:Server=servername;Database=databasename"
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
The information to connect to SQL server is available here: http://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php

Categories