1

I have a PHP page index.php. In this page, I am creating a connection with a MySQL database (using mysqli), and executing some MySQL queries. I am opening the connection ($connection = new mysqli("localhost", "root", "");) in the start of index.php file, and closing the connection ($connection->close()) at the end of the index.php file.

Within the HTML <head> portion of index.php, I have included a JQuery script which makes an AJAX call to another PHP file load_more_data.php, which makes a few MySQL SELECT queries. This JQuery script is invoked on the click of button.

What I am currently doing is that in load_more_data.php file, I open a NEW connection with the same database (by calling $connection = new mysqli("localhost", "root", "");) and then execute the queries. But there is a problem: When I execute my queries by doing $connection->query($sql) from index.php, the run fine and the correct dataset is returned. BUT when I execute the same query from load_more_data.php, $connection->query($sql) returns FALSE.

QUESTIONS:

I have two questions:

  1. Is this the right approach? That is creating a new connection object in each file which needs to query the database? or is there a way to pass one connection object from server side (index.php) to client side (JQuery script) from where AJAX sends that connection object back to the server side (load_more_data.php)? How should I go about this thing?

  2. Why is $connection->query($sql) returning FALSE when called from load_more_data.php, when it returns the proper expected dataset fetched from database when called from index.php?

Solace
  • 8,612
  • 22
  • 95
  • 183
  • 3
    Could you share the relevant code, the one in your `load_more_data.php` file? If it's returning false, there should be an error waiting for you in `$connection->error`, so echo that out and you'll see. – Qirel Dec 10 '16 at 17:43
  • 5
    Neither PHP nor MySQL care if the request was made with AJAX or not. Your query probably contains an error. Post the code and add error checking to your code. – Charlotte Dunois Dec 10 '16 at 17:44

1 Answers1

1

To answer your two questions.

1.

PHP is a server side scripting language. JavaScript (this includes Ajax and JQuery) is a client side scripting language.

In order for you to use javascript to get value from a database, you usually have to go through the server, by using ajax, to connect to the database, since most databases don't allow external access, as a security procaution.

When you call the php file using ajax, it is necessary that you connect to the database in that file, as the file isn't being included into the file you call from, meaning that it requires a new connection in the file that you're calling from ajax. Otherwise you won't be able to access the data.

If you pass a connection from the server to the client, that would ultimately allow the client to access the database and exploit it, since the user would be able to see the data passed from server to client and to server again.

2.

As Charlotte Dunois says, it is of no significance whether you query your database through one language rather than another language. The ultimate reason behind the result you get from your query, is as following:

$connection->query($sql)

Returns false, when there is an error in the query, in this case being the value of $sql. Another cause, could be that you haven't connected and selected a database. The only way to find out what that error is, if you aren't notified of it, is to use mysqli_error

  • Thank you, my query `$sql` was correct. My error was `No database selected`. But thank you so much, as I have learnt from your explanation. – Solace Dec 10 '16 at 18:09
  • 1
    I will add that to the answer, but I did answer it indirectly through question one. however. –  Dec 10 '16 at 18:11