MySQL Access in Php to a database created in C-Panel - php

Basically, i'm having trouble connecting to a mysql database using a php web page.
I created the database in C-panel using the wizard
i'm connecting like this
$db_host = "localhost"; //your host Database address
$db_username = "xxxx"; //your account username
$db_pass = "xxxxx"; //your account password
$db_name = "xxxxx"; //your database name
#mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
#mysql_select_db("$db_name") or die ("no database");
But all my page seems to do is trigger the " could not connect to mysql"
my page does have wordpress installed at the minute but I'm planning to get rid of it as I'm creating my site myself. I'm just baffled why it can't connect, because in Phpmyadmin ( a feature on C-panel) it says the database is in localhost.

In C-panel you need to add database user and assign certain roles to it before you can connect to the database.

Related

How do I Connect an android application to a running MySQL Database, on an external hosted server

Basicly, i've been doing a lot of searching around and still haven't found anything that solves the issue.
I am trying to connect my application to a database that is ran and hosted by a company, on an external server (we already use this database for a application we run on users computers)
I want to query the database for a login, so that when a user enters in his username and password on my app, it will compair these details with the details in the database, and reply back, stating if these credentials are correct, or not, and allowing the user to see a different page which will pull specific details from the database for that user (but thats something i need to solve in the future)
my main issue at the moment, is I cant seem to get the app to be able to connect to the database, currently I am using a WAMP server to host a php file I made...
NOTE: i will be using "user" and "pass" instead of the actual username and password I have
<?php
//connecting
//server, username, password, database
$dbhost = 'external ip entered here';
$dbuser = 'user';
$dbpass = 'pass';
$dbdb = 'datbasename';
//connect to mySQL
$connect = mysql_connect ($dbhost,$dbuser,$dbpass) or die("Connection error");
//select the database
mysql_select_db($dbdb) or die ("database selection error");
//Retrieve the login details via POST
$username=$_POST['username'];
$password=$_POST['password'];
//Query the table
$query="SELECT llogint,lpasint FROM TCHAUFF (nolock)
WHERE llogint='$username' AND lpasint='$password'";
//check if there any results returned
$num = mysql_num_rows($query);
//if a record was found matching the details entered in the query
if($num == 1){
//create a while loop that places the returned data into an array
while($list=mysql_fetch_assoc($query)){
//store the returned data into variable
$output = $list;
//encode the returned data in JSON format
echo json_encode( $output );
//close the connection
mysql_close();
}}
?>
I don't think theirs any issue with the php file to be honest but please correct me if so haha!
the problem i'm having is if I try and run this php file from WAMP by placing the file into the www folder and going to localhost/androidtest.php
I get an error saying Warning: mysql_connect(): No connection could be made because the target machine actively refused it
I have tried literally every solution i could find,
added this information to the config.inc.php file
/* Server: UserSeaCogi[1] */
$i++;
$cfg['Servers'][$i]['verbose'] = 'external database';
$cfg['Servers'][$i]['host'] = 'external ip entered here';
$cfg['Servers'][$i]['port'] = '';
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['user'] = 'user';
$cfg['Servers'][$i]['password'] = 'pass';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
I'm lost for ideas!
i have added
bind-address = 0.0.0.0
to the my.ini file
but am thinking its something to do with the way i have WAMP setup, but I cant seem to find what to change.
I also know I definitely have access to the database, as I can run querys from it if I add it to visual studio...one thing to note..it is a 2003 server...i know that had certain limitations with visual studio not sure about with android?
Thanks in advance
In your php code
mysql_select_db($dbdb)
should be
mysql_select_db($dbdb,$connect) ;
Then add these lines
$result = mysql_query($query, $connect);
$num = mysql_num_rows($result);

making connection code dynamic

the connection to database via PHP is as:
<?php
$DbHost = 'localhost';
$DbName = 'root';
$DbUser = 'root';
$DbPwd = '';
$dbConn = mysql_connect ($DbHost, $DbUser, $DbPwd)
or die ('MySQL connect failed. ' .mysql_error());
mysql_select_db($DbName,$dbConn) or die('Cannot select database. ' . mysql_error());
?>
Here I've hardcoded my hostname, username and password. Everytime I take my project to another machine, I've to change it.
How can I make this connection code dynamic?
You can put machine specific information in a settings.php file and use
require_once "settings.php";
to get hold of the machine settings. Do this on all your machines and put the file in a specific place so your project(s) is/are able to find it.
When you move your project to another machine, it will use the corresponding settings.php file. (Assuming you are not copying this file as well ;) )

