PHP Function Page

Undefined function mysql_connect() [duplicate]

I have ran aptitude install php5-mysql (and restarted MySQL/Apache 2), but I am still getting this error:

14 Answers 14

In case, you are using PHP7 already, the formerly deprecated functions mysql_* were removed entirely, so you should update your code using the PDO-functions or mysqli_* functions instead.

If that’s not possible, as a workaround, I created a small PHP include file, that recreates the old mysql_* functions with mysqli_*() -functions: fix_mysql.inc.php

Perfect solution for my case, I could not just upgrade yet and break all my existing code. However, the global $con variable in your file was a problem because the included file was not able to read the global variable of the parent class in which constructor I included it. However, I simply created a connection variable inside the functions of the included file where needed. Now my code works fine. Thank you.

This was my problem today when godaddy moved my server. Despite selecting php 5.6 (not 7.0+) and enabling mysql and mysqli options in cpanel, ‘mysql_connect’ was still undefined. It was still 2 hours of work to diagnose and fix, but your script saved me from making it 8-10 hours! Thank you.

I see that you tagged this with Ubuntu. Most likely the MySQL driver (and possibly MySQL) is not installed. Assuming you have SSH or terminal access and sudo permissions, log into the server and run this:

sudo apt-get install mysql-server mysql-client php5-mysql 

If the MySQL packages or the php5-mysql package are already installed, this will update them.

Since this answer still gets the occasional click I am going to update it to include PHP 7. PHP 7 requires a different package for MySQL so you will want to use a different argument for the apt-get command.

# Replace 7.4 with your version of PHP sudo apt-get install mysql-server mysql-common php7.4 php7.4-mysql 

And importantly, mysql_connect() has been deprecated since PHP v5.5.0. Refer the official documentation here: PHP: mysql_connect()

Well, this is your chance! It looks like PDO is ready; use that instead.

Try checking to see if the PHP MySQL extension module is being loaded:

If it’s not there, add the following to the php.ini file:

Is this a better way to connect to a database? I will look into it but I would really like to get this working first as it worked previously until I moved to a new server.

It is. mysql_* functions are dangerous and deprecated as of PHP 4.1. Check your Apache error logs and restart the server. Does that work?

I’m using xampp, after adding that line to php.ini I get the following message in apache error.log Unable to load dynamic library ‘\\xampp\\php\\ext\\php_mysql.dll

Читайте также:  Css text overflow heights

If someone came here with the problem of docker php official images, type below command inside the docker container.

$ docker-php-ext-install mysql mysqli pdo pdo_mysql 

For more information, please refer to the link above How to install more PHP extensions section(But it’s a bit difficult for me. ).

I was also stuck with the same problem of undefined MySQL_connect().I tried to make changes in PHP.ini file but it was giving me the same error. Then I came to this solution where I changed my code from depreciated php functions to new functions.

$con=mysqli_connect($host,$user,$password); mysqli_select_db($con,dbname); //To select the database session_start(); //To start the session $query=mysqli_query($con,your query); //made query after establishing connection with database. 

I hope this will help you . This solution is correctly working for me .

If you upgrade form old php you need to apt-get install php7.0-mysql

Run the page and search for mysql . If not found, run the following in the shell and restart the Apache server:

sudo apt-get install mysql-server mysql-client php5-mysql 

Also make sure you have all the following lines uncommented somewhere in your apache2.conf (or in your conf.d/php.ini) file, from

My guess is your PHP installation wasn’t compiled with MySQL support.

Check your configure command ( php -i | grep mysql ). You should see something like ‘—with-mysql=shared,/usr’ .

You can check for complete instructions at http://php.net/manual/en/mysql.installation.php. Although, I would rather go with the solution proposed by @wanovak.

Still, I think you need MySQL support in order to use PDO.

The question is tagged with ubuntu , but the solution of un-commenting the extension=mysqli.dll is specific to windows. I am confused here. anyways, first thing run and search for mysql* under Configuration heading. If you don’t see such a thing implies you have not installed or enabled php-mysql . So first install php-mysql

sudo apt get install php-mysql

This command will install php-mysql depending on the php you have already installed, so no worries about the version.

Then comes the unix specific solution, in the php.ini file un-comment the line

verify that msql.so is present in /usr/lib/php/ , ELSE

Then finally restart the apache and mysql services, and you should now see the mysql section under Configrations heading in phpinfo page

I was getting this error because the project I was working on was developed on php 5.6 and after install, the project was unable to run on php7.1.

Just for anyone that uses Vagrant with ubuntu/nginx, in the nginx directory(/etc/nginx/), there is a directory named «sites-available» which contains a file named like the url configured for the vagrant maschine. In my case was homestead.app. Within this file there is a line that says something like

 fastcgi_pass unix:/var/run/php/php7.1-fpm.sock; 

There you can change the php version to the desired for that particular site.

Googled this but wasnt really able to find a simple answer that said where to look and what to change.

Hope that this helps anyone. Thanks.

Источник

How to Fix “Call to Undefined Function” in PHP?

