The short version of the story is that when I import MySQLdb in a script that I run from the browser, I always get 500 Internal Server Error.
The longer version:
Trying to learn web programming concepts. So far I've done a lot of work with PHP and MySQL using MAMP. I have created a big Javascript/PHP app and a big MySQL db to go with it. Now I'd like to learn Python, and I'd like to start with a familiar environment, namely, my MAMP setup and the db I've created.
I run my PHP scripts by entering localhost:8888/the-script.php in my browser. Now I have python scripts that run the same way, so I know python is working in my MAMP environment. What I haven't figured out yet is how to get my python scripts to talk to the MySQL db.
I installed Django, and I got it to see my db, but it wants to run its own server. I don't want that. I want my python scripts to run on the same server as my PHP scripts (I suppose I could try to get my PHP scripts to run on the Django server, but I'd rather consider that a last resort, to be taken only in desperation).
I've tried installing various packages with pip and macports, and I can run python from the command line and import MySQLdb, and it works fine. I've been all over stackoverflow and tried many of the applicable suggestions. I found a straightforward set of installation instructions that I thought might be the answer. But if I import MySQLdb in a script that I run from the browser, I always get 500 Internal Server Error.
I was hoping to learn python without first having to get a PhD in a lot of other areas like how Apache works internally, rebuilding MAMP and MySQL from the source code, etc. I realize that learning web programming concepts will eventually include those things, but I'm not ready for that. Right now I just want to learn python.
Is there a simple way to get a python script to run in the same environment as my PHP scripts, and interact with the same MySQL db that I've created for use with my PHP scripts?
Related
Introduction
Before getting started with the actual question, I’d like to make a notice of being an extreme beginner to the world of web sockets using PHP. As a matter of fact, I’m still trying to grasp the very basics of it. I do however have lots of experience with sockets overall, from languages such as Java.
Upon doing my research, I stumbled upon the tutorial from PHP builder, whereas I think myself to having learned how to set socket connections up using this simple library.
Problem
Unto the problem we go. For a development server, I have chosen to go with XAMPP—it’ss sort of stayed with me from my Windows days, many years ago. At this very moment, I am using the terminal UI for XAMPP in Ubuntu—meaning, I launch the XAMPP server and then do a killall should I ever wish to close it.
In the tutorial mentioned above, you enter this information into your PHP document where you keep your server socket:
$server = new Server('192.168.0.8', '8000');
The Server class being a class extended by the WebSocketServer provided in the aforementioned library. You’re then supposed to run the PHP server socket using (with the dollar sign symbolizing terminal input):
$ php -q serversocket.php
It was suggested somewhere else on Stackoverflow that you run the server socket with the -f flag instead, but both of them—instead of starting the server socket—prints out the file contents in the terminal.
The PHP I’m using (to attempt running the server socket) is the one you get from running (on Ubuntu):
$ sudo apt-get install php
I’d assume this to be the regular version of PHP.
I’m honestly probably just being a huge moron, but truly do need help to solve this problem. Have I missed something?
The problem that was occurring at the time was the PHP code being displayed as raw data in the terminal whenever run. I was using short opening tags at the time <? and did have them enabled in the configuration.
I did however, resolve the issue, by rewriting the code to use the full opening PHP tags <?php. Perhaps there was more things to configure, maybe the version of PHP I was using was broken. Who knows?
I recently started PHP (I am a beginner) and I wanted to create a form with HTML to add numbers together. However, when I send queries to PHP it shows my source code (because it is not a server - file is run locally). I was wondering would it be possible to preview my PHP's code result on a website of some sort or even better on my computer? Do you know any services or ways to accomplish this, preferably free of charge?
To run php files online, go to Write Code Online.
As long as you can install programs on your computer, you should install a web server to run php files locally. To run any basic app, especially if it uses forms, you will use multiple php files and the testing will be too complex to run online. If you are using windows, install wamp. For mac, try mamp.
you seriously require some help :-)
First, PHP is a server side scripting language, so it requires server (like Apache, which you can install using the wamp or xampp installers) Try to search google on how to install them on local computer.
Second, their are some online PHP editors, like http://www.compileonline.com/execute_php_online.php or writecodeonline .com and many others. It may not replace the server but helps to write the PHP code and execute it for the sake of practising.
Also, it is better if you read some tutorials about PHP. One tutorial I can point you to is the http://www.w3schools.com/php/php_form_complete.asp which is related to question.
I'm a bit new to php so I've never dealt with test servers before, but with the release of Google's Polymer project I now need a test server in order to go through their tutorial, so here it goes!
I use Intellij for all of my web coding and am having trouble trying to get a test server up and running. I'm on OSX and have downloaded things such as MAMP but can't seem to get the test server actually.. running with Intellij. Or know how to view my website on the test server with Intellij. I've looked up so many guides but just can't seem to figure it out.
Any help? Even a link to something useful? Please!
Thank you for any help!
You can start up a simple HTTP server for debugging purposes from any directory, if you have Python installed. Run:
$ python -m SimpleHTTPServer 8000
Now you can access the directory as a web site directory using:
http://localhost:8000
You'll need better alternatives as you explore more advanced stuff, but this works just fine as a starting point.
I have several parts to this problem. First, I've got a website and I'm trying to import data from another system, but the import could take several minutes. Meanwhile I'd like to let the user go on and continue working on the website, running the import in the background.
I'm doing my dev on a local WAMP server, but the test and production environments are Linux.
I've found several answers on here, some which suggest using pcntl_fork(), others suggesting running a command-line PHP script and others suggesting Ajax.
My problems with each of these approaches:
The pcntl_fork() spins up another instance of apache, which isn't what I want.
Ajax seems to lock the website until the import completes.
I'm trying to run a single FUNCTION rather than an entire script, so I don't believe the command-line will work.
Anyone have any ideas how to run this exclusively in the background? Also, any way to check and see if the process is complete?
I am able to easily call a python script from php using system(), although there are several options. They all work fine, except they all fail. Through trial and error I have narrowed it down to it failing on
import MySQLdb
I am not too familiar with php, but I am using it in a pinch. I understand while there could be reasons why such a restriction would be in place, but this will be on a local server, used in house, and the information in the mysql db is backed up and not to critical. Meaning such a restriction can be reasonably ignored.
But how to allow php to call a python script that imports mysql? I am on a Linux machine (centOs) if that is relevant.
The Apache user (www-data in your case) has a somewhat restricted environment. Check where the Python MySQLdb package is installed and edit the Apache user's env (cf Apache manual and your distrib's one about this) so it has a usable Python environment with the right PYTHONPATH etc.