-2

What I am trying to do is get mysql database to load up my .php file. I am using hostgator to run mysql database server. So far what i have for sql is a table with three columns.

  • int: id (primary key / A.I.)
  • varchar: name
  • text: message

I save the table and name it "test" and the database is called "testdb"

My php file (tutorialTest.php) looks like this:

<?php

$username = "nfoggia_nick";
$password = "imnick";
$database = "nfoggia_testdb";

mysql_connect(localhost, $username, $password);

@mysql_select_db($database) or die("Unable to find database");

$name =$_GET["name"];
$message = $_GET["message"];

$query = "INSERT INTO test VALUES (' ', '$name', '$message')";
mysql_querry($query) or die(mysql_error("error"));
mysql_close();
?>

I added the .php file in my file directory on hostgator and now my problem is this: I know that this code will do nothing, but when i type in

 http://localhost/tutorialTest.php 

the web browser says "browser cannot connect to local host" when it should just show a blank screen. Any help? What did i do wrong?

EDIT: I moved my php file to the document root for my website and now when i run the

http://myWebsiteName/tutorialTest.php this shows up:

Fatal error: Call to undefined function mysql_querry() in     /home2/nfoggia/public_html/tutorialTest.php on line 15
nfoggia
  • 513
  • 1
  • 8
  • 28
  • 2
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). FYI, you are also wide open to [SQL injections](http://stackoverflow.com/q/60174) – John Conde Jan 06 '14 at 03:28
  • 2
    you say hostgator but url is loclahost? im confused –  Jan 06 '14 at 03:28
  • If the file is on some hostgator server, then the URL should be the URL of your server, not the localhost – shad Jan 06 '14 at 03:29
  • I can't understand very well but the only reason that i can found is that you are writting but the directory file... check it – WearFox Jan 06 '14 at 03:30
  • Localhost is your computer. What is your hosting URL ? – Raptor Jan 06 '14 at 03:37
  • well it should be my website name right? i've tried that (myWebsitesName/tutorialTest.php) – nfoggia Jan 06 '14 at 03:40
  • where is this file located? on the webserver in the right directory or on your computer? –  Jan 06 '14 at 03:41
  • please see update above. I put the file in a new place – nfoggia Jan 06 '14 at 03:49
  • I would post it as a new question. –  Jan 06 '14 at 03:51
  • @nfoggia mysql_querry() is not a function. current the spell of query. Use mysql_query() instead. But highly re-comment to change to MYSQLi. – Sibiraj PR Jan 06 '14 at 03:57

1 Answers1

-1

Before mentioning all the PHP errors, your URL is wrong.

localhost means your local computer, instead of your hosting environment, which you mentioned is hostgator.

  • Do you upload your PHP to hostgator server?
  • Is the MySQL database schema exist in hostgator environment?

Your URL should look like : http://www.hostgator.com/whatever/tutorialTest.php (or under your domain name). Anything but not http://localhost


First of all, remove the @ to reveal the error.

For MySQL connection, the first parameter is a string, so you have to enclose it with single quotes, that is:

mysql_connect('localhost', $username, $password);

Last, you have multiple PHP errors :

mysql_querry($query)

You misspelled the function. Also, mysql_error() accepts link identifier as optional parameter instead of a string.


As a side note, stop using deprecated mysql_* functions. use MySQLi or PDO instead. Also, your code is subjected to SQL Injection attack, as you directly allow GET values to be inserted in your query.

Raptor
  • 53,206
  • 45
  • 230
  • 366