I wnt to connect php and mysql database [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
<?php
$host ="localhost";
$user = "root";
$pass = "";
$db = "StuDet";
$conn = mysql_connect($host,$user,$pass);
mysql_select_db($db,$conn);
#mysql_select_db($database) or die ("Unable to select database");
?>
I have a doubt in line "$conn = mysql_connect($host,$user,$pass);"

<?php
$host ="localhost";
$user = "root";
$pass = "";
$db = "StuDet";
$con = mysqli_connect($host,$user,$pass,$db);
// Check connection
if (!$con){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try the above code. Use mysqli.

The code is correct. If no connection is made, check your credentials and db name.
Also, since mysql is deprecated, change to mysqli.
link http://php.net/manual/en/book.mysqli.php

Related

mysqli_select_db() ERROR, Database couln't connect [closed]

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

Connection to a db from localhost to another server with php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
It's possible to create a mysql connection from localhost to another server using php, if it's possible, how? Thanks!
$host_name = "www.yourdomain.com";
$database = "pdo"; // Change your database name
$username = "root"; // Your database user id
$password = "test"; // Your password
try {
$dbo = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Yes it is possible.You must have the username and password of that domain in which you want to connect.
mysqli_connect("www.domain.com","username","password","database_name")or die("Error " . mysqli_error());
Yes, Simply pass following details about your server:
<?php
$servername = "your-server-name-or-ip";
$username = "your-server-username";
$password = "your-server-password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
$url = 'mysql:host=xxx.xxx.xxx.xxx;dbname=xxxxxx'
$username = xxx;
$password = xxx;
$db = new PDO($url, $username, $password);
$query = $db->query('select * from some_table');
$query->execute();
$res = $query->fetchAll(PDO::FETCH_ASSOC);

It is no any erro message showing when using PHP to Connect Mysql Database , [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
It is nothing to show!!! No any error message
It seems to just print out the word and not trying to connect mysql
I am using Netbean to build the project Is it have any problem?? Thankyou
Here is mycode:
<?php
$link = mysql_connect("localhost", "root", "")
or die("No");
print ("Connected");
mysql_close($link);
?>
Please check below :
<?php
$link = mysqli_connect("localhost", "root", "");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Note: Try to use mysqli instead of mysql, as it is deprecated now.
try this
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

How to connect to mysql database [closed]

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

Unable to connect to database using PHP [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm having trouble getting this code correct:
$connection=mysql_connect(db_server, db_user, db_pass);
if(!$connection)
{
die("Database Connection failed".mysql_error());
}
$db_select=mysql_select_db(db_name,$connection);
if(!$db_select)
{
die("Database Selection failed".mysql_error());
}
When I try the script it says:
Parse error: syntax error, unexpected T_DNUMBER in /home/a8592246/public_html/pic/include/connect.php on line 3
If posssible, can anyone copy and paste the code with the correct settings?
$mysql_host = "mysql16.000webhost.com";
$mysql_database = "a8592246_dbname";
$mysql_user = "a8592246_dbuser";
$mysql_password = "mypassword";
} is missing in second if statement, and use correct parameters in mysql_connect and mysql_select_db functions
Please try this code
$mysql_host = "mysql16.000webhost.com";
$mysql_database = "a8592246_dbname";
$mysql_user = "a8592246_dbuser";
$mysql_password = "mypassword";
$connection=mysql_connect( $mysql_host,$mysql_user,$mysql_password);
if(!$connection){
die("Database Connection failed".mysql_error());
}
$db_select=mysql_select_db( $mysql_database,$connection);
if(!$db_select) {
die("Database Selection failed".mysql_error());
}
I don't use mysql_connect because it is better to use PDO or ADOdb, but lemme give it a try! Try this code :)
$connection=mysql_connect("mysql16.000webhost.com", "a8592246_dbuser", "mypassword");
if(!$connection)
{
die("Database Connection failed".mysql_error());
}
$db_select=mysql_select_db("a8592246_dbname",$connection);
if(!$db_select)
{
die("Database Selection failed".mysql_error());
}
mysqli_connect(host,username,password,dbname);
host: you need to write here IP address or the name of the host you are working on eg:
mysqli_connect("localhost",username,password,dbname);
username: you need to mention the username here eg:
mysqli_connect(host,"root",password,dbname);
password: write password if any.
dbname: write name of the db you want to connect with. eg.
mysqli_connect(host,username,password,"my_db");
In your case:
$mysql_host = "mysql16.000webhost.com";
$mysql_database = "a8592246_dbname";
$mysql_user = "a8592246_dbuser";
$mysql_password = "mypassword";
mysqli_connect($mysql_host, $mysql_user,$mysql_password,$mysql_database);
According to Your provided code , see below:-
$mysql_host = "mysql16.000webhost.com";
$mysql_database = "a8592246_dbname";
$mysql_user = "a8592246_dbuser";
$mysql_password = "mypassword";
$connection=mysql_connect($mysql_host, $mysql_user, $mysql_password);
if(!$connection)
{
die("Database Connection failed".mysql_error());
}
$db_select=mysql_select_db($mysql_database,$connection);
if(!$db_select)
{
die("Database Selection failed".mysql_error());
}
In Your existing code, $ is missing in mysql_connect() function and in db_name
try to use mysqli_connect instead of mysql_connect
<?php
$con=mysqli_connect(db_server,db_user,db_pass,"database_name");
/** Check connection **/
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else{
echo "Connection established ";
}
mysqli_close($con);
?>

Categories