Ok, first, JavaScript cannot access a database on your server directly on its own. JavaScript is completely client side, which means that it only runs inside of the client user's browser. Therefore, it can really only control the browser's behavior, and manipulate the web page that the user sees. (Just imagine if the user's browser could access your database directly. That would be quite a security problem for anyone who wanted to exploit that with a few DELETE statements

)
So because of that, all JavaScript really can do is make a request to a file on your server (in your case, a .php file), to do the database access on its behalf. That php file can then return whatever result that you want your JavaScript code to receive. You achieve this in JavaScript with an XMLHttpRequest; more commonly known as an AJAX request.
To make your php page "return" any piece of data, all it has to do is echo/print it onto the page's output. The AJAX request receives everything that the php page outputs as the "response" from the php page. So if your php page echo's "5", then that "5" can be read by some JavaScript code, and saved into a JavaScript variable. You can then use that variable to write your redirect.
If you can, post up both the JavaScript and the php code that you have at this time. With that, I can direct you to exactly what you need to do, rather than trying to create an example while making assumptions as to how your code is working at this time.
-Greg