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);
?>
Related
I have an issue with PHP script not connecting to MySQL database. Below code always generates error:
$dbconn = #mysql_connect('SFDC1', '<username>', '<password>');
if (!$dbconn)
{
die('Error connecting to DB!');
}
There is no issue if I connect to the database using MySQL workbench with same credentials. Issue only occurs during the communication between PHP and MySQL.
Any help on debug of this issue?
Try this one;
$dbhost = "localhost";
$dbuser = "username";
$dbpass = "pass";
$dbname = "DBname";
if(!#mysql_connect($dbhost,$dbuser,$dbpass)) {
echo "Cannot connect";
die();
} else
mysql_select_db($dbname);// or report();
Use this code to connect mysql.
<?php
$dbconn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$dbconn ) {
die('Could not connect: ' . mysql_error());
}
?>
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
This is a general question.
I`ve compliled site in xampp and it is properly running, because lack of experience I used mysql functions rather than the improved mysqli.
Uploading my site on ecowebhost the site does not work, I have changed paths so that, apparently, no connection error happens. but still I cannot interact with my database.
From the site seems that php4 version are supported, am I required to recompile my web site because of that?
this is a little sample code of what I am trying to do...
<?php
function connectto($tablename){
$db_host = "xxx";
$db_username = "xxx";
$db_pass = "xxx";
$link = mysqli_connect("$db_host","$db_username","$db_pass","$tablename") or die ("Could not connect to MySQL");
// #mysql_connect("$db_host","$db_username","$db_pass") or die ("Could not connect to MySQL");
// #mysql_select_db("$tablename") or die ("No database");
}
?>
Thanks
if I run the following
<?php
// connection to database
// include_once "connectto.php";
// connectto('test');
$tablename = "test";
$db_host = "xxx";
$db_username = "xxx";
$db_pass = "xxx";
$link =mysqli_connect("$db_host","$db_username","$db_pass","$tablename")
or die ("Could not connect to MySQL");
if (mysqli_connect_errno()) { die("Failed to connect to MySQL: " . mysqli_connect_error());}
else { echo " <br /><br />connection established! <br />";}
?>
the only error message that comes out is
Could not connect to MySQL
update....
I got a bit of my code working but I am struggling with finding a way to insert some values in the database...
<?php
$db_host = "xxxx";
$db_username = "xxxx";
$db_pass = "xxxx";
// $link = mysqli_connect("$db_host","$db_username","$db_pass","cl45-members-7b5") or die ("Could not connect to MySQL");
#mysql_connect("$db_host","$db_username","$db_pass") or die ("Could not connect to MySQL");
#mysql_select_db("cl45-members-7b5") or die ("No database");
$sql ="INSERT INTO members (username, email, gps_lat, gps_long, password, skill, skill_rate,) VALUES ('a','2','3','4','5','5','6')";
print '<br /><br /> Great! now you are registered; now update your profile <br />';
mysql_query ("$sql");
print '<br /><br />tutto ok;<br /><br />';
?>
Is there anything that strikes being wrong? I got no error like database not found...
Your question is confusing me slightly but you can connect using MySQLi using the following:
$link = new mysqli($db_host,$db_username,$db_pass,$tablename);
In your code you have
or die ("Could not connect to MySQL");
Which says if you can not connect for whatever reason just print out this. You're not going to get any more information because you need to include $conn->connect_error e.g.
if ($link->connect_error) {die ("Failed: " . $link->connect_error);}
You should end up with something like
$tablename = "test";
$db_host = "xxx";
$db_username = "xxx";
$db_pass = "xxx";
$link = new mysqli($db_host,$db_username,$db_pass,$tablename);
if ($link->connect_error) {
die ("Failed: " . $link->connect_error);
} else {
echo " <br /><br />connection established! <br />";
}
After some wondering around and splitting the problem in little subtasks, i believe I managed to understand that that little stupid extra comma in the list was preventig me to run the program, I copy pasted a syntax shared on-line and now everything seems to work...
outcome:
- check the spelling
- Isolate problems one by one...
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.
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