Connecting Tech Pros Worldwide Help | Site Map

Problems trying to get MySQL to work from PHP script

Martin
Guest
 
Posts: n/a
#1: Dec 5 '05
I'm having trouble getting a new PHP/MySQl installation to work.

Windows XP Pro, IIS 5.1, PHP 5.1.1, MySQL 5.0.16, ISAPI

This is a new computer. The whole setup is for development use only -
no access from beyond my own network.

PHP seems to be working OK. I can open a test.php page ok - It
executes down to the statement where I try to connect to the MySQL
database. At that point, nothing further happens. The page is served
out. No errors are generated.

MySQL also seems to be working. I've installed MySQL Administrator
and, from what I can see, everything looks OK. I have a database - it
has tables in it - the tables have data in them - I can execute
queries against them. This database is a copy of one I developed on
another machine

FWIW, the MySQL extension is activated in the PHP.INI file thus:
extension=php_mysql.dll

Here's the beginning of the script in the test.php page:
--------------------------------------------------
Here we go!

<?php

echo("<br>Inside the php script!");

$conn=@mysql_connect( "localhost", "root", "admin" );
echo("<br>Connected to the database!");

$rs = @mysql_select_db( "crowdwisdom", $conn );
---------------------------------------------------

When this runs, the displayed page consists of:
Here we go!
Inside the php script!

Help?


Aggro
Guest
 
Posts: n/a
#2: Dec 5 '05

re: Problems trying to get MySQL to work from PHP script


Martin wrote:
[color=blue]
> No errors are generated.[/color]
[color=blue]
> $conn=@mysql_connect( "localhost", "root", "admin" );
> echo("<br>Connected to the database!");[/color]

Your problem is that you don't know php. If mysql_connect() fails, your
program won't print any errors and it won't stop there. The @-characters
in front of a function disables printing of any internal error messages
and since you don't even thech the result of $conn, you won't get any
information if the function fails or doesn't fail.

Solution:

$conn=mysql_connect( "localhost", "root", "admin" );
if( !$conn )
die( "<br>Database connection failed!" );

echo("<br>Connected to the database!");
Andy Jeffries
Guest
 
Posts: n/a
#3: Dec 7 '05

re: Problems trying to get MySQL to work from PHP script


On Mon, 05 Dec 2005 20:48:28 +0000, Aggro wrote:[color=blue][color=green]
>> No errors are generated.[/color]
>[color=green]
>> $conn=@mysql_connect( "localhost", "root", "admin" );
>> echo("<br>Connected to the database!");[/color]
>
> Your problem is that you don't know php.[/color]

Inflamatory considering that you seem to have misunderstood his point...
[color=blue]
> If mysql_connect() fails, your
> program won't print any errors and it won't stop there. The @-characters
> in front of a function disables printing of any internal error messages
> and since you don't even thech the result of $conn, you won't get any
> information if the function fails or doesn't fail.[/color]

I think you're missing the OPs point. The code (better formatted as you
typed it above) should try to connect to the database (without printing
errors or stopping). Then it should print a line of text (regardless of
connection or not).

The OP is saying it never gets that far! It only prints the first line,
which means the script is stopping on the mysql_connect line, not just
failing to connect.

To be honest, I'd say that for some reason it's not recognising the MySQL
extension and if you remove the @ symbol you'll get a fatal error saying:

"undefined function mysql_connect"

The script is therefore stopping at that point but your @ is supressing
the printing of the message.

But that's just a best guess, I've never had PHP not recognise the MySQL
extension, but then again I've never installed it on Windows...

Cheers,


Andy

--
Andy Jeffries | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Aggro
Guest
 
Posts: n/a
#4: Dec 7 '05

re: Problems trying to get MySQL to work from PHP script


Andy Jeffries wrote:
[color=blue]
> I think you're missing the OPs point. The code (better formatted as you
> typed it above) should try to connect to the database (without printing
> errors or stopping). Then it should print a line of text (regardless of
> connection or not).[/color]

I don't think I missed the point, but I admit that I didn't read the
whole message very carefully. The reason for that is that I wouldn't
bother solving the real problem before I had fixed the first problem.
And the first problem was that no error messages were printed out while
there surely were errors.

But thank you for making this clear.
Closed Thread