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");
Related
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
I got following error when i run code:
Notice: Use of undefined constant newdb - assumed 'newdb' in C:\xampp\htdocs\cj\include\conn.php on line 9 No database selected
The following code for connecting i am using in conn.php file
,
<?php
$lusername = "root";
$lpassword = "";
$lhostname = "localhost";
// connection to the database
$dbhandle = ($GLOBALS["___mysqli_ston"] = mysqli_connect($lhostname, $lusername, $lpassword)) or die("Unable to connect to mysql");
mysqli_select_db($GLOBALS["___mysqli_ston"], newdb) or die("Unable to connect to mysql");
// echo "Connected to mysql<br>";
?>
how to fix it?
This is just something to consider: try using it without ["___mysqli_ston"]
Example
<?php
$servername = "localhost";
$username = "";
$password = "yourpass";
$dbname = "yourDBname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Read special character sets
$conn->set_charset("utf8");
// Check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
Just an example of different easier approach all up to you.
For the converter
Example
<?php
$servername = "localhost" ;
$username = "root";
$pass = "";
$con = ($GLOBALS["___mysqli_ston"] = mysqli_connect($servername, $username, $pass)) or die("Problem occur in connection");
$db = ((bool)mysqli_query($con, "USE " . info));
?>
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.
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
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 );
?>