Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-14-2013, 11:11 PM   PM User | #16
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 01-14-2013, 11:51 PM   PM User | #17
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
PHP Code:
$dbc mysqli_connect('localhost''dbuser''dbpass''dbname'); 
these values should be your variables:

PHP Code:
$dbc mysqli_connect($dbhost$dbuser$dbpass$dbname); 
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
AndrewGSW is offline   Reply With Quote
Old 01-15-2013, 01:34 AM   PM User | #18
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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.
Ctechinfo is offline   Reply With Quote
Old 01-15-2013, 01:41 AM   PM User | #19
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
Quote:
Originally Posted by AndrewGSW View Post
PHP Code:
$dbc mysqli_connect('localhost''dbuser''dbpass''dbname'); 
these values should be your variables:

PHP Code:
$dbc mysqli_connect($dbhost$dbuser$dbpass$dbname); 
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
Ctechinfo is offline   Reply With Quote
Old 01-15-2013, 02:05 AM   PM User | #20
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
code block as it stands now
PHP Code:
<?php
// Set the database access information as constants: 
if (!defined('DB_USER')) { 
    if (
LOCAL) { 
        
DEFINE ('DB_USER''root'); 
        
DEFINE ('DB_PASSWORD''password'); 
        
DEFINE ('DB_HOST''localhost'); 
        
DEFINE ('DB_NAME''test'); 
    } else { 
        
DEFINE ('DB_USER''user'); 
        
DEFINE ('DB_PASSWORD''password'); 
        
DEFINE ('DB_HOST''localhost'); 
        
DEFINE ('DB_NAME''dbname'); 
    } 

print(
'<table cellpadding="5" cellspacing="0" width="100%" align="center" border="1"><tr><th bgcolor="#c2c2c2" text-align="center">Date</th><th bgcolor="#c2c2c2" text-align="center">Location</th><th bgcolor="#c2c2c2" text-align="center">Description</th><th bgcolor="#c2c2c2" text-align="center">Contact Info</th></tr>');
if (
$dbc mysqli_connect('localhost''root''password''test'))
{
            
    
$query "SELECT * FROM `sgroups` ORDER BY `date` ASC, `city` ASC LIMIT 0, 30 ";
    
$data mysqli_query($dbc$query);
    

while (
$row mysqli_fetch_array($data)) {
    
//Show results table
    
echo '<tr><td><center>' $row['date'] . '</center></td><td><center>' $row['city'] . ', ' .$row['state'] . '</center></td><td><center>' $row['description'] . '</center></td><td><center>' $row['contact'] . '</center></td></tr>';}
mysqli_close($dbc);
print(
'</table>');
}
else
{
    print(
'<tr><td colspan="4">Could not connect to the database</td></tr></table>');

?>
Fou-lou I did manage to have a brain storm and get the code figured out that you posted earlier in the thread to kick the or die clause..

Last edited by Ctechinfo; 01-15-2013 at 03:08 AM.. Reason: typo fix + revamped the code with Fou-Lou's suggestion
Ctechinfo is offline   Reply With Quote
Old 01-15-2013, 05:22 AM   PM User | #21
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
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):
PHP Code:
<?php
print('<table cellpadding="5" cellspacing="0" width="100%" align="center" border="1"><tr><th bgcolor="#c2c2c2" text-align="center">Date</th><th bgcolor="#c2c2c2" text-align="center">Location</th><th bgcolor="#c2c2c2" text-align="center">Description</th><th bgcolor="#c2c2c2" text-align="center">Contact Info</th></tr>');
if (
$dbc mysqli_connect('localhost''root''password''test'))
{
            
    
$query "SELECT * FROM `sgroups` ORDER BY `date` ASC, `city` ASC LIMIT 0, 30 ";
    
$data mysqli_query($dbc$query);
    

while (
$row mysqli_fetch_array($data)) {
    
//Show results table
    
echo '<tr><td><center>' $row['date'] . '</center></td><td><center>' $row['city'] . ', ' .$row['state'] . '</center></td><td><center>' $row['description'] . '</center></td><td><center>' $row['contact'] . '</center></td></tr>';}
mysqli_close($dbc);
print(
'</table>');
}
else
{
    print(
'<tr><td colspan="4">Could not connect to the database</td></tr></table>');

?>
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>&nbsp;</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..
Ctechinfo is offline   Reply With Quote
Old 01-15-2013, 05:47 AM   PM User | #22
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 01-15-2013, 08:17 AM   PM User | #23
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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..
Ctechinfo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:19 AM.


Advertisement
Log in to turn off these ads.