mcg...@gmail.com wrote:
> […] I am using apache webserver. When I run a php script through
> localhost through my browser the script works well. When I run my
> mysql_connect.php then all I get is a blank page. nothing happens. I
> placed an echo statement at the end to verify that it connected.
> The echo statement doesn't appear when I refresh the page.
The “echo” statement is not supposed to appear. Perhaps you mean that the
output it is supposed to generate does not appear.
[code reformatted to 80 columns]
> <?php //# Script 7.2 - mysql_connect.php
>
> // This file contains the database access information.
> // This file also establishes a connection to MySQL and selects the
> // database.
>
> // Set the database access information as constants.
> DEFINE ('DB_USER', 'root');
^^^^^^
> DEFINE ('DB_PASSWORD', 'pass@word1');
> DEFINE ('DB_HOST', 'localhost:3306');
> DEFINE ('DB_NAME', 'reg');
>
> // Make the connnection.
> $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD) OR
^^
> die ('Could not connect to MySQL: ' . mysql_error() );
>
> // Select the database.
> @mysqli_select_db(DB_NAME) OR die ('Could not select the database: ' .
^^
> mysql_error() );
It is not a good idea to write the identifiers of functions or operators in
all-uppercase. This style should be reserved for constants.
It is also not a good idea to let the opening parenthesis of a function call
be preceded by whitespace. This style should be reserved for control
statements like “if”.
> echo 'hello';
> ?>
You can and should omit the “?>” except when you want to leave PHP parsing
mode to output something (usually in templates only).
Debugging is easier if you do not suppress the messages that code generates.
So, for now, you should remove the “@”s, and run
ini_set('display_errors', true);
error_reporting(E_ALL | E_STRICT);
before all other statements. This should be the default for your local
(development) server, though; edit php.ini if necessary (all recommended
development values are in the comments of a default distribution).
If that still does not help, make sure that the “mysqli” PHP module is
loaded. You can use phpinfo() for that.
Because a Parse Error or a Fatal Error is required for execution not to
reach the “echo” statement. This could be either a syntax error or you
could have attempted to call a function that was not defined, respectively.
I can see no obvious syntax error in the posted code (although your code
lines are too long to be easily readable; consider wrapping at column 80 at
the latest, at least for Usenet) which leaves only the second possibility.
--
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.