mysql permissions with 1and1 - php

Does anyone know to give my user account access to a table? I connect to the database fine but when I try to select the table it tells me Access denied for user 'dboxxxxx'#'%' to database.
I'm using basic php code
$con = mysqli_connect('xxxxxx.db.1and1.com', 'xxxxx', 'xxxxxx');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
else {echo 'success!';}
$selected = mysqli_select_db($con,'userTable');
if(!$selected){
die ('cannot use db: '.mysqli_error($con));
}
There is no permissions options on the 1and1 phpmyadmin that I can see unless I'm totally missing it. Anyone have any ideas?

First of all, you need to supply dboxxxxx as a username, not 'dboxxxxx'#'%'

Are you running this code localy or within your server? The 1und1 firewall blocks connections from the foreign networks

You're probably not making the connection to the database correctly.
Try the changes I made. Note that I do not know that you are using classes and functions.
UPDATE
$mysqli = new mysqli('xxxxx.db.1and1.com', 'xxxx', 'xxxx','xxxx');
if(mysqli_connect_errno ())
{
printf('Unable to connect to database ' . mysqli_connect_error());
}
else
{
$stmt = $mysqli->stmt_init();
if(!$stmt)
{
echo "init failed";
}
else
{
$cmd = "insert into `userTable` (`oauthtoken`,`oauthtokensec`,`userid`,screenname`) VALUES ('?','?','?','?')";
if($stmt->prepare($cmd))
{
$stmt->bind_param('1','1','1','1');
$stmt->execute();
echo $stmt->affected_rows . " Row(s) Inserted";
$stmt->close();
}
else
{
echo "Prepare Failed";
}
} $mysqli->close();
}

Related

Why this PHP Code won't add the data to the MySQL DB?

I am brand new in PHP / MySQL.
I got this code from
http://www.johnmorrisonline.com/how-to-insert-form-data-into-a-mysql-database-using-php/
(great tutorial by the way) and used it. The problem is that it runs smoothly (Got no errors) but it just won't add the new row to the database. I tried to simplify the code to figure it out. If i run the query on PHPMyAdmin it works ok (it adds a new row) but from browser (php script) won't.
I cant tell what's wrong.
I am using MAMP on my MBP computer.
Thanks so much
Rafa
Here's the code:
<?php
$mysqli = mysqli_init();
if (!$mysqli) {
die('mysqli_init failed');
}
if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
die('Setting MYSQLI_INIT_COMMAND failed');
}
if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
if (!$mysqli->real_connect('localhost', 'root', 'root', 'procedimientos')) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . $mysqli->host_info . "\n";
// Insert our data
$sql = "INSERT INTO PROCEDIMIENTOS (`RUT`) VALUES ('1587');";
$insert = $mysqli->query($sql);
// Print response from MySQL
if ( $insert ) {
echo "Success!";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
$mysqli->close();
?>
You have to add $mysqli->commit(); before $mysqli->close(); to persist your changes to the DB.
You have this on top of your code:
if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
die('Setting MYSQLI_INIT_COMMAND failed');
}
In other words, you're wrapping everything you do against MySQL server inside a transaction.
Your options are:
Do not start transactions automatically by default (and do it manually whenever you actually need them)
Commit the transaction on success

sql queries embeded in php code don't get executed while working with cpanel

I am new at cpanel and I encountered a problem. I have made a database and a database user for it in cpanel and successfully connected to them via my php code. In spite of that I have a successful connection to database but none of my queries run in the application code (while they run in the phpMyadmin!).
<?php
session_start();
$conn =new mysqli('localhost','myDBname','myDBpass','myDBuser');
if($conn)
echo "<script>alert('successful connection');</script>";
$rawresults ="SELECT * FROM `articles`";
$result = $conn->query($rawresults);
if($result->num_rows>0)
{
echo "<script>alert('dd')</script>";
$_SESSION["i"]=0;
while($results = $result->fetch_assoc())
{
setcookie("searchResult","yes");
$_SESSION["topic".$_SESSION["i"]]=$results['topic'];
$_SESSION["name".$_SESSION["i"]]=$results['fileName'];
$_SESSION["texts".$_SESSION["i"]]=$results['texts'];
$_SESSION["i"]++;
}
header('location:index.php');
}
else if($result->num_rows==0)
{
echo "<script>alert('cc')</script>";
setcookie("searchResult","yes");
header('location:index.php');
}
?>
The problem is that I permanently face: alert(cc)! While table 'articles'
contains lots of information and num_rows is a positive value.
I 'd like to mention again that connection to db has no problem and I get alert(successful connection).
Try this after connecting:
if ($conn->connect_error) {
die('Connect Error (' . $conn ->connect_errno . ') '
. $conn->connect_error);
}