How to Fix Call to Undefined Function in PHP

The majority of new web developers see the fatal error “Call to undefined function” in PHP code, and they are unsure of the cause. If you’re one of them, then continue reading this article to the end.

Читайте также:  Java and mysql server

We encounter the uncaught error “ Call to undefined function ” when we define and call a user-defined function. There are numerous causes for this error, and in this post, we will explore them all and provide easy explanations to dispel all of your doubts.

However, before we go into the details of this article, you need first to comprehend what a function is and what we call it. So without further ado, let’s get started with the post.

Table of Contents

How Do Functions Work in PHP?

Similar to other programming languages, PHP has functions. A function is a separate code that processes additional input as a parameter before returning a value. In PHP, we have two types of functions Builtin functions and user-defined functions .

Builtin functions are the functions that PHP provides to us to use them. Actually, you seldom ever need to build your own function because there are already more than 1000 built-in library functions available for various purposes; all you need to do is call them as needed.

On the other hand, we can create our own functions, called user-defined functions . In the case of user-defined functions, there are two key aspects you need to understand:

Creating your own PHP function is pretty simple. Let’s say you want to create a PHP function that, when called, will display a brief message in your browser. The example below invokes the method printMessage() immediately after creating it.

The name of a function should begin with the keyword “ function ,” and all PHP code should be enclosed in “ ” brackets, as seen in the example below:

     // Calling a PHP Function printMessage(); ?> 

What Are the Reasons and Solutions For The “Call to Undefined Function” Fatal Error in PHP?

Following are the reasons for facing the “ Uncaught Error: Call to undefined function “:

  1. Misspell of function
  2. Not using this with the function name
  3. Use include or require properly
  4. Using dot (.) instead of object operator (->)

1. Misspell of Function Name

To prevent “ Call to undefined function “, always double-check the function name. Let’s look at a straightforward example to see what output the following code will return if the function name is misspelt:

 function myfunction()< $this->printMessage(); > > $myvar = new myclass(); $myvar->myfunctions(); ?>

In this example, we write myfunctions () in place of myfunction (), which causes this error, so it is always better to double-check the spelling of functions to avoid this error.

2. Not Using This with Function Name Within PHP Class

We face a “ call to an undefined function ” when we don’t use $this with the function or property name of the class. For example:

 function myfunction() < printMessage(); >> $myvar = new myclass(); $myvar->myfunction(); ?>

The $this keyword in PHP refers to the class’s current object. Using the object operator (->) , the $this keyword gives you access to the current object’s attributes and methods.

Only classes have access to the $this keyword. Beyond the class, it doesn’t exist. You’ll see an error if you try to use $this outside of a class.

Читайте также:  Get index string php

You only use the $ with this keyword when you want to access an object property. The property name is not used with the dollar sign ($).

We now rewrite the code that causes the abovementioned errors with this keyword and examines the results:

 function myfunction()< $this->printMessage(); > > $myvar = new myclass(); $myvar->myfunction(); ?>

3. Use Include or Require Properly

When we create a namespace and include it in another file, we often face “ Call to undefined function “. For example:

include myfunction.php   echo tempNamespace\printMessage(); ?>

Code of myfunction.php

To avoid this error, we have to write include or require statements correctly and in the proper place. Now we write the above code again using the appropriate include statement.

Code of myfunction.php

Most of the time, when the user doesn’t check the file name, writes the wrong namespace name, or doesn’t include the namespace correctly, then faces “ Call to undefined function ” in PHP.

4. Using Dot(.) Instead of Object Operator(->)

The PHP code syntax is different from other programming languages. So when you come from JS or any other Object Oriented language, we use the dot( . ) operator to call a method on an instance or access an instance property. But in PHP, we access the members of the provided object using the object operator ( -> ). For example:

 function myfunction()< $this->printMessage(); > > $myvar = new myclass(); // Use . operator in place of object operator -> $myvar.myfunction(); ?>

We can easily get rid of this error by just replacing the dot (.) with the object operator (->) . For example:

 function myfunction()< $this->printMessage(); > > $myvar = new myclass(); // Use . operator in place of object operator -> $myvar->myfunction(); ?>

Conclusion

Finally, you arrive at a point after finishing this reading when you can quickly get rid of the “ Call to undefined function “. We covered all the causes in this article and gave you all the solutions. In this circumstance, we provide you with straightforward examples to help you resolve this uncaught issue.

To summarise the article on “ How to fix call to undefined function in PHP “, always first search for the PHP file containing the function definition. Next, confirm the file’s existence and check to see if the page had the necessary (or included ) line for the file, as mentioned above. Ensure the absolute path in the require / include is accurate as well.

Double-check that the spelling of the required statement’s filename is correct. Use this keyword in class to refer to the same class function. Always check the syntax of your code; many times, users from different languages use the wrong operators.

Share this article with your fellow coders if you found it beneficial, and let us know in the comments below ⬇️ which solution you used to solve the uncaught error “Call to undefined function”.

Источник

Оцените статью