Re: mysql_connect() Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Since you seem to want to handle the errors yourself, you may try using the
@ operator on the mysql functions.
It'll silence any errors or warning that the functions might usually
generate; but, of course, you'll be left to test for errors yourself.

  $link = @mysql_connect($hostname,$mysql_login,$mysql_password);

  @mysql_select_db($dbname);

With that, the following statement should return any errors that MySQL would
otherwise generate:

  if (!$link) {
    die (mysql_error());
  }


Now, I don't see any reason for the 2nd echo (i.e., echo " After connecting
to the database...";) to not be called.
Is it in an if statement or other structure/scope in your actual script?

However, I do see a reason for another to never be called:

  if (!$link) {
    die('Could not connect: ' . mysql_error());
    echo "Some problem here....";
  }

Once you call die, script execution discontinues.
The echo after it will never be called, either by !$link returning false to
if or by die being called first.


On another note...
This sounds like you have PHP & MySQL installed for private use.
But, even if that's the case, I don't recommend connecting to the admin
account unless you really need to.

You should check out the following page on adding users:
http://dev.mysql.com/doc/refman/5.0/en/adding-users.html

And read up on the following commands:
CREATE USER: http://dev.mysql.com/doc/refman/5.0/en/create-user.html
GRANT: http://dev.mysql.com/doc/refman/5.0/en/grant.html

- Jon L.

On Sat, Mar 8, 2008 at 1:36 PM, Manysh <geminish@xxxxxxxxx> wrote:

> Hi,
>
> I am trying to make a connection to the MySQL database from PHP but it
> seems like I am missing a step or something and hence cannot connect
> to the database. I have tried everything but I can't figure out why.
> Please suggest/help (I am a beginner in PHP/MySQL/Apache).
>
> I am using MySQL 5.0.51a-community-nt on my Windows XP machine. PHP
> version used is 5.2 and Apache version is 2.2.
>
> Code Snippet:
>
> $hostname="localhost";
> $mysql_login="admin";
> $mysql_password="admin";
> //$database="sampleapp";
>
> echo "Print this..."; // THIS MESSAGE IS PRINTED
>
> $link = mysql_connect($hostname,$mysql_login,$mysql_password);
>
> echo " After connecting to the database..."; // THIS DOES NOT PRINT
> if (!$link) {
> die (mysql_error()); // NOR THIS ONE
> }
>
> $dbname = 'sampleapp';
> mysql_select_db($dbname);
>
> echo $link;
>
> if (!$link) {
>    die('Could not connect: ' . mysql_error());
>        echo "Some problem here....";
> }
> //echo 'Connected successfully';
> mysql_close($link);
> ?>
>
> Any help is appreciated.
> Thanks,
> Manysh
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux