Son:
You have some stuff to learn.
Your php script executes on the server. The script may talk to the database. Typically the results of the script are 'echoed' or 'printed' in the form of a web page.
In the script you submitted, your php script creates a nice html webpage. Embedded in that page is some javascript.
Javascript lives in THE BROWSER. Php lives far away on the server. Javascript doesn't know about the server. It doesn't know about the database. It only knows about the browser.
You can load some javascrript variables into the web page you built using PHP. If you do this, javascript can access those variables -- without any server or database activity.
You can only access your php scripts and database by sending information to your server. Typically this is done by FORM submission or by LINKING to a particular script file. (There are other methods, but based on your code, those are way out of your league at the moment.)
By the way, your php code doesn't make a ton of sense...If you have been adapting existing code, I highly suggest reverting and being more precise about your desired changes.
Hope this helps.