[go: up one dir, main page]

0% found this document useful (0 votes)
35 views7 pages

What Is MySQLi Connect

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

What Is MySQL_Connect?

As we’ve alluded above, a connection is required to access a MySQL database. And


the MySQL_Connect() function is provided solely for this purpose. Once you call the
function in your application, it opens a non-persistent connection to the specified
MySQL database.

If the connection to the database was successful or TRUE, the connection is returned,
and you can begin interacting with the database. On the other hand, if the connection
was unsuccessful or FALSE, you’ll get an error message informing you accordingly.

Syntax
Now, let’s take a look at the syntax for using the MySQL_Connect() function properly:

1 mysql_connect(hostname,username,password,databasename);

As can be observed from the above syntax, the function takes on


three parameters:

1. Hostname: The hostname indicates the server where the database


is located. This parameter is also known as servername. It’s an
optional parameter and can also include the specific port number
the MySQL database is listening (for example “hostname:3306”).

2. Username: The connection can also take on the username


parameter which indicates the user with access to the database.

3. Password: Next, the connection needs the user’s password,


which is passed on with this parameter.

4. Database Name Finally, you need to specify the name of the


database you are trying to connect.

Another critical point to remember is that the database


connection established with this function is non-persistent. And
this means that the database connection closes once the script
completes its task.
What Is Persistent Database Connection?

A persistent database connection is a SQL connection to a


database that does not close when the execution of the script
ends.

Whenever you establish a persistent database connection, there’s


a check to ensure that an identical persistent connection (i.e.,
one opened prior) does not exist. If one already exists, a new
connection will not open but rather the existing one used.

The MySQL_Connect() is a non-persistent database connection


function. If you want to establish a persistent database
connection, then you must use the MySQL_PConnect() function.

How PHP Works with MySQL_Connect()

As one of the most popular open-source databases in the world,


MySQL is often used as part of the LAMP stack. LAMP stands for
Linux Apache MySQL and PHP.

So it’s right to say that must of the users of MySQL database is


using it within their PHP web application. Of course, users of
other programming languages such as Python, Ruby, ASP.NET, etc.
equally use MySQL.

Now, from a PHP perspective, you can connect your PHP website or
application to a MySQL database using the PHP MySQL Connect
function accordingly.

\
PHP MySQL_Connect() Syntax

Here’s what the syntax looks like when establishing a database


connection using PHP MySQL_Connect() function:

1 <?php
2     $mysql = new mysqli("hostname", "username", "password", "databasename");
3 ?>

If you’re connecting to a local database that is on the same


server as the PHP website, then the hostname parameter will be
localhost.

Then you can simply provide the username, password and


databasename parameters to establish a database connection.

What Does MySQL Connect Return

Once you successfully (i.e., TRUE) connection to the MySQL


database using this function, the function returns MySQL link
identifier object if the connection was successful. However, if
the connection was unsuccessful (i.e., FALSE), the function
returns a failure error message which you catch and handle
appropriately.

MySQL_Connect vs MySQLI_Connect

With this in mind, you may have come across the MySQLI_Connect
function and rightfully wondering what’s the difference between
it and the MySQL_Connect function.

So let’s address this difference and discuss it a little bit


more.
What Is The Difference Between MySQL_Connect and MySQLI_Connect?

The MySQLI_Connect() function is an improved version of the


MySQL_Connect() function. The i stands for improved.
MySQLI_Connect is more secure and provides many features and
benefits which the previous one does not such as:

1. It supports prepared statements


2. Provides procedural and object-oriented interfaces
3. Transactions can be run through API
4. Support stored procedures
5. Enhanced security and improved debugging facilities

What Does MySQLI_Connect Return?

As with its predecessor, the MySQLI_Connect function returns a


link identifier object if the connection was established
successfully. A successful connection allows you to access and
interact with the database. If the connection failed, then it
will throw an error and return a failure message.

How To Change MySQL to MySQLI?

If you’re considering changing from MySQL to MySQLI the first and


easiest thing to do is replace the mysql in your code to mysqli.

So for example:

1. mysql_connect will become mysqli_connect


2. mysql_query will become mysqli_query
3. mysql_error will become mysqli_error

..and so on.
Of course, the above is a simplistic approach on how to change
MySQL to MySQLI. Furthermore, you must check all parameters of
the different function calls in your code to make sure any
differences are addressed.

Is MySQL_Connect Deprecated?

Yes, the creative minds at PHP.net have deprecated the


MySQL_Connect() function since PHP 5.5 and it’s no longer
supported as of PHP 7. So it’s highly recommended that everyone
starts using the new MySQLI_Connect() function.

With the deprecated MySQL_Connect() function, PHP now provides


the MySQLI_Connect() function as a replacement.

Why Is MySQL Connect Deprecated?

MySQL_Connect() was deprecated to provide an improved and more


secure way of establishing connections to a MySQL database. The
benefits of the new function are many as outlined above.

MySQL_Connect Deprecated Replacement

Upon deprecating the MySQL_Connect() function, PHP provided the


MySQLI_Connect() function as a reliable and improved replacement.
Other Considerations

Now, let’s look at other usage consideration for this function.

Dealing With Dreaded “WARNING MySQL_Connect” Error Message

When working with MySQL database, you’ll eventually encounter the


warning mysql_connect error message at one point or the other.
However, do not be alarmed as this is merely telling you that
there was an issue with your connection call, and it didn’t go
successfully.

Therefore, first of all, check to make sure that your connection


parameters are correct. You can do this by making sure you’re
connecting to the right database by looking at the hostname
specified. In most case, you should use localhost as the
hostname.

Then you need to ensure that the databasename provided as well as


username and password are accurate. Also, make sure that the user
you’re connecting with has the appropriate privileges to the
database.

Handling “MySQL_Connect Not Working” Situations

Whenever you encounter the mysql_connect not working error


message, the first thing you should do it to check which PHP
version you’re working with.

If you’re working with PHP 7, then remember that the


MySQL_Connect() function is no longer supported in PHP 7. And in
such instance, you need to use the MySQLI_Connect() function
instead.
Working with MySQL_Connect on LocalHost

Most times, you’ll be working on a local environment (i.e., your


computer) and need to be able to connect to a MySQL database to
test things out.

You need to specify your hostname as localhost in your connection


parameters. And if you’re trying to figure out how to connect to
MySQL on Windows then you use the IP address 127.0.0.1 in place
of the localhost parameter value.

And remember, as we’ve mentioned above, you may also need to


indicate the port number in your MySQL_Connect() function.

Final Thoughts

In conclusion, we hope that this article has added more to your


knowledge of this essential MySQL function and how to best work
it. Feel free to read more about the function online and let us
know what you think.

U mod_process (installer) promeniti sql u sqli connect!

You might also like