mysql_connect() Fails without error
Moderator: General Moderators
mysql_connect() Fails without error
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.
But no further php is parsed and no error message is returned.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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>';
}
?>feyd | Please use
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]- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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).
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).
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
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
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.dllIf 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
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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";
}
?>Code: Select all
<?php phpinfo(); ?>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.