Jump to content

hisoka

Recommended Posts

I am a newbie and I have some questions concerning MySQL php server database

As I know , MySQL server database is a place in the server where information like usernames , passwords , images , texts , telephone numbers ..... are stored (please correct me if I am wrong)There is this PHP function :DEFINE('DB_HOST', 'localhost');DEFINE('DB_USER', 'root');DEFINE('DB_PASS', '');the function define in php defines a constant . However these 3 lines are not clear to me and confuse me :DB_HOST is the name of the MySQL server database. Is not "localhost" the hostname of a server installed in a computer !! so why it is the name of MySQL server database?? why should we assign a name to a database like "DEFINE('DB_HOST', 'localhost');"?? is not MySQL database installed with the server and when the server wants to retrieve information from it . It connects , automatically to it?? what is the 'root' username ? is it the admin ( the person with the highest privilege who control all in the server)?? why should he define himself (a root) username and define a password in MySQL database??? for what purpose??DEFINE('DB_PASS', ''); why is the value of DB_PASS is not defined meanwhile the value of DB_USER is defined as 'root'???and on the light of all that what does this function do :mysql_connect($db_host, $db_user, $db_pass) ????

 

Link to comment
Share on other sites

To access MySQL database server you have to provide host, username, and password, by default set up these are usually 'localhost', 'root', and password setup by you, to acces a specific database you would specify database name.mysql_connect($db_host, $db_user, $db_pass) just sends the credentials to access mysql and then the database and its tables.The default 'root' privileges allow to access, create, delete, upadte and other numerous feature for tables etc.

Edited by dsonesuk
  • Like 1
Link to comment
Share on other sites

thank you . Could you please tell me 1) why I need a password and a username to connect to the database ? why I cannot connect to it without password and username?

 

2)"sends the credentials to access MySQL and then the database and its tables"

 

What is the difference between MySQL and the database ?

Link to comment
Share on other sites

You can login without username and password, but you would have to amend a config file, as by default this is how it is setup for minimum security.

 

MYSQL is a database management system, that uses SQL example "SELECT itemname FROM itemdb WHERE itemname = 'shoes'" UPPERCASE is SQL the rest table field names, database name, and search criteria. This requires the credentials to access a database and its tables.

  • Like 1
Link to comment
Share on other sites

BECAUSE if you don't! just about anyone (hackers), will be able to access private information that may or may not be on your database. if you want anyone to access, all you have to do is change the configuration to allow them to do so, but within a few days, it will have malware, spam, and undesirable links, appearing on your site, which i thought you would not want. but fine if you want that! just google allow access to mysql without username and password. Note: you HAVE to provide these if you use mysql on a website host, the host would usually set host, and username, AND maybe database name, so you should get use to using these, and understand how they work.

  • Like 1
Link to comment
Share on other sites

thank you

 

when a user, first, registers in a website , he must give his email , username and password . Are these data (email , username and password) stored in the MySQL server database ?? this is the first question . The second question :

 

if I am the owner of the website in which this user is log in , after his registration and then after giving his username and password, and I do not want him to access my website -so that when he puts his username and password he gets only "password or/and username are not valid - is this done by deleting his username and password from the MySQL database of my server ???

Link to comment
Share on other sites

1) yes the username, and password which would be encrypted, and email, usually would be stored in a database table called 'users' for instance.

 

2) Usually email fields should be set up in such a way that duplicates would not exist, you then could have a field named 'blocked' or similar, so when the user logs in again, the username, password would be checked and if the block value equals a blocked user value, he would NOT be able to access the site, and if he attempts to register again under the same email, because the email already exists he would be prevented from doing so, with a message 'Sorry a user with this email has already registered'.

Link to comment
Share on other sites

You already told me that " to access MySQL database server you have to provide host, username, and password" . I understood why I need to provide a username and a password . However , I cannot understand why I need to provide a host . So why?

 

the second question is :

 

when I delete the username and the password from the MySQL database of my server , normally , the next time the user put his username and password to log in , the server does not find the username and the password (because they are deleted from the database by the admin ) thus it alerts the user that " password or username are invalid " Am I right or am I wrong ??. Please correct me if I am wrong

 

the third question is about this

 

define(TABLE_NAME,'employee_list');define(EMPLOYEE_NAME,'employee');define(SALARY_NAME,'salary');

 

what does the three define function do ? do they create a table with three columns which has employee_list in the first column , employee in the second column and salary in the third column ???

Link to comment
Share on other sites

1) the host is required, to give access to your specific mysql database, which is usually from your host a ip address, or specific url address to a mysql server.Those define() functions, are usaully used only for connecting to myswl database as already discussed, I don't think they are used for creating/accessing DB, table or field, as you would use as already mentioned a SQL query statement, which would show fields table you wished to search through.

Link to comment
Share on other sites

However , I cannot understand why I need to provide a host . So why?

