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

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.

Related

How to create a new mySQL database user from php

I was wondering if there was a way to create a new user for my MySQL Database? I'm trying to add basic log-in functionality that will allow the user to connect to database. If user has no account, then I want to create a new one. This is what I have so far:
<html>
<head>
<title>Test</title>
</head>
<body>
<?php
$username = "root";
$password = "root";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$selected = mysql_select_db("contactsDB",$dbhandle)
or die("Could not select examples");
echo "Found contacts DB";
?>
</body>
Everything connects properly, but now, instead of having to type in the username and password (root), I want user to type their username and password. Again, if they don't have one, then I want to create a new one.
You need atleast one admin account to create a user with less privilege.
Please refer to
create db and user mysql and set privileges php
which is a good reference.
This is perfectly possible, but one thing I would stay clear of is logging in as root to do these commands.
With that in mind a method similar to this may work.
mysql_connect('localhost', 'user', password);
mysql_query("CREATE USER 'username'#'localhost' IDENTIFIED BY 'password';");
mysql_query("GRANT ALL ON db1.* TO 'username'#'localhost'");
mysql_close();
If you have a dedicated database for these new users, you can easily assign it then.

mysql_connect(): Access denied for user 'adminryan'#'localhost' (using password: YES)

I'm very new to PHP and mySQL and I've been trying to create a logon via xampp with apache and mySql server. I keep on getting this error can someone explain what the error actually means? I've seen some people ask this question but the answer usually only pertains to their code especially. I'll provide my code as well. I've been watching this series of youtube tutorials if anyone is interested in what I have done http://www.youtube.com/watch?v=NuiTzSdGmKM . Hope someone can help thanks! My code below:
<?php
$username = "yolo";
$password = "swag";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
$selected = mysql_select_db("login", $dbhandle);
$myusername = $_POST['user'];
$mypassword = $_POST['pass'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$query = "SELECT * FROM logon WHERE Username='$myusername' and Password='$mypassword'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
mysql_close();
if($count==1){
echo 'It worked!';
}
?>
The error is about establishing the connection to a mySQL server.
(User 'adminryan' can't connect to server 'localhost' and a password was used trying to connect.
This can have several reasons, like:
user adminryan doesn't exist.
the provided password is wrong.
the provided servername is wrong.
user adminryan doesn't have the proper privileges.
etc...
your code says:
$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
so I wonder why you see the error given by you instead of "Could not connect to database".
I would expect mysql_connect would return FALSE and therefore die("Could not connect to database") would be executed. (And all code after die(...) wouldn't be executed.)
It has nothing to do with $query = ...
I can't, but I would vote down the comments of MuhammadAli and ArtisticPhoenix because they don't make any sense in relation to the error you provided.
And following your code this line won't be executed.
Do switch to PDO or at least mysqli though.
For the purposes of having a possible solution for others that encounter this error and confirm the username, password and database do exist. I was coming across the same error and when restructuring my requests I was able to get the desired result.
$username = 'username';
$password = 'password';
$db = mysql_connect('localhost', $username, $password);
mysql_select_db("database",$db);
In my instance I have the full username and database being used. So it seems to depend on your cpanel account username. So for example if my domain cpanel login was mydata, then my username would be mydata_username and my database would be mydata_database.
Now I don't know if that matters or not, I haven't tested it the other way around, I have had this error a few times and this is a solution that worked for me. I don't know why it worked as I'm no expert but it did and it's worth a try for others who are experiencing the same issue.
The only difference between this code and my earlier code which is similar to this error is my use of quotes. This code is using single quotes ', the earlier code used double quotes ".

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());
}
?>

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

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.

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");

Categories