john-jones
john-jones

Reputation: 7780

mysqli works when run directly but not when via js-ajax

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

Upvotes: 0

Views: 152

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94672

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

Upvotes: 1

M H
M H

Reputation: 2183

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.

Upvotes: 0

Related Questions