Your include has to have a proper path, and not be previously included if it contains any un-redefinable structure data like functions, classes or constants. Better yet, use require_once.
What are you or die'ing there? You don't have any lhs, so that will throw an error (um, T_LOGICAL_OR I believe it will be).
Saying you have errors doesn't help. You need to post the errors.
I use code like the following to switch between local and live:
PHP Code:
// Set the database access information as constants:
if (!defined('DB_USER')) {
if (LOCAL) {
DEFINE ('DB_USER', 'Andrew');
DEFINE ('DB_PASSWORD', 'Password2');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'Andy2');
} else {
DEFINE ('DB_USER', 'LiveUser');
DEFINE ('DB_PASSWORD', 'Password1');
DEFINE ('DB_HOST', 'some.host.com');
DEFINE ('DB_NAME', 'dbase_name');
}
}
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Your include has to have a proper path, and not be previously included if it contains any un-redefinable structure data like functions, classes or constants. Better yet, use require_once.
What are you or die'ing there? You don't have any lhs, so that will throw an error (um, T_LOGICAL_OR I believe it will be).
Saying you have errors doesn't help. You need to post the errors.
the "or die" bit is a leftover that does nothing as the database works.. but the intent was connect or die... Old code I reused since it work initially.
As for the errors:
Code:
Warning: mysqli_connect() [function.mysqli-connect]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for '-5.0/no DST' instead in C:\apache2\htdocs\hydrocephalustalk\index.php on line 66
Warning: mysqli_connect() [function.mysqli-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\apache2\htdocs\hydrocephalustalk\index.php on line 66
Warning: mysqli_connect() [function.mysqli-connect]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for '-5.0/no DST' instead in C:\apache2\htdocs\hydrocephalustalk\index.php on line 66
Warning: mysqli_connect() [function.mysqli-connect]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. (trying to connect via tcp://$dbhost:3306) in C:\apache2\htdocs\hydrocephalustalk\index.php on line 66
Warning: mysqli_connect() [function.mysqli-connect]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for '-5.0/no DST' instead in C:\apache2\htdocs\hydrocephalustalk\index.php on line 66
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\apache2\htdocs\hydrocephalustalk\index.php on line 66
Could not connect to the database.
I use code like the following to switch between local and live:
PHP Code:
// Set the database access information as constants:
if (!defined('DB_USER')) {
if (LOCAL) {
DEFINE ('DB_USER', 'Andrew');
DEFINE ('DB_PASSWORD', 'Password2');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'Andy2');
} else {
DEFINE ('DB_USER', 'LiveUser');
DEFINE ('DB_PASSWORD', 'Password1');
DEFINE ('DB_HOST', 'some.host.com');
DEFINE ('DB_NAME', 'dbase_name');
}
}
That looks like it should fit the bill.. It is my understanding that the prefered method of the host, user, password, dbname in a seperate file, is that correct? When I view the page source with that info in the page code it's not shown so I am a tad bit confused on that..
Results of trying that code on the test server:
Warning: main() [function.main]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for '-5.0/no DST' instead in C:\apache2\htdocs\hydrocephalustalk\index.php on line 65
Notice: Use of undefined constant LOCAL - assumed 'LOCAL' in C:\apache2\htdocs\hydrocephalustalk\index.php on line 65
Date
Location
Description
Contact Info
Warning: mysqli_connect() [function.mysqli-connect]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for '-5.0/no DST' instead in C:\apache2\htdocs\hydrocephalustalk\index.php on line 79
Warning: mysqli_connect() [function.mysqli-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\apache2\htdocs\hydrocephalustalk\index.php on line 79
Warning: mysqli_connect() [function.mysqli-connect]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for '-5.0/no DST' instead in C:\apache2\htdocs\hydrocephalustalk\index.php on line 79
Warning: mysqli_connect() [function.mysqli-connect]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. (trying to connect via tcp://DB_HOST:3306) in C:\apache2\htdocs\hydrocephalustalk\index.php on line 79
Warning: mysqli_connect() [function.mysqli-connect]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for '-5.0/no DST' instead in C:\apache2\htdocs\hydrocephalustalk\index.php on line 79
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\apache2\htdocs\hydrocephalustalk\index.php on line 79
Could not connect to the database.
This is the mysqli_connect line:
$dbc = mysqli_connect('DB_HOST', 'DB_USER', 'DB_PASSWORD', 'DB_NAME')
One the live server it just comes up cannot connect to the database
Last edited by Ctechinfo; 01-15-2013 at 01:59 AM..
Reason: Added results of the Andrew posted
by the looks of things with the adjustments made by Fou-Lu do work IF the database/table is intact. If no DB is found or the info is mismatched the script apparently dies and doesn't proceed to the else clause..
I reused the code (modified for a new table) with data for a non-existant database table and it threw the same errors as above. but the other code/table works fine as it has the appropriate association..
The working code (only works since it does connect):
The code for the new table that has no database table to connect to and dies producing errors:
PHP Code:
<?php
print('<table cellpadding="5" cellspacing="0" width="100%" align="center" border="1"><tr><th bgcolor="#c2c2c2" text-align="center" colspan="2">Hydrocephalus Association WALK Events</th></tr>');
if ($dbc = mysqli_connect('localhost', 'root', 'password', 'test'))
{
$query = "SELECT * FROM `walk` ORDER BY `date` ASC LIMIT 0, 30 ";
$data = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($data)) {
//Show results table
echo '<tr><td width="10%"><center>' . $row['date'] . '</center></td><td width="90%"><center>' . $row['event'] . '</center></td></tr><tr><td width="10%">Anytime</td><td width="90%"><a href="http://walk4hydro.kintera.org/virtual">Hydrocephalus Association Virtual WALK</a></td></tr>';}
mysqli_close($dbc);
print('</table>');
}
else
{
print('<tr><td width="10%><center> </center></td><td width="90%">Could not connect to the database</td></tr><tr><td width="10%">Anytime</td><td width="90%"><a href="http://walk4hydro.kintera.org/virtual">Hydrocephalus Association Virtual WALK</a></td></tr></table>');
}
?>
Warning: mysqli_fetch_array() [function.mysqli-fetch-array]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for '-5.0/no DST' instead in C:\apache2\htdocs\hydrocephalustalk\index.php on line 63
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\apache2\htdocs\hydrocephalustalk\index.php on line 63
Line 63: while ($row = mysqli_fetch_array($data)) {
Last edited by Ctechinfo; 01-15-2013 at 05:45 AM..
That's right the new will always construct a new object in the mysqli.
You can get around it by using:
PHP Code:
$con = new MySQLi(...);
if (!$con->connect_errno) // older versions of php must use mysqli_connect_errno($con)
{
// this is fine
}
Timezone can be set by using date_default_timezone_set. Set it to the correct timezone (UTC is fine).
Mysqli_fetch_array has failed since the mysqli_query has failed. This one can be done with an or die syntax or by using an if.
I added the date function you mentions in the top of the code block, and attempted to modify the code to use the $con code you posted but flubbed it up so I tried to revert back to the original code adding html to the or die, but that failed as well, so I went back to the newer code with the if/else.. I must of done something right this time around cause of the last page refresh that code works..
on the test server it still throws:
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'root'@'localhost' (using password: YES) in C:\apache2\htdocs\hydrocephalustalk\index.php on line 57
but that is to be expected as errors is turned on.. And I just uploaded the new code to the live server and it is working..
EDIT - using the connection information from my test server on the live server it ouputs the or die output accordingly BUT creates an error_log file.. If I use the connection info for the live server is shows the table header but nothing else for the new table as there is no DB setup on the live server yet..
Last edited by Ctechinfo; 01-15-2013 at 08:30 AM..