mysqli_connect did not work
i will try the make local database from csv File. but Sql did not connect local Mysql server , why? ----------------------------------------------------------------
I am new both to MariaDB and to this forum but this person's question describes my situation today (28 July 2016 around dawn UK time; that's right: I can't sleep) so I will try to give enough details of my situation because I am stuck and I hope that you can help me.
By the way I hope that the way I am adding to this enquiry/question is correct; I am presented with a form with boxes for the original question and answer which seems to invite me to edit both of them.
I set up a new Linux server to be a web host. I installed Ubuntu MATE, Apache, and PHP successfully and the server works. PHP works so I have scripts working including phpinfo().
I installed MariaDB following instructions on a website for that and I believe that I created a database name because following instructions I did this (using iphdb1 as database name):
MariaDB [(none)]> CREATE DATABASE iphdb1;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> USE iphdb1;
Database changed
I conclude from this that I have a working MariaDB installation and a (so far empty) database with name iphdb1 so I then tried to write a PHP script that would connect to this so far empty database for the first time and create the first table in it.
Now I was previously webmaster for a while of a site that used MySQL and had an existing database with a PHP code library, so I borrowed some basics from that, and (when I discovered yesterday that the mysql_ functions had been deprecated and renamed mysqli_) where, please note, the appended letter i before the underscore, I wrote this:
$DB_NAME='iphdb1';
$IPHDB=dbconnect();
function dbconnect(){
global $DB_USERNAME,$DB_PASSWORD,$DB_NAME;
$mydb=mysqli_connect('localhost', $DB_USERNAME, $DB_PASSWORD, $DB_NAME);
if (!$mydb){
echo 'Could not connect to database “'.$DB_NAME.'”. '.
'mysqli_error() was:<br/><pre> '.mysqli_error().'</pre><br/>';
echo "Error number: " . $mysqli->connect_errno .'<br/>'."\n";
echo "Error: " . $mysqli->connect_error .'<br/><br/>'."\n";
exit();
}
echo 'Connected successfully to database “iphdb1”.<br/>';
$mydb_result=mysqli_select_db($DB_NAME,$mydb);
if (!$db_result) {
echo 'Could not select database “'.$DB_NAME.'”. '.
'mysqli_error() was:<br/><pre> '.mysqli_error().'</pre><br/>';
exit();
}
return $mydb;
} When I invoked this (using require_once()) from a test script I got the following in the browser:
Could not connect to database “iphdb1”. mysqli_error() was:
Error number:
Error:
So the invocation of mysqli_connect() did not cause the script to crash with no output at all to the browser, as the attempt using now-deprecated mysql_connect() had done.
But the call to mysqli_error() and references to $mysqli->connect_errno and _error gave nothing.
Is this what I should expect here if I got the username and/or password wrong? Why doesn't MariaDB say "unknown username" or "wrong password" or something then?
What can I ask MariaDB at the shell window prompt MariaDB [(none)]> to tell it what username I am working under? Or how else can I establish what I have managed to set up by default as my username and password for this purpose in this PHP script?
Thanks.
I have searched for some sort of basic example of using PHP to access, create and update and read a MariaDB database on a system like mine and cannot find any hits at all. If I search for +MariaDB +mysqli_connect (using plus to insist hits contain both) the search (the big G) comes back with precisely zero results. Is nobody writing about using PHP functions to access a MariaDB database?!
Thanks again; puzzled.
Answer Answered by Ian Gilfillan in this comment.
Your code has some problems. Take a look at your web server error log, and then take a look at the PHP manual to fix this. For example, $mysqli
will be undefined because you connect in a procedural style, but then go into object-oriented style after that. See for example http://php.net/manual/en/mysqli.error.php