how to mysql database connection in php for sqlyog - php

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)

Related

Database connection Unknown error in PHP with MySQL server workbench not in phpmyadmin

I`m working on a java swing app. so that i have created a database called c_app in MySQL server workbench. what i need is to fetch data into a HTML page using PHP.
$username = "root";
$password = "";
$hostname = "localhost:3307";
$db = "c_app";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password, $db)
or die("Unable to connect to MySQL");
This code segment gives me Warning: mysqli_connect(): (HY000/1049): Unknown database 'c_app' in C:\xampp\htdocs\C_AppWeb\linkpage.php on line 7
Unable to connect to MySQL
What is the problem here? do i need phpmyadmin database rather than MySQl database? can anyone help me?
First one, both phpmyadmin and workbench are not database. They are tools to work with database.
I see you use port 3307 in your code, so let you try:
$username = "root";
$password = "";
$hostname = "localhost";
$port = 3307;
$db = "c_app";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password, $db, $port)
or die("Unable to connect to MySQL");

Connecting php to MS SQL server 2008

I need a connection script of php and MS SQL:
in trying the following but it doesn't work
$server = 'MVEKELDG156\SQLEXPRESS'; $username = 'EDUC.PWV.GOV.ZA\Mveke.L';
$password = 'password#'; $database = 'SAMS_EMIS_Warehouses';
if(!mysql_connect($server, $username, $password)) { exit('Error: could not
establish database connection'); } if(!mysql_select_db($database)) {
exit('Error: could not select the database'); }
The MSSQL extension is enabled by adding extension=php_mssql.dll to php.ini.
To get these functions to work, you have to compile PHP with --with-mssql[=DIR] , where DIR is the FreeTDS install prefix. And FreeTDS should be compiled using --enable-msdblib .
More References :
Installation
How to use PHP to connect to sql server
Connecting to an SQL Server Database with PHP
Check this
$myServer = "localhost";
$myUser = "your_name";
$myPass = "your_password";
$myDB = "SAMS_EMIS_Warehouses";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB
Source How to connect to MS SQL Server database

Not able to connect to mysql php/xampp

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.

Storage of database credentials in php.ini or a php file (ex. database_connection.php)?

Is one preferred over another?
And what would be php.ini equivalent syntax to connect to a database the same way this php script does?
database_connection.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
?>
The equivalent syntax would be:
$conn = mysql_connect() or die ('Error connecting to mysql');
There are no required parameters. If you do not provide any, the defaults from php.ini are used.

PHP MySQL authentication error

I have created an html form (demo.html) & POST it's value to a php file (demo1.php). Then i created database in MySQL & wrote connection code in the php file (demo1.php) but it gives the following error
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'#'localhost' (using password: YES) in C:\xampp\htdocs\demo1.php on line 8
Error connecting to mysql
<?php
// contact to database
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
//$conn = mysql_connect("localhost","root","password") or die ('Error connecting to mysql');
$dbname = 'test';
mysql_select_db($dbname,$conn);
//$connect = mysql_connect("localhost", "root", "pass") or die ("Error , check your server connection.");
//mysql_select_db("test");
//Get data in local variable
$v_name=$_POST['name'];
$v_email=$_POST['email'];
$v_msg=$_POST['msg'];
// check for null values
if ($v_name=="" or $v_msg=="")
echo "All fields must be entered, hit back button and re-enter information";
else{
$query="insert into contact(name,email,msg) values('$v_name','$v_email','$v_msg')";
mysql_query($query) or die(mysql_error());
echo "Your message has been received";
mysql_close($conn);
}
?>
Password for user root is incorrect.
Default password on local (home) server is empty, so I think this can help:
$dbpass = '';
The error is pretty explicit - the username/password combination youre using is incorrect. IF thisis a fresh install you need to set a password and/or create a new user.
mysqladmin
User Creation
Granting Privileges
Setting Passwords
For Local Xampp username-root password --''
<?php
// contact to database
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
?>
The main reason for this is that the password is wrong. Did you specify the root password when setting up xampp? If not, then leave the password blank as delphist states above

Categories