Not able to connect to mysql php/xampp - php

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.

Related

how to mysql database connection in php for sqlyog

I have a php file to import .csv file into database. For that i am using connection strings like below :
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'demo-eams';
$db_connect = mysql_connect($db_host, $db_user, $db_pass) or die ("Could not connect to MySQL");
$db_select = mysql_select_db($db_name) or die ("Could not connect to database");
i am using sqlyog for access mysql database.
My problem is whenever i run my coding it showing the connection error :
Could not connect to Mysql.
How to solve this?
Check following:
Check if your mysql is up and running
Validate mysql port (if its not default then add port in host name)

Mysql database connection to PHP

I am trying to connect mysql database to a php program. I used following program to establish connection. But it is not giving any error even I make any mistake with the code.
<?php
// creating database connection
$dbhost ="localhost";
$dbuser = "root";
$dbpass = "1234";
$dbname = "my_new";
//connection
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
//testing connection
if(mysqli_connect_errno()){
die ("database connection failed :".
mysqli_connect_error() .
"(".mysqli_connect_errno().")"
);
}
?>
In my computer, your code is work well. And I find that mysql_connect will not throw exception.

Wamp, PHP MySQL - Connecting error

I've installed a WAMP server. Now I want to connect to a MySQL database using php. I have one problem you need to get the username.
This is the code I use. Its from php.net http://php.net/manual/en/mysqli.quickstart.connections.php
<?php
$db_host = "localhost";
$db_username = "root";
$db_password = "password";
$db_name = "test";
$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
?>
I get the following error:
Warning: mysqli::mysqli(): in C:\wamp\www\Atletieker\dbconnect.php on line 7
Failed to connect to MySQL: (1045) Accès refusé pour l'utilisateur: 'root'#'#localhost' (mot de passe: OUI)
Warning: main(): Couldn't fetch mysqli in C:\wamp\www\Atletieker\dbconnect.php on line 11
How do I get the username of my database?
Try this...
$db_host = "127.0.0.1";
$db_username = "root";
$db_password = "";
$db_name = "test";
Obviously the $db_password should be empty, if you haven't changed any settings the default password should be blank.

Uploading my site from XAMPP to Apache public host

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...

Connecting to MySQL through WAMP

I had paid VPS for 1 month and everything i created i've tested it on the VPS. But now i want to make that on my WAMP, so i have installed WAMP.
My tables on the site depends from my mysql base, but they are not shown because there is some error with connecting to MySQL.
MySQL connection php file:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
Is there something that i need to change in the wamp settings to enable connection ?
Try this order
<?php
$hostname = "localhost";
$username = "root";
$password = "";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
Sometimes, you should follow the order of dbhandle values.

Categories