The mysqli_connect not working
I installed apache, php and mysql. Now when I execute the php code, the mysqli_connect()
is not working, neither it's showing the message in die.
$dbc=mysqli_connect(' ', ' ', ' ', ' ') or die('not connecting');开发者_JS百科
Now someone tell me what database username shall I pass to mysqli_connect
and what password. I don't remember I was asked for any username or password
and why isn't the message in die()
showing up?
My var_dump(function_exists('mysqli_connect'));
outputs bool(false).. if it has anything to do with it, how do I correct it.?
Looks like MySQLi extension is not installed. What does var_dump(function_exists('mysqli_connect'));
output?
Do you have error_reporting(E_ALL);
And ini_set('display_errors',1);
?
The problem could be somewhere else.
Also how do you know it's failing if it is not priting the message in Die()
?
for the host, if you are using "localhost:8889", try using "localhost", and in the mysqli_connect() put in '8889' (or whatever port you are using) as an argument after the other arguements.
worked for me.
eg:
mysqli_connect('localhost','root','root','dbname','8889');
If you pass no values, I think MySQL is using defaults from the settings ini. So maybe this happens too if you pass empty values. In that case the connection could actually be established, and the result won't 'die'. Best thing is to use var_dump
to check what $dbc
contains after the call and continue from there.
But anyway, there is no way, PHP is going to tell you which settings to use if you don't remember them. :)
If you just installed mysql, then there exists only the root
user, without a password (or blank one if you prefer). You are strongly encouraged to change that password AND create a new user and password for your application, who has only access to just one database, the one your application uses.
To change the root
password, you may do this:
$ mysql -u root -p
Enter password: [press enter]
mysql> use mysql;
mysql> UPDATE user SET `password`=PASSWORD('your_desired_password') WHERE username='root';
# this next bit is to create a database and a username for your web application
mysql> CREATE DATABASE your_application_name;
mysql> GRANT ALL ON your_application_name.* TO 'your_username'@'localhost' IDENTIFIED BY 'yourpassword';
Obviously change all your_*
values with correct ones.
For the reason why the die()
gets not executed, do what @yes123 and @binaryLV had said (I think both are right, the mysqli
is not installed, so it throws a E_FATAL_ERROR
upon calling mysqli_connect(...)
and as error_reporting
is disabled (or maybe display_errors
, or maybe both), you don't see that error.
//1 create a data base connection
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD);
if (!$con) {
die('mysqli connection failed: ' . mysql_error() );
}
//2 connect with a data base
$db_select = mysqli_select_db($con , DB_NAME);
if (!$db_select) {
die('data base selection failed: ' . mysql_error() );
}
//3 create query
$result = mysqli_query($con, "select * from subjects");
if (!$result) {
die('query not successed: ' . mysql_error() );
}
//4 use the returned data
while ($row = mysqli_fetch_array($result)) {
echo $row['menu_name'] . " " . $row['position'] . "<br>" ;
}
//5 close connection...
if (isset($con)) {
mysqli_close($con);
}
Run this code if you have any query then feel free to ask me.
First check your php version echo 'Current PHP version: ' . phpversion();exit; if it is above 5.5 and even not working then write script in your php file phpinfo(INFO_MODULES);exit; it show all about php and check Mysqli listed or not. if not then inform to our server admministrator or if you have access then go to phpini file and enable mysqli (remove semicolon from it)
Try this:
$dbc = @ mysql_connect('localhost', 'root', '') or exit('Not connecting.');
精彩评论