Problem in displaying and connectivity with mysql in php

I am new in php, I created a page with mysql db connectivity but when the page is run it displays a blank page. If i write a echo statement before the connection statement then only echo statement is displayed and nothing else is displayed. Here is my code..
$con = mysql_connect('localhost','root','admin');
mysql_select_db('testdb',$con);
if ($con)
{
die('Connected to database!');
}
$sql = " INSERT INTO customer ([Name],[Website]) VALUES('$_POST[fname]','$_POST[lname]') ";
$result = mysql_query($sql, $con);
if(!$result)
{
echo mysql_error();
exit;
}
// close the connection
mysql_free_result($result);
mysql_close($con);
Anyone please help why this problem occurs and is there is anything wrong in the page.
Display Errors was off. I edit in the php.ini file 'display_errors' On. But still the connectivity issue is not resolved. It displays an fatal error at connectivity line statement.
fatal error: Call to undefined function mysql_connect()
$con = mysql_connect('localhost','root','admin');
mysql_select_db('testdb',$con);
if (!$con)
{
die('Not Connected to database!');
}
$sql = " INSERT INTO customer ([Name],[Website]) VALUES('$_POST[fname]','$_POST[lname]') ";
$result = mysql_query($sql, $con);
if(!$result)
{
echo mysql_error();
exit;
}
else
{
echo 'Query Success';
}
// close the connection
mysql_free_result($result);
mysql_close($con);
As you have
die('Connected to database!');
this will stop the script here, script written after it will not be executed , use instead
echo('Connected to database!');
[Name],[Website] should be replace with Name, website
your script dies everytime it success to connect to DB
change
if ($con)
{
die('Connected to database!');
}
to
if (!$con)
{
die('Connected to database!');
}

mysql not connect with php scripts?

<?php
$cons=mysql_connect("localhost","root","");
mysql_select_db("infogallery") or die('Not Connected to the data base:'.mysql_error());
?>
I write above code for connection with mysql but when i run this scripts..nothing display on the brouser...what can i do for the connection with mysql....
If nothing is displayed, then it means it succeeded. Add more code which queries the database and displays some results.
Don't connect as the root account. Create an account specifically for playing around with.
Once you've done that, modify your code as follows:
$cons = mysql_connect('localhost', 'username', 'password');
if ($cons === FALSE) {
die("Failed to connect to MySQL: " . mysql_error());
}
mysql_select_db(etc.....);
You don't check if the connection failed, then try to do a database operation on that potentially failed connection. The or die(...) you have will only show the error caused by the select attempt, and the error message from the failed connection will be lost.
I like to just do
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("infogallery") or die(mysql_error());
echo "So far, so good.";
How about something like the following:
<?php
try {
$cons = mysql_connect("localhost","username","password");
} catch ($e) {
die('Failed to connect to the database: ' . mysql_error());
}
try {
mysql_select_db("infogallery", $cons);
} catch ($e) {
die('Failed to select the database: ' . mysql_error());
}
?>

Trouble connecting to MySQL

I'm trying to connect to a mysql database with php and myadmin. I've tried a lot of codes I could find online, but I just can't put this thing to work...
Can anyone please tell me what I might be doing wrong?
this is the php script I am using:
<?php
$useremail = $_POST["useremail"];
$password = $_POST["password"];
if($useremail && $password){
// open database
$connect = mysql_connect("localhost", "carlos", "nenem");
if(!$connect){
die ("Not able to connected to the database: " .mysql_error());
}
// select database
$select_db = mysql_select_db("vergilioDB", $connect);
if(!$connect_db){
die("Not able to connect to the database: " .mysql_error());
}
mysql_close($connect);
} else {
die("Please enter useremail and password, or REGISTER if you are a new user!");
}
?>
Carefull, $select_db != $connect_db
The variable names are different, rewrite to:
$select_db = mysql_select_db("vergilioDB", $connect);
if(!$select_db){
die("Not able to connect to the database: " .mysql_error());
}
note that you are closing your MySQL connection regardless of anything else before you ever use it for anything..
mysql_close($connect);
You likely want to separate your connection logic from your login logic. You likely need the DB connection to validate the password no matter what and your DB connection should be the first thing you do to make sure you can even DO anything with a given page...

Categories