0

I have a php script that places information in a db. When i run the script manually on the server all works as expected. But when I run that very script via javascript using ajax the php script fails at the following line:

$connection=new mysqli($servername,$username,$password,$dbname);

And the error i get is PHP Fatal error: Class 'mysqli' not found in /var/www/html/..

Which doesn't make any sense for I just ran that very script directly on the server with no problem.

I'm finding this error in /var/log/apache2/error.log.

Here's how my ajax call looks:

$.ajax({
    url:'db/insert.php',
    complete: function(response){
        //$('#output').html(response.responseText);
        alert('complete: '+response.responseText);
        },
    error: function(){
        //$('#output').html('Bummer: there was an error!');
        alert('error');
        },
    async:"false",
    type:"POST",
    data:{visit_report:'1'}
    });

Why is the script failing in this way?

This question is continued here: Enable mysqli in my in-webserver copy of php

2
  • 2
    did you check if mysqli is enabled in your in-webserver copy of PHP? command line and SAPI have different .ini files, which don't necessarily have the same modules enabled. Commented Jul 20, 2015 at 18:21
  • Post the page you are calling with ajax. Commented Jul 20, 2015 at 18:34

2 Answers 2

1

Find you php.ini file, be careful there may be 2 of them. You probably want the one in your apache/bin folder.

Look for

extension=php_mysqli.so

Make sure it does not have an # comment character at the front of it.

If you are not sure where it lives try a whereis php.ini

Sign up to request clarification or add additional context in comments.

8 Comments

wheres the apache/bin folder?
Sorry I dont know I am a windows user primarily. Did you try a whereis
i did whereis php.ini and it gives me a list of files that seems to be php executables.
Try this command to find it php -r "phpinfo();" | grep php.ini
Read this lornajane.net/posts/2012/managing-php-5-4-extensions-on-ubuntu you need to run a php5enmod mysqli
|
0

I have the same issue sometimes.

The issue is that when you call the page via ajax, all the server side includes and requires are dead. You have to explicitly call your db connection and make sure to include all your pages on the page you are calling via ajax so that they get created on that call.

1 Comment

I can't. Wherever you are creating your database connection, you need to create it again on any page you want to call with ajax. And make sure to include your needed includes on those pages too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.