Jump to content

Falling at the 1st hurdle - simple php script calling mysql


stevievelvet

Recommended Posts

I have the following sample.html which calls sample.php, but I receive a blank page, what could be wrong ? (mysql 5.1.29)

 

sample.html

<html>

<body>

<form action=sample.php method=GET>

Search for Country: <input type=text name=country size=25 maxlength=25>

<p>

<input type=submit>

</form>

</body>

</html>

 

 

sample.php

<?

mysql_connect (localhost, root, password);

mysql_select_db (world);

 

echo $country

 

if ($country == "")

{$country = '%';}

$result = mysql_query ("SELECT * FROM Country

WHERE Name LIKE '$country%'");

ql_close();

 

echo $result

>?

 

I've also referenced test.php n the above code, again a blank web page:

 

<?

echo 'hello!';

echo $country;

 

?>

 

I SEE hello! on my webpage but not the value of $country ??

what could be wrong ?

 

(1) Apache works & displays phpinfo

(2) I'm using the world dsample database from mysql.com

 

many thanks

 

Sally

 

 

Link to comment
Share on other sites

-Most of your basic HTML syntax is wrong.  You're missing quotes everywhere, your submit button doesn't have a name, you're declaring the GET method for your form but don't use it in sample.php.

 

-Here's what you should do.  Read up on basic HTML syntax.

-Instead of using the GET method use the POST method.

-In sample.php you need to use the POST method to get the input from country:

 

$country = $_POST['country'];

 

There's a lot more I could post here but try those first and come back with your updated files.

 

Link to comment
Share on other sites

The code is using register_globals, so it is at least 6 years out of date. Register_globals were turned off in php4.2 in the year 2002 and have been completely removed in php6. No new code, new books, new tutorials, or new hosting accounts should have been created after that point in time that relied on register globals being on.

 

The code is also using short open tags <?. These can be disabled on any server and using them results in code that is non-portable. Always use a full <?php tag.

 

The closing tag in sample.php is invalid and is probably causing a fatal parse error. You should be learning php, developing php code, and debugging php code on a local development system where error_reporting is set to E_ALL and display_errors is set to ON in your php.ini to get php to help you find errors like this one. Stop and start your web server to get any changes made to php.ini to take effect. Turning on these two settings would also point out the variables that are not defined due to the register_globals problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.