I was wondering how to link a SQL database on my PHP code.
I am using a database on my local server that I set up and I have already created a database, I tried writing a PHP script with ++notepad, but when I clicked on the script, it opened the browser and displayed a blank page. What am I doing wrong? I created my databases using phpMyAdmin.
You need to be running a web server locally which supports PHP (like Apache). It it the web server which does the PHP processing. At the same time you will have to run a MySQL (or any other) database engine too, so your PHP code can connect and query it. (I'm assuming you've already installed PHP.)
I would recommend you install XAMPP which includes Apache, PHP, MySQL and a bunch of other stuff. It one of "Apache friends". Once you install XAMPP you would have a folder htdocs in the xampp folder. Create a folder for you project myproject and place your PHP script script.php in there. Now if MySQL and Apache are running following should behave as you expect.
http://127.0.0.1/myproject/script.php
For the tutorial, to begin with you can use W3Schools. Brief and to the point. They have both a PHP tutorial and a SQL tutorial.
This is a very good book to start with:
PHP and MySQL
Web Development
Luke Welling and Laura Thomson
You have to play with php/sql code practically. Only reading cant help!
go for w3shools as mentioned above :)
start with simple code and then go to the complexity. all the best.. :)
There are multiple ways on how to do that.
One of the most modern ways to do that, is to use PDO to connect to the DB with PHP.
To run PHP on your PC(which i assume is your workstation), you need the supporting software for it, for php to run you need the PHP software installed, and likewise if you want to run the server software you need apache, however you can go for a single install package which will install almost everything for you to run your php script, download and install the wamp.
to install wamp which stands for (windows, apache, mysql, PHP) if you are running windows, and lamp for linux or mamp for mac, all of which are free softwares, you can download wamp from the following link. http://www.wampserver.com/en/download.php that is the first step which you need to do.
step 2: once wamp is installed the default directory of installation is C:/wamp (i reccomend do not change the location of the installation), you need to start the wamp server by just opening or double clicking the software which you just installed.
step3: navigate to the default directory where you should be storing your PHP files and that is C:/wamp/www note that you should be keeping all your PHP files here apart from this location if you store it anywhere else it wont simply run at all, and that it should and must have the .php extension.
step4: create or save the php files in this location(c:/wamp/www) i for example will assume you have created a file test.php in the www folder. now to access this file open your web browser and type http://localhost/test.php there you go, you will have the access to your PHP file (Note: PHP files cannot be run by simply double clicking the file, you have to follow this procedure in order to run the PHP files)
step5: open the test.php in any of your text editor like notepad, or i recommend use an IDE (Netbeans is free and worth a try), and write your code in the following format.
<?php
your PHP code should go here.
?>
when i started writing the code i started by following a nice tutorial which i found in zend.com, it has one of the best examples, and moreover the tutorial is meant for the beginners. hope this helps you..
http://devzone.zend.com/tag/PHP101
Related
Could anyone please tell me how to run a php file locally on my system.
Currently I am using a server to run files.
I know both php & Apache to be installed.
I need to see out put of this program, for example:
<?php
$a=5;
$b=10;
$c=$a+$b;
print $c;
?>
Can you please tell how I can run these files of if I need anything more.
php have a easy way to run a light server:
first cd into php file directory, then
php -S 127.0.0.1:8000
then you can run php
You have to run a web server (e.g. Apache) and browse to your localhost, mostly likely on port 80.
What you really ought to do is install an all-in-one package like XAMPP, it bundles Apache, MySQL PHP, and Perl (if you were so inclined) as well as a few other tools that work with Apache and MySQL - plus it's cross platform (that's what the 'X' in 'XAMPP' stands for).
Once you install XAMPP (and there is an installer, so it shouldn't be hard) open up the control panel for XAMPP and then click the "Start" button next to Apache - note that on applications that require a database, you'll also need to start MySQL (and you'll be able to interface with it through phpMyAdmin). Once you've started Apache, you can browse to http://localhost.
Again, regardless of whether or not you choose XAMPP (which I would recommend), you should just have to start Apache.
In short:
Install WAMP
Put this file to C:\wamp\www\ProjectName\filename.php
Go to browser: http://localhost/ProjectName/filename.php
I just put the content in the question in a file called test.php and ran php test.php.
(In the folder where the test.php is.)
$ php foo.php
15
If you have apache running, put your file in server folder for html files and then call it from web-browser (Like http://localhost/myfile.php ).
3 easy steps to run your PHP program is:
The easiest way is to install MAMP!
Do a 2-minute setup of MAMP.
Open the localhost server in your browser at the created port to see your program up and runing!
I would like to run PHP along with MYSql in VS Code as I would run using 'xammp'. I came across some extensions to use like IIS-Express and vscode-database but don't know how to configure and link them together.And which PHP would be suitable i.e. Thread safe or non Thread Safe.
PS: I am completely new to learn PHP and VS Code and want to use VS Code for every language I learn :)
IIS Express is an extension that will basically run your project folder as a website. It's an extremely handy tool that I couldn't live without. Usage is simple, and noted below. I should also point out that IIS Express is only available for Windows and will not work on Linux or OSX.
File > Open Folder
Select the folder with your PHP and HTML within
Install the IIS Extension from the extension menu. (Or with the command palette using "ext install iis-express")
Open the command palette (CTRL + SHIFT + P)
Type "IIS Express: Run Website"
This starts the web server and will open a browser and connect to your website for testing.
To stop the server, simply type the command a second time.
More information can be found here.
After looking around, it seems vscode-database does not host an actual database, but rather, gives you a way to test your queries in the editor. The information page for vscode-database doesn't make that very clear, though. So for MySQL functionality, you may want to look at running MySQL in a "portable mode". Instructions for this can be found here.
After getting that setup, you would just connect to it in php like any other MySQL database.
I download xampp 1.7.7 and it has PHP and MySQL. I bought a book named HeadFirst PHP MySQL. My problem is that I can't run the examples from the book.
The example files consist of 2 files: one HTML, and one PHP file. The example is like this you fill the form from HTML file then send it to a web server then the php will handle the HTML file then return it to you.
When I filled the form then I click the submit button, it didn't return what it was supposed to return. What it returns is the actual PHP code.
Do I need to transfer all the files from the book to a webhost so that I cant try the examples I said? How can I run it locally?
UPDATE!!
I tried what anselm said now it says OBJECT not found... url not found then the 404 error.
Ensure that you're accessing the html file through http://localhost/file.html instead of c:\etc etc.html
if you are developing on windows i really recommend using wamp it really easy to use and include the main stuck which is php, mysql, apache it really easy to install and automatically configures everything you then can find www folder in wamp folder on your c drive where you have to drop you php files
http://www.wampserver.com/en/
Your PHP code isn't being parsed by PHP. Either the Apache config is wrong (unlikely since you installed XAMPP, but you can check by following the steps at http://www.php.net/manual/en/install.windows.apache2.php), or your files aren't really named .php.
In Windows, make sure you have file extension viewing turned on, and ensure the file extensions really are .php.
In your XAMPP Control Panel, make sure that Apache started.
I've just started reading about php, it needs me to install php, apache and MySql to run any php script. can any one suggest me a simplest method to install php, apache and MySql so that i can sun those php script.
I've tried the zip files from php.net, Are those required to run the script offline for practicing the script? what do i do with them? i felt the things given on the same site a bit complex, and was unable to run the script. please help.
I'm assuming you're using Windows - get wamp - it has exactly what you need:
Apache
MySQL
PHP
Wamp comes with a nifty tray controller app which you can use to access the most common tasks, like restarting services, editing config, etc.
Equivalent to Artem's post, just a different organization which packages it differently. And my preference. It is called XAMPP.
If you are using Windows, you could install XAMPP to have the environment setup for you. You'll then need to learn how to bring up the server (basically executing the XAMPP control panel program), copy your PHP scripts into the correct directories (for XAMPP, it'd be C:/[xampp installation dir]/htdocs), access MySQL and creating the necessary MySQL databases (you could access the MySQL via the included PhpMyAdmin).
You can probably refer to a tutorial here (it's a YouTube video, BTW).
Don't have enough rep to comment but wanted to add something to Artem's recommendation of Wampserver. It's what I use on my windows machine, and one of the nicest things about it I find is the ability to have multiple versions of versions of apache, php and mysql installed alongside each other as plug-ins and then select which to have running at any time. It makes it easy for me to emulate the eventual hosting environment for any given project.
Okay, I've looked all over the internet for a good solution to get PHP and MySQL working on IIS7.0. It's nearly impossible, I've tried it so many times and given up in vain. Please please help by linking some great step-by-step tutorial to adding PHP and MySQL on IIS7.0 from scratch. PHP and MySQL are essential for installing any CMS.
Have you taken a look at this:
http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis7/
MySQL should be pretty straight forward.
Let us know what problems you're encountering...
I've been given a PHP / MySQL web site that I'm to host with IIS 7.0 on 64-bit Windows Server 2008.
I'm a .NET / MSSQL developer, and am unfamiliar with either PHP or MySQL.
Kev wrote:
Have you taken a look at this…
I don't know if any one implementation of Win64 PHP is more authoratative or popular than another.
I'm going to try following the steps in Kev's Enable FastCGI support in IIS7.0 article with file php-5.2.5-x64-2007-11-12.zip from fusion-x lan.
It's "PHP Version 5.2.5 (x64)", but according to php.net, the latest version is PHP 5.2.6. Oh, well.
Make sure "ISAPI Extensions" are installed in IIS (mine were).
Download and then unzip php-5.2.5-x64-2007-11-12.zip
Copy contents of folder php-5.2.5 (x64) into *C:\php*
Copy file C:\php\php.ini-dist into folder *C:\Windows*
Rename file C:\Windows\php.ini-dist as php.ini
Edit php.ini in Notepad. Remove leading semi-colon (;) from line:
;extension=php_mysql.dll
Save and close
Copy file C:\php\ext\php_mysql.dll into folder *C:\Windows\System32*
Within IIS Manager's "Handler Mappings", choose "Add Script Map…"
Request path: *.php
Executable: C:\php\php5isapi.dll
Name: PHP
Install MySQL (someone had already installed MySQL 5.0 for me).
Create file C:\inetpub\wwwroot\test.php as
<html>
<head>
<title>PHP Information</title>
</head>
<body>
<?php phpInfo(); ?>
</body>
</html>
Navigate to http://localhost/test.php in your web browser. You will see a page of information about PHP.
Roadblock: How do I get PHP to work with ADOdb and MySQL?
It's supposed to work via FastCGI. But I haven't had great success (using Vista). I can get PHP to run, but it crashes after a page loads (FastCGI does). So I'm modding you up. I'd like to see a reliable answer myself.
From my experience with windows/apache it's just a matter of install MySQL, I can't Imagine that IIS/Apache has anything to do with this.
Apache is a major pain to get running in Vista. And II7 (and 6) are suppose to run PHP fine. So why bother with Apache?
I would suggest if you are going for a PHP and MySQL install to instead use WAMP. It works great and is easy to add extensions and modify everything. I use it for work and love it.
One of the IIS developers has an excellent walkthrough here:
http://blogs.iis.net/bills/archive/2006/10/31/PHP-on-IIS.aspx
However, for the love of god why?