Can't get my PHP document to connect to my xampp server

I am very new to php, and am sorry if this question turns out to be too vague, but if there is any other information that you need from me let me know.
Anyway, basically what my problem is I have some simple php code that should call die() if it can't connect to the xampp server I have set up, however, even if I put in invalid info for the server or user it prints Successful. I really don't know where to go with this, so if anyone has any suggestions that would be great.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
if ( !mysql_connect($dbhost, $dbuser, $dbpass) )
{
die(mysql_error());
}
else
echo 'Succesful';
?>
Ok for some reason it was just not working with the root user, so I created a new user who required a password and apparently that connected like it was supposed to. Thanks everyone for the answers.
Your code is correct pistolpete333. For your root user issue, that your not able to connect with root user you can check below file for your phpmyadmin configuration
C:\wamp\apps\phpmyadmin3.5.1\config.inc.php.
In above file you can check your host, username and password of phpmyadmin.
I have found that when trying to connect php to an xampp server there are usually only a few things that could cause the issue you are having.
1. mySQL service is not running
2. no specific database is selected to try to access
3. user does not exist in phpmyadmin.
When you pick a user to connect php to mySQL with you should always make sure that a password is set, that is usually the thing people miss. You can use root if you create another instance of it in the user list and require a password, or you can add a password to the root user already in the list, but the best option is to create a new custom user that only has access to the database you want to access with your php and make sure it has a password required. (make sure any user you use can connect with localhost; root can by default and I think most newly created users can as well).
Below is the php I use for websites I design that need php to access a DB.
<?php
// This configures the variables for database access
$dbhost = 'localhost';
$dbuser = '????';
$dbpass = '????';
$dbname = '????';
// This will connect to the server and then the correct database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
This is working because you have specified valid username, host and password to mysql. If you specify wrong password, you get mysql error. This code works and connects to the mysql server though you have not selected any database.
Try this code instead
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link); //foo is your database name
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>

code to connect to my database in yahoo web hosting

I got to design a login and sign up form using yahoo webhosting.
I have designed the login page and the sign up page using html, but I have to write a .php module to save the entered fields in the "my database" table with table name "login".
I have created the "login" table with the fields in the signup form. But I am not able to connect to the data base thorugh phpMyAdmin. I have written the following code in .php file
<? php
$db_host = "localhost";
$db_username = "user123";
$db_pass = "password123";
$db_name = "my database";
#mysql_connect("db_host","$db_username","$db_pass") or die ("could not connect to my database");
#mysql_select_db("$db_name") or die("no database");
echo "Successful Connection";
?>
But I am getting 500 - Internal Server Error. Why?
I have to establish a connection and update the values entered in signup form by writing the php code for it.
You are using variables as parameters for your MySQL functions, and so you don't need (infact you shouldn't) double quotes there!
Change
#mysql_connect("db_host","$db_username","$db_pass") or die ("could not connect to my database");
#mysql_select_db("$db_name") or die("no database");
to
mysql_connect($db_host,$db_username,$db_pass) or die ("could not connect to my database");
mysql_select_db($db_name) or die("no database");

Can't select database table even though the code is right

I am trying to display a list of my vbulliten threads on a non-vbulliten portion of my site. However I can't select the vbulliten database:
<?php
$host = "localhost";
$user = "my username";
$pass = "my password";
$dbname = "tableprefix_forum";
mysql_connect($host, $user, $pass) or die ("Could not connect to database server.");
mysql_select_db($dbname) or die ("Could not select database.");
?>
I am substituting some things here in this example but all my credentials are correct including my db server username, password and forum db name. So what is the problem? Is it due to some internal security feature in vbulliten, does this system not allow you to connect to it's db if the page trying to connect to it is a non-vbulliten page?
Vbulletin has NO control over the permissions given by the server. But you do need to make sure that the user/pass you are using has been granted permission to access the database you are requesting.

Categories