Why wouldn't it be required? When you tell PHP to connect to MySQL, PHP is supposed to read your mind and figure out which MySQL server you want to connect to? How about you tell it which server to connect to? Just because most MySQL servers are running on the same machine as the web server doesn't mean that all of them do.

what does the three define function do ?

The define function in PHP defines a PHP constant. It doesn't have anything to do with a database. The code might use those constants as placeholders for parts of the database structure, but the define function itself has nothing to do with a database.
Link to comment
Share on other sites

"Those define() functions, are usaully used only for connecting to myswl database as already discussed"

 

What I understood is that these :

 

DEFINE('DB_HOST', 'localhost');DEFINE('DB_USER', 'root');DEFINE('DB_PASS', '');

 

are used to create a password , username and host that are used later to connect to the database throughout this function :

 

MySQL_connect($db_host, $db_user, $db_pass)

 

we need to create a password , username and provide the host name before we can access our database

 

then once connected , we define a table that is we create a table with employee name , salary and employee list

 

throughout these function with their constants and value

 

define(TABLE_NAME,'employee_list');define(EMPLOYEE_NAME,'employee');define(SALARY_NAME,'salary');

 

Is not it???

 

please correct me if I am wrong

Link to comment
Share on other sites

"No, it doesn't create the password. The define() function creates a constant"

 

so , according to what you wrote to me , here : "DEFINE('DB_PASS', ''); " the define function creates a constant whose name is DB_PASS and value should be given by me .

 

and in "DEFINE('DB_USER', 'root');" the define() function creates a constant whose name is DB_USER and whose value is root

 

and in define(EMPLOYEE_NAME,'employee'); the define () function creates a constant whose name EMPLOYEE_NAME and value is employee

 

and so on ........................ This is what I am writing in my previous message . Is not it ?

Link to comment
Share on other sites

thank you

 

php mysqli_select_db() function : according to w3schools , The mysqli_select_db() function is used to change the default database for the connection.

 

syntax :

 

mysqli_select_db(connection,dbname);

 

both are required

 

connection Required. Specifies the MySQL connection to use dbname Required. Specifies the default database to be used

 

I cannot understand very well these two parameters and what is said concerning it in w3schools

 

for example :

 

 

 

1)the mysqli_select_db() function change the default database : it changes the name of the default database to another name ??

 

2)why change the name of the default database ? what is the purpose beyond changing it ?

 

3)what does it mean by :"Specifies the MySQL connection to use"??

 

4)Specifies the default database to be used : it means specifies the name of the default database to be used ?

 

5)The MySQL_select_db() function is used to change the default database for the connection : So why the default database is

 

required for the connection ??? I am confused

Link to comment
Share on other sites

1)the mysqli_select_db() function change the default database : it changes the name of the default database to another name ??

No, it doesn't make any changes on the database server. It tells MySQL which database PHP is trying to access. It selects the database (hence the name) for PHP to use. Any other database queries will go to that database that you select.

3)what does it mean by :"Specifies the MySQL connection to use"??

The mysqli_connect function creates and returns a connection to the database server. It's possible to write a PHP script that connects to multiple database servers, where each connection is a variable returned by mysqli_connect. You use the connection that you want to use when selecting or querying the database.

4)Specifies the default database to be used : it means specifies the name of the default database to be used ?

That sounds like the same thing. It specifies the database that you want to use on the database server, and it specifies the database by name.

5)The MySQL_select_db() function is used to change the default database for the connection : So why the default database isrequired for the connection ??? I am confused

If you never select a database then your SQL queries don't go anywhere. When you want to use a database you need to connect to the database server that you want to use, and then tell it which database you're going to use.
  • Like 1
Link to comment
Share on other sites

when we want to extract data from database , we use select .

 

For example : SELECT * CustomerName FROM customers WHERE CustomerName= Brock AND city= England

 

but 1) why we want to extract the username and the password from the database??? like in

 

SELECT * username FROM table WHERE username= Brock AND password= England

Link to comment
Share on other sites

It depends what your application does with the data. Maybe you want to store the username in the session, for example. Personally, when someone logs in to my application I prefer to select the record with that username and then validate the other information like password. That means I can have separate error messages for invalid username versus incorrect password.

  • Like 1
Link to comment
Share on other sites

concerning sessions and cookies , could you please explain these to me:$_COOKIE['logedin']$_SESSION['logedin']and what is the "logedin" inside the array ?what is the meaning of both php script?

http://www.w3schools.com/php/php_cookies.asphttp://www.w3schools.com/php/php_sessions.asp

  • Like 1
Link to comment
Share on other sites

I already read them before I posted my first message concerning sessions and cookies . I know that $_COOKIE and $_SESSION with the variable logedin inside an array are used to store "logedin" variable in the cookie and session . However I could not understand , in the context of authentication or login and http l not in the context of programming , what does

 

$_COOKIE['logedin']$_SESSION['logedin']

 

mean ? and what they do ? their role

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...