I realise there are a lot of questions like this here, but none of them seem to help me. The problem is as follows:
I have a website with an SQL Database, and now I am developing an application that reads the data. I've read that the best way to do that is a web service, so I'm in the process of writing one now. First, I'm trying to get all the data to display on a webpage, for testing purposes. I have the following code now:
<?php
$servername = "localhost";
$username = "myusername";
$password = "mypassword";
$db = "mydatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $db, 3306);
// Check connection
if (!$conn){echo "Not connected";} else {echo "connected";}
$query = "SHOW TABLES FROM mydata";
$result = mysqli_query($query) or die(" but not executed" . mysqli_error());
?>
When I save this and go to mywebsite/test.php, it displays the following:
connected but not executed. That means that it connects right, but the mysqli_query() returns false. However, mysqli_error is empty.
This is the entire test.php file. Am I missing something?
"Am I missing something?"
Yes, db connection arguments to both mysqli_query() and mysqli_error().
RTM's
http://php.net/manual/en/mysqli.query.php
http://php.net/manual/en/mysqli.error.php
It's all in there.
Btw, this is a community wiki. No rep should come of this.
If that still doesn't work, then you may have to remove the port , 3306.
Your $query appears to say
SHOW TABLES FROM mydata
However, your database is actually called mydatabase, as demonstrated on the $db line.
Related
So I'm starting to learn how to use PHP to access MYSQL databases and after successfully connecting to the database. I want to select data from it. However I get the issue that the table doesn't exist. Yet, it exists when I check it in my phpmyadmin. I've checked spelling, capitalization, etc.. and nothing works. I've tried it with multiple databases and I get the same error. So I'm really confused as to whats going on because from the looks of it, there is NOTHING wrong with the code. I've tried running the SQL query in phpmyadmin just to verify that the query works.. and it does. I just get the error "Table 'test.blog_table' doesn't exist" with the code below
<?php
$host = "localhost";
$user = "root";
$password = "";
$database_in_use = "test";
$conn = new mysqli($host, $user, $password, $database_in_use);
if ($conn->connect_errno) {
echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
}
echo $conn->host_info . "<br>";
$sql = "SELECT * FROM blog_table";
$result = $conn->query($sql);
$result = $conn->query($sql) or die($conn->error);
$conn->close();
?>
So I'm just completely lost and have no idea whats going on with it.
Solved! I connected to the wrong database, which is why the tables were there, but weren't showing up. Since I was connecting to a different database and the one I created tables in didn't have the default port.
Probably you are missing the mysqli and the mysqlnd PHP extensions.
Also, I recommend you to use \PDO object to fetch queries to your DB instead of the mysqli driver, if you do it, you will be free to change in the future to a PostgreSQL DB for example anytime just changing the DSN in the constructor (you need for that the PDO and the pdo_whatever_db_driver (e-g.: pdo_mysql) extensions.
When my registration form tries to insert with user1 (which has been granted ALL PRIVILEGES) it works. But when I try to insert with user2 (which has also been granted ALL PRIVILEGES) it won't insert. I don't get any errors and have been troubleshooting for a couple of hours now and can't find a problem, better yet a solution.
User2 didn't have all privileges (like any sane person should do of course). When I tried granting it all privileges it still couldn't insert while user1 which has the same privileges can.
As I don't know where the problem is, I can only include the database connection code which seems to be right. If anything else is needed I'd gladly put it here.
$dbservername = "localhost";
$dbusername = "user1";
$dbpassword = "password1";
//$dbpassword = "password2"; (I keep this here to change users fast when troubleshooting and yes I change the username everytime)
$dbname = "database1";
$db = new mysqli($dbservername, $dbusername, $dbpassword, $dbname);
I expected user2 to insert into the database but it won't. I don't have any error messages.
Privileges:
EDIT:
I have tried running the code Martin gave and both users connect...
Please use the below code to give yourself error feedback:
$dbServerName = "localhost";
$dbUsername = "user1";
$dbPassword = "password1";
$dbName = "database1";
$mysqliDriver = new \mysqli_driver();
$mysqliDriver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
try {
$db = new \mysqli($dbServerName, $dbUsername, $dbPassword, $dbName);
}
catch(\mysqli_sql_exception | \Exception $em) { // format requires PHP 7.2
error_log(print_r($em,true)); // READ THIS!!!
die("failed. Check the error log!");
}
die("We are connected!");
This sets up a MySQLi Driver reporting mode which can give i) Exception reporting and ii) more informative/easier to access reasons for failure.
Please let us know (Update/Edit your question) what the error reporting catches when connecting with User2.
NOTE:
If you do not have PHP7.2 running replce that line with:
catch(\Exception $em)
NOTE:
Your pastebin code; you are creating the $db as a PHP object but are using it with static commands (mysqli_query($db, $sql) etc.) as if it was not an object (procedural). This is inconsistent and will give you issues.
Plese read here how to correctly structure your PHP / SQL interface
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 ".
I face with a strange problem yesterday. I have server running Debian with installed PHP 4.4.4-8 and mysql 5.5.9. That server serves a several websites.
For some reason randomly I get that error "Access denied for user 'www-data'#'localhost' (using password: NO)" when I try to load the webpage.
If I hit refresh the page loads normally, but afer several clicks that message appears again. Username which that page use to connect to mysql server is not www-data.
Does anyone has faced similar problem ?
www-data is the Debian user that runs apache and php. If you attempt a query when you don't have a valid connection, php/mysql will attempt to create a connection using <unix-user>#localhost with no password. This is where www-data#localhost (using password:NO) is coming from.
The most likely reason that this has started happening now (though it has been running fine for 2-years prior) is that your db load has increased to the point where some connections are unable to succeed (probably due to max_connections, or max_user_connections; though this can also result from other limits like memory, threads, etc). When this happens, your call to mysql_connect will emit an error message, and return FALSE. If you fail to detect this failure, then your next mysql call (probably mysql_query, or mysql_select_db) will attempt the connection to www-data#localhost -- thus causing the problem you're seeing.
I suggest enabling error reporting, and error display (as suggested by #DarkMantis) :
ini_set('error_reporting', E_ALL|E_STRICT);
ini_set('display_errors', 1);
Also, be sure that your call to mysql_connect is not preceded by a # sign; and make sure to check the return value. It should look something like this:
$cxn = mysql_connect('localhost','yourusername','yourpassword');
if( $cxn === FALSE ) { die('mysql connection error: '.mysql_error()); }
It sounds like the query that is causing the error happens when something specific is called. This could mean that when the query is called, you aren't connected to the database with the correct username/password.
Try to ensure that you are definatly connected, use or die(mysql_error()); at the end of all your query variables to debug them.
Also, use the following two lines at the top of your php file:
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
That will show you any little php errors that may occur within your class/file which you may not have picked up before.
If your still having a problem after this, please post your PHP code and I will take a look at it directly.
Thanks!
i faced the same problem.
The problem was in my config.php!
I simply changed the $dbserver from
"127.0.0.1" -> "localhost".
Now the connection works again!
For absent-minded people, this error may happen when mysql_query() is called after mysqli_connect(), when it should be mysqli_query().
Use password 'NO'
(MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Deactivating safe mode fixed it for me:
Notice: mysql_connect(): SQL safe mode in effect - ignoring host/user/password information in /var/www/html/test.php on line 4
For solve this problem. I had to change the connection script Using
(MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
hi im fairly new to cms made simple and ive stumbled upon a problem thats beyond me, my coad is
<?php
$dbh = 'localhost';
$dbu = 'root';
$dbp = '';
$connect = mysql_connect($dbh, $dbu, $dbp) or die ('Error connecting to mysql');
$yatzi = 'myposts';
mysql_select_db($yatzi);
echo "hello";
?>
and im using this through a user defined tag to import a php file, the proble is that everytime i load this page an error pops up saying:
string(61) "Smarty error: unable to read resource: "globalcontent:footer"" string(61) "Smarty error: unable to read resource: "globalcontent:footer""
and everything gets messedup, i seriously have no idead what is going on, can anybody please help me,, thnks...
If this connection is to the same db server the issue could be that youre overwriting the connection resource and thus your CMS cant pull anything form the db.
This would be because by default PHP will detect that you already have a connection open and return that one if they share the same parameters. You can override this behavior by forcing a new connection:
$connect = mysql_connect($dbh, $dbu, $dbp, true);
Then when using this server you need to make sure you always specify which link to use:
mysql_select_db($yatzi, $connect);
mysql_query($query, $connect);
// etc...
Ohter possible issues might be that you have the code in the worng place (like directly in a Smarty template file without the special php escape tags surrounding it), or that the problem isnt related to your code at all and something is up with your CMS installation or customization.
<?php
$dbh = 'localhost';
$dbu = 'root';
$dbp = '';
$yatzi = 'myposts';
$connect = mysqli_connect($dbh, $dbu, $dbp,$yatzi) or die ('Error connecting to mysql');
echo "hello";
?>