Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi I'm new to PHP and i coded a php page and created a database. I tried to connect db but it's not connecting.
<?php
$uname = "root";
$pwd = "";
$hostn = "localhost";
//connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd)
or die("Unable to connect to MySQL");
//select a database to work with
$dbselect = mysqli_select_db("dataforuse",$mysqlconn)
or die("Could not select dataforuse");
mysql_close($mysqlconn);
?>
Hi Try to debug and echo bugs in connection with mysqli_error() function.
$uname = "root";
$pwd = "";
$hostn = "localhost";
//connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd) or die(mysqli_connect_error());
or
$mysqlconn = mysqli_connect($hostn, $uname, $pwd , $database) or die(mysqli_connect_error());
You are mixing mysql and mysqli
You have to replace
/connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd)
or die("Unable to connect to MySQL");
//select a database to work with
$dbselect = mysqli_select_db("dataforuse",$mysqlconn)
or die("Could not select dataforuse");
With this code
$mysqlconn = mysqli_connect($hostn, $uname, $pwd,"dataforuse")
or die("Unable to connect to MySQL");
connection variable then database name ..try this
$dbselect = mysqli_select_db($mysqlconn,"dataforuse");
and mysql close should be like this
mysqli_close($mysqlconn);
dont mix mysql with mysqli
You should write
<?php
$uname = "root";
$pwd = "";
$hostn = "localhost";
$dbname = "dataforuse";
$conn = new mysqli($hostn, $uname, $pwd, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
This will solve Your Problem
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 am creating a form that will take a person's name and email and see if the email exists in the database already but I can't get the database to be selected. The mysql_error() won't display the error either. Is there something wrong with my code for selecting the database? All it shows is the hardcoded text "Could not select database because" then nothing. I replaced all the variables associated with the database with random fillers so as not to give away my info but everything I have is correct regarding that.
$host = "host";
$user = "user";
$password = "pass";
$database = "db";
$port = xxxx;
$table = "table";
// Create connection
$conn = mysqli_connect($host, $user, $password, $database, $port);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connection Successful";
mysqli_select_db($database)
or die("Could not select database because".mysqli_error());
// check if the username is taken
$check = "select email from $table where email = '".$_POST['email']."';";
$qry = mysqli_query($check)
or die ("Could not match data because ".mysqli_error());
$num_rows = mysqli_num_rows($qry);
if ($num_rows != 0) {
echo "Sorry, there the username $username is already taken.";
}
Don't call mysqli_select_db. You've already selected your database with the fourth parameter to mysqli_connect.
You only need to call mysqli_select_db if you want to access a different database after the connection has been established.
Also, Raptor is correct that if you call procedural mysqli_error() you must pass it a connection handle.
For Procedural style mysqli_error(), you must supply the MySQLi DB link as parameter:
mysqli_select_db($database)
or die("Could not select database because" . mysqli_error($conn));
But it's useless to call mysqli_select_db() as you already specified the DB schema in mysqli_connect().
Additionally, your SQL is vulnerable to SQL Injection attack. Always escape the parameter before putting into SQL statement, or use prepared statements.
try this code
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
I am trying to connect to a MySQL database through a php script. It gives me this error from mysql_error(): Access denied for user '#localhost' to database 'userinfo'
userinfo is the database.
my script is this
<?php
$servername = "localhost";
$username = "root";
$password = "'mm'";
$database = "userinfo";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully<br>";
mysql_select_db($database) or die(mysql_error());
echo "connected successfully to db:" . $database . "<br>";
?>
you are connecting using
mysqli_
function
and selecting data with
mysql_
avoid using both at the same time. since they're incompatible.
use the mysqli_ alternative instead
mysqli_select_db($conn, $database);
and
mysqli_error($conn)
Please keep in mind this isn't the safest way. But since you have said your learning this it is a start.
<?php
$servername = "localhost";
$username = "root";
$password = "mm";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
http://www.w3schools.com/php/php_mysql_connect.asp
To select data from the database
http://www.w3schools.com/php/php_mysql_select.asp
It appeared you where combining the old mysql in php with the new mysqli
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How do i write the connection string to connect to mysql database with this info:
Using php connection string
Database Details:
phpmyadmin url: https://medicalng.com/phpmyadmin
host: localhost
database: wlfmedic_ptest1
username: wlfmedic_ptest1
password: Prog#te$t104
Use mysqli. mysqli connection is like this:
$host = "localhost";
$database = "wlfmedic_ptest1";
$username = "wlfmedic_ptest1";
$password = 'Prog#te$t104';
$connection = mysqli_connect($host, $username, $password, $database);
if(mysqli_connect_errno()){ //To show error if fails to connect
die(mysqli_connect_error());
}
Something basic, not secure tho.
<?php
mysql_connect(localhost, ##YOUR USERNAME##, ##YOUR PASSWORD##);
mysql_select_db('##YOUR DATABASE NAME##');
$result = mysql_query("SELECT * FROM emails");
?>
and updated mysqli http://www.sanwebe.com/2013/03/basic-php-mysqli-usage
<?php
$db_hostName="localhost";
$databaseName= "wlfmedic_ptest1";
$db_userName= "wlfmedic_ptest1";
$db_password= "Prog#te$t104";
$con=mysql_connect($db_hostName,$db_userName,$db_password);
if(!$con){
die ("Could Not successfully with DataBase".mysql_error());
}else{
echo "Connect connect with succssfully .";
}
$db=mysql_select_db($db_databaseName,$con);
if(!$db)
{
echo "Can notConnect with Bd Social";
}
else
{
echo "Conncet With Bd Social";
}
?>
try this ..
<?PHP
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
print "Database Found ";
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
}
?>
also you can refer this link