Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  2. #17
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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

  3. #18
    Regular Coder
    Join Date
    Sep 2012
    Posts
    100
    Thanks
    5
    Thanked 3 Times in 3 Posts
    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.

  4. #19
    Regular Coder
    Join Date
    Sep 2012
    Posts
    100
    Thanks
    5
    Thanked 3 Times in 3 Posts
    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

  5. #20
    Regular Coder
    Join Date
    Sep 2012
    Posts
    100
    Thanks
    5
    Thanked 3 Times in 3 Posts
    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

  6. #21
    Regular Coder
    Join Date
    Sep 2012
    Posts
    100
    Thanks
    5
    Thanked 3 Times in 3 Posts
    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.

  7. #22
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  8. #23
    Regular Coder
    Join Date
    Sep 2012
    Posts
    100
    Thanks
    5
    Thanked 3 Times in 3 Posts
    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.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •