PHP does not connect to MySQL successfully

Asked 2 years ago, Updated 2 years ago, 70 views

I'm trying to connect to MySQL using PHP mysql_connect, but it doesn't work.

Here's the code.

<?php

  $connect=mysql_connect("hiroki_%", "hiroki", "");
  echo mysql_errno().": ".mysql_error()."<BR>";

  if($connect){
    echo1;
  } else{
    echo2;
  }

?>

Here's what you did.

>2005: Unknown MySQL server host 'hiroki_%' (20)
2

ご As you may know,

echo mysql_errno(). ": ".mysql_error(). "<BR>";

is the code that displays the most recent error.

Creating a database called phptest

I think it should have been created by users.

I think it should have been created.

Typically

$connect=mysql_connect("localhost", "root", "");

I think it's normal to connect with , but in this case, the result is

>1045: Access denied for user 'root' @'localhost' (using password: NO)
2

That's what happens.
I created and executed a new user named Hiroki because it would be about user authentication.

Please help me with the solution.

--Additional --

Set the password for localhost

<?php
$connect=mysql_connect("localhost", "root", "password");

echo mysql_errno().": ".mysql_error();

The results of the run are

 0: 

Yes, I was able to connect without any errors, but when I see the phpMyAdmin page,

#1045 - Access denied for user 'root' @'localhost' (using password: YES) 

and error statements, which prevented me from entering phpMyAdmin.

 An attempt to connect to the MySQL server was rejected.Verify that the config.inc.php host, username, and password match the information provided by the MySQL server administrator.

and so on.

I searched finder to edit the file config.inc.php.
About 14 config.inc.php files appear and I don't know which one to edit.

php mysql phpmyadmin

2022-09-30 17:07

2 Answers

As you can see in the error message, the server could not be found because the hostname was incorrect or the name resolution was not successfully be resolved.

In the first place, % cannot be used for the hostname, but does the specified hostname exist?

    The first argument of
  • mysql_connect() is the hostname of the destination.
  • The hostname specified by the
  • privilege is the hostname of the source.

This doesn't seem to be distinguished from the content of the question.

On the other hand, the function of mysql_* is deprecated, so it is recommended that you review it from there.


2022-09-30 17:07

First of all,
MySQL and PHP are both on the same server, aren't they?


I think Hiroki's host should be localhost.

After
I'm sorry just for my memory, but
In the past, MySQL was able to connect without root or path, but
Recently, I feel that I couldn't connect to the root without adding a password.
Even if it was localhost.

Reply to post
I set the password for root, so
I guess I can't put it in phpMysqlAdmin anymore.

Please refer to the site below.
The configuration file (config.inc.php) for phpMyAdmin is
Rewrite to the password set by root
You'll be able to connect.

http://php1st.com/435/

It's the location of config.inc.php, but
I think I'm starting Apache on MacOSX, but
I think it's in the phpMyAdmin folder published on Apache.


2022-09-30 17:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.