mysql_connect() on localhost - php

I am trying to implement a little 'intranet' on my home network, but I am an apache/mysql configuration noob...
Running fedora 10, and have apache, mysql, and php set up as well as i know how. However, when I try using a standard form with POST, the php script seems to error out on the mysql_connect(...); line. I don't get an error message, in fact the browser page is blank, even in the view source window. Here is what I have:
$conn = mysql_connect("localhost:1186", "user", "password");
...
I have added the username and password in the mysqladmin tool, and i've tried "localhost" and "localhost:1186", as I saw the 1186 port referenced in my my.cnf file.
Obviously I have something configured wrong, any ideas?

The resulting page is blank when there's an error. One of the more common errors to make in your setup is to forget to install either the php-mysql or mysql-server packages. Verify that they're installed, and if not install either or both and then restart the httpd service, and start the mysql service if not running.

the default for a fresh mysql install would be
$conn = mysql_connect("localhost:3306", "root", "");
or just
$conn = mysql_connect("localhost", "root", "");
This is good for testing purposes but leaving the root account unprotected is a bad habit.

Related

WampServer, mysqli_connect (HY000/1045) Access denied for user

I'm using wampserver 3.2.0 and mysql 8.0.18.
I already reinstalled the wampserver and started everything from the begining after many tries, so now with fresh reinstall and clear datas this is what i did:
Set password for the root "pass123", after this i created a database (testdb) at wampserver>mysql>mysql console (i was logged in as root), then filled it up with the data i needed. Checked the user accounts/privileges, and the database privileges, i have all the privileges everywhere as root, but still i get this error message: screenshot about the problem
my code:
<?php
$ser="localhost";
$user="root";
$pass="pass123";
$db="testdb";
$connect = mysqli_connect($ser ,$user ,$pass ,$db) or die("Connection Failed");
echo "Connected!"
?>
Thanks in advance! (i'm new to php)
I solved the problem by specifying the exact port number used by mySQL.
The standard port is 3308, so just change the following line:
$ser="localhost:3308";
Not the actual SOLUTION, but, it works fine. Easy.
If you have the same problem, DO NOT use WAMP, use XAMPP instead.
With the same settings, it works.

How do I configure MySQLi to recognize mysqli commands in PHP?

Currently, I'm trying to test connection to a database from one machine to another. One machine has MySQL installed on it and has a database set up and running, and I want to have another machine connect to it using mysqli in PHP. Both machines are Ubuntu, and I'm using PHP 5.6. I installed the mysql client on the machine I want to use to connect to the database, and when I try and connect to the database in php using the following code:
<?php
$mysqli = new mysqli('hostname', 'username', 'password', 'dbname');
if($mysqli->connect_errno) {
echo "Failed to connect to MySQL database.";
exit();
}
?>
it errors out, and shows a "This page isn't working" error page with a HTTP Error 500. I've tried the following:
sudo apt-get install php-mysqli (Got the message that it was installed and at standard version, I assume this is because I installed the mysql client)
phpenmod mysqli (This didn't change anything)
I'm not sure what else I should try. I believe I have PHP 5.6 and PHP 7.1 both installed, will this cause an issue? What steps can you recommend to troubleshoot and fix the issue of mysqli not working?
First, I would take a look at the error log on the machine you are trying to connect from. There should be some explanation for the 500 error.
Second, I believe you need to install the mysqlnd PHP extension on the machine you are connection from. The machine that is hosting the database is just doing that. So, you don't need to worry about having PHP install there.

PHP file can't access MySQL database after changing server

So I have a PHP application running well on my local server, powered by Zend Server. After I uploaded it to the ubuntu server, it stops working, except for two php file, the index.php and register.php, which handles the user registration part. When I say working, it means I can view the page as in my local server but the internal process is not functioning. For example, when I try to register a user, it should tell me whether it is successful or not. Instead, it is a just a blank page.
I rule out the directory problem. All the path are relative and they are working fine. I also rule out the MySQL database problem as I can access the database and do everything with it. So I wrote the following script to test where php can access mySQL (the MySQL database is set up in the same host). So I wrote the following script:
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
require_once("config/connect_database_viewer.php");
echo $db_hostname;
echo "<br>";
echo "1";
echo $db_username;
$sql = "CREATE TABLE writer (UPC VARCHAR(15)) ENGINE MyISAM";
if ($db_server->query($sql)) {
echo "the database works";
} else {
echo "so it didn't even reach the server";
}
Fatal error: Fatal error: Class 'mysqli' not found in /var/www/viewer/angelo/config/connect_database_viewer.php on line 3.
But it doesn't make sense to me as I tried to install php5-mysql by this command:
sudo apt-get install php5-mysql
it comes back to me saying php5-mysql is already the newest version. And I checked the ubuntu page for the version, the php5-mysql module should include a mysqli extension. The php module is enabled too! Here is my connect_database_viewer.php file:
<?php
$db_hostname = '127.0.0.1';
$db_database = 'viewer';
$db_username = 'juvo1';
$db_password = 'juvo1';
$db_server = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if ($db_server->connect_error) die($db_server->connect_error)
?>
You need to check the error log. It'll tell you where the error happened in your PHP script. Try looking in
/var/log/httpd/error.log
and see what it says.
If only the first line of your test script is printed, your include script creates an error. The reason can be that your include fails (maybe due to rights problem) or something inside the script. I would include the
ini_set("display_errors", 1);
error_reporting(E_ALL);
to get the errors in the browser if your console (= error log?) does not mention anything else.
It appears that there are two problem in the process. The first one is that there are no mysqli module built-in for my version of php. People can install the php mysqli module with this command:
sudo apt-get install php5-mysqlnd
This stops the white page from showing up. But there is still warning about trying to connect via tcp. So I change the database conf php file from 127.0.0.1 to localhost. For more information, please refer to this link: mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2).

Strange behavior of mysql_connect in PHP

This is how a connection is created to MySQL server at localhost
$Connection = mysql_connect("localhost","root","");
But MySQL seems ignoring my username and password, even if I create the connection with some nonsense username & password:
$Connection = mysql_connect("localhost","nonsense-username","");
Both cases give me the $Connection as "resource(39) of type (mysql link)". And this only happens when password is blank. Is it a default behaviour of MySQL to accept any username when password is blank?
But it is supposed to have $Connection equal 'false' when such 'nonsense-username' given. Anything wrong?
I'm going to assume that you've setup the MySQL server on your own computer, without configuring users, permissions or things like that. If that's the case, then the cause might be SQL Safe Mode. If in your php.ini file, sql.safe_mode is set to 1, then PHP will substitute any arguments you pass to mysql_connect() with their defaults. That would certainly explain this behavior. Try looking at phpinfo() to see if that's the case.
It seems you have not restarted the webserver after install of php-mysql. Please restart the webserver it should work.
I spent two hours debugging that and the simple solution was restart

PHP mysql_connect cannot resolve Host name when sessions are enabled

Crazy right? Sample code:
<?php
session_start();
$hostname="samplehost";
$username="sampleuser";
$password="samplepass";
$dbname="sampledb";
$link = mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($dbname, $link);
?>
Will throw Unknown MySQL server host 'samplehost' (2). If we remove the session_start() or just do a session_destroy() before the mysql_connect(), it works correctly. Basically, if we have a session open, its like mysql_connect wont resolve the host name. The host name we use for the server is correctly added to /etc/hosts.
It's a production server running PHP 5.3.2-1ubuntu4.7 - just started happening today. Anyone run into this?
edit: should mention, with or without sessions, we can specify the IP of the DB server, and it works correctly.
If someone comes across this - it turns out to be a an issue with the amount of Virtual Hosts we had in Apache. Appears when you start hitting the limit on file descriptors, you get some odd symptoms - it was finally diagnosed when we started getting a 'Too many open files' error in PHP.
It was a legacy setup in how we were automatically generating vhosts for new domains. Better managed that process, reduce the amount of vhosts and the issues went away.
Why don't you just connect first, then start the session?

Categories