I am having trouble using my Webmatrix database. I would like to insert data into the database with a registration page written in PHP.
When I created my database the "web.config" file was created. How can I use the connectionString from the "web.config" file in my PHP to be able to connect to the database?
Connection string from my web.config file:
<connectionStrings>
<add connectionString="server=localhost;uid=root;database=sitedetest;Pwd=123" name="sitedetest" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
I tried this:
config.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '123';
$dbname = 'sitedetest';
?>
In the registration page:
include 'config.php';
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
or die ('Error connecting to mysql');
mysql_select_db($dbname);
But when I try using the registration form, I always get the 'Error connecting to mysql' message
Thank you for your time
Related
I have created a website for my versity project...But I am asked to add a database in it..That's why I need to know how to add a database via xamp in a website.
Use this code to create a connection to your database from php
<?php
$servername = "localhost";
$username = "root";
$password ="db-password";
$dbname = "db-name";
//create connection
$conn = new mysqli($servername,$username,$password,$dbname);
//check connection
if ($conn->connect_error) {
die("connection failed" .$conn->$connect_error);
}
?>
use this link to understand how to create and execute queries.
https://www.w3schools.com/php/php_mysql_connect.asp
I recently switched to CPanel, and ever since then, I've had issues. I'm having an issue connecting to a database. I created it in CPanel, with the hypothetical username 'root' and password '123'. I have a file /scripts/dbh.php and a file /index.php. Here are the contents.
dbh.php:
<?php
// Database Handler
$db_name = 'hughchalmers';
$servername = "localhost";
$username = "root";
$password = "123";
$table_name = 'accounts';
$conn = mysqli_connect($servername, $username, $password, $db_name) or die('Cannot connect to database. Contact an admin');
?>
(hughchalmers is a database, accounts is a table)
index.php:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/memedictionary/scripts/header.php');
include($_SERVER['DOCUMENT_ROOT']. '/memedictionary/scripts/dbh.php')
?>
<head>
<title>Login</title>
</head>
<body>
</body>
What's the issue here? It just dies with that message I set (Cannot connect to database. Contact an admin). Apologies, I'm very new to MySQL and CPanel
Check your last parameter for mysqli_connect(). Looks like you're referring to a table when instead it must be the database name or is it just a variable name but you're passing a database name actually?
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)
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.
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