mysqli works when run directly but not when via js

2019-08-19 07:13发布

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楼-- · 2019-08-19 07:56

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.

查看更多
该账号已被封号
3楼-- · 2019-08-19 08:05

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

查看更多
登录 后发表回答