Page 1 of 1

mysql_connect() Fails without error

Posted: Mon Jul 31, 2006 10:01 am
by frogg
I have apache 2.2, php 5.2 and mysql 5.1 installed. php is working, and I can log on to the mysql command line client. php.ini is correctly configured. I try to use the function: mysql_connect($host,$user,$pass) (Where $host is localhost, $user is root and $pass is my password)
But no further php is parsed and no error message is returned.

Posted: Mon Jul 31, 2006 10:04 am
by volka
more code please.

Posted: Mon Jul 31, 2006 10:05 am
by RobertGonzalez
Try this and post back what is returned. Make sure to substitute your actual data with the login credentials in the script:

Code: Select all

<?php
define('DB_USER', 'user'); // USE YOUR ACTUAL LOGIN NAME
define('DB_PASSWORD', 'pass'); // USE YOUR ACTUAL PASSWORD
define('DB_HOST', 'localhost'); // USE YOUR SERVER ADDRESS
define('DB_NAME', 'mydatabase'); // VERIFY THIS IS YOUR DATABASE NAME

if (!$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)) {
    die('<h1>Could not establish a connection to the database server ' . DB_HOST . ': ' . mysql_error() . '</h1>');
} else {
    echo '<h1>We are connected!</h1>';
}

if (!mysql_select_db(DB_NAME)) {
    die('<h1>Could not select database ' . DB_NAME . ': ' . mysql_error() . '</h1>');
} else {
    echo '<h1>We are in!</h1>';
}
?>

Posted: Mon Jul 31, 2006 10:31 am
by frogg
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I haven't created a database in mysql yet, but that's only relevant to the second half of the code you sent me, right?
When i run the code below, it returns nothing...

Code: Select all

<?php
define('DB_USER', '**********'); // USE YOUR ACTUAL LOGIN NAME
define('DB_PASSWORD', '*********'); // USE YOUR ACTUAL PASSWORD
define('DB_HOST', 'localhost'); // USE YOUR SERVER ADDRESS

if (!$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)) {
    die('<h1>Could not establish a connection to the database server ' . DB_HOST . ': ' . mysql_error() . '</h1>');
} else {
    echo '<h1>We are connected!</h1>';
}
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jul 31, 2006 10:35 am
by RobertGonzalez
Yes, you are correct.

Try running just the connection part. If you get a blank page from that, I would be willing to bet that you are having a critical error either because of a MySQL Client API issue preventing a connection or from an undefined function call because the mysql extension is not loaded by default on PHP5 (mysqli is though, I believe).

Posted: Mon Jul 31, 2006 10:36 am
by GM
Means that it's failing somewhere, and you've got errors turned off. My guess is that you haven't loaded the mysql module.

If you are on a Windows setup, you need to edit the php.ini file, and uncomment the line

Code: Select all

extension=php_mysql.dll
if you are using mysql version 5ish or higher, uncomment the php_mysqli.dll extension too.


If you are on a *ix system, I can't help you there, I'm afraid, but I'm sure one of these kind people will :)

EDIT: must... type... faster :wink:

Posted: Mon Jul 31, 2006 10:40 am
by feyd
Run the following in a new file and tell us the results please.

Code: Select all

<?php

$neg = array(0, false, '', null, 'off');
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
$rg = (in_array(strtolower(ini_get('register_globals')), $neg) ? 'Off' : 'On');
$de = (in_array(strtolower(ini_get('display_errors')), $neg) ? 'Off' : 'On');
$so = (in_array(strtolower(ini_get('short_open_tag')), $neg) ? 'Off' : 'On');
$le = '';
$cli = (php_sapi_name() == 'cli');
$eol = ($cli ? "\n" : "<br />\n");

$gle = get_loaded_extensions();
$rows = array();
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
    $le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . "\n";
}
if ($cli)
{
     $le = $eol . $le;
}
else
{
 $le = '<pre>' . $le . '</pre>';
}

$ec = array(
   'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
   'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
   'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
   'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
   if (($t & $v) == $v)
   {
      $e[] = $n;
      $t ^= $v;
   }
}
$er = $er . ' (' . implode(' | ', $e) . ')';

if (!$cli)
{
  echo '<html><head><title>quick info</title></head><body>' . "\n";
}

echo 'PHP Version: ' . $ve . $eol;
echo 'PHP OS: ' . $os . $eol;
echo 'Error Reporting: ' . $er . $eol;
echo 'Register Globals: ' . $rg . $eol;
echo 'Short Tags: ' . $so . $eol;
echo 'Display Errors: ' . $de . $eol;
echo 'Loaded Extensions:' . $le . $eol;

if (!$cli)
{
  echo '</body></html>' . "\n";
}

?>

Posted: Mon Jul 31, 2006 10:57 am
by frogg
Yes the mysql module was not loaded.

I've got it working by copying libmysql.dll to C:\Windows
I had C:\Program Files\PHP 5 defined in Path previously, does this need a restart to take effect?

Posted: Mon Jul 31, 2006 10:59 am
by volka
If php is installed as (apache) module, yes.
But not the whole computer, just the apache process.

Posted: Mon Jul 31, 2006 11:01 am
by frogg
PHP is installed as an apache module.
I've restarted apache, but the Path method doesn't work :s

Posted: Mon Jul 31, 2006 11:09 am
by volka

Code: Select all

<?php phpinfo(); ?>
tells you which php.ini is used. Did you change the right file?
IF so, there should be a mysql section in the output of phpinfo.

And while you're at it, please set error_reporting=E_ALL and maybe display_errors=on if this is "only" your development server.

Posted: Mon Jul 31, 2006 11:17 am
by frogg
Yep i changed the correct file. Everything works now. I'm going to leave it with libmysql.dll in C:\windows, since it works.

Thanks for your help! You people are too helpful. :o