php safe mode database connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

php safe mode database connection

Post by tenacious-dee »

I' have great difficulty getting any of my php scripts which contain a database connection to run on my php safe mode server.

Does anybody have any advice on modifications i might have to make to the code below such that it will execute properly?

Code: Select all

<?php 
            $mysql = mysql_connect("localhost","anmluser","******","anmldb");
    
            if(mysql_connect_errno()){
                printf("Connect failed: %s\n", mysql_connect_error());
                exit();
            }else{
            $sql = "SELECT news_name, news_date, news_location, news_desc from tblnews order by news_id desc limit 5";
            $res = mysql_query($mysql, $sql);
        
            if($res){
                while($newArray = mysql_fetch_array($res, MYSQL_ASSOC)){
                    $title=$newArray['news_name'];
                    $date=$newArray['news_date'];
                    $location=$newArray['news_location'];
                    $desc=$newArray['news_desc'];
                    echo "<table cellpadding='4' style='border-bottom-style: dotted; border-color:purple;'>";
                    echo "<tr>";
                    echo "<td><font color='green' size='3pt'><b>".$title."</b></font></td>";
                    echo "<td><b><i>Posted:</b> ".$date."</i></td>";
                    echo "</tr>";
                    echo "<tr>";
                    echo "<td colspan=2><b>Location:</b> ".$location."</td>";
                    echo "</tr>";
                    echo "<tr>";
                    echo "<td colspan=2>".$desc."</td>";
                    echo "</tr>";
                    echo "</table>";
                    echo "<br/>";
                }
            }else{
                printf("Could not retrieve records: %s\n", mysql_error($mysql));
            }
        
            mysql_free_result($res);
            mysql_close($mysql);
            }
            ?>
I'm very desperate so help is seriously appreciated!

Thanks in advance!
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: php safe mode database connection

Post by jayshields »

There's no such function as mysql_connect_errno().

Change your first if() statement to:

Code: Select all

if(!$mysql)
which will test if the MySQL link contains a valid resource or not.

Probably better to get rid of the other call to mysql_connect_errno() too, and replace it with mysql_error().
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

Re: php safe mode database connection

Post by tenacious-dee »

Okay that has has printed the final error message to the screen... which is much better than nothing! :)

so this is the code as it stands.. incorporating the above changes..

Code: Select all

<?php
$mysql = mysql_connect("localhost","anmluser","8uhb2wsx","anmldb");
    
            if(!$mysql){
                printf("Connect failed: %s\n", mysql_error());
                exit();
            }else{
            $sql = "SELECT news_name, news_date, news_location, news_desc from tblnews order by news_id desc limit 5";
            $res = mysql_query($mysql, $sql);
        
            if($res){
                while($newArray = mysql_fetch_array($res, MYSQL_ASSOC)){
                    $title=$newArray['news_name'];
                    $date=$newArray['news_date'];
                    $location=$newArray['news_location'];
                    $desc=$newArray['news_desc'];
                    echo "<table cellpadding='4' style='border-bottom-style: dotted; border-color:purple;'>";
                    echo "<tr>";
                    echo "<td><font color='green' size='3pt'><b>".$title."</b></font></td>";
                    echo "<td><b><i>Posted:</b> ".$date."</i></td>";
                    echo "</tr>";
                    echo "<tr>";
                    echo "<td colspan=2><b>Location:</b> ".$location."</td>";
                    echo "</tr>";
                    echo "<tr>";
                    echo "<td colspan=2>".$desc."</td>";
                    echo "</tr>";
                    echo "</table>";
                    echo "<br/>";
                }
            }else{
                printf("Could not retrieve records: %s\n", mysql_error($mysql));
            }
        
            mysql_free_result($res);
            mysql_close($mysql);
            }
            ?>
Any further ideas??

Thanks guys
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

Re: php safe mode database connection

Post by tenacious-dee »

When I run the code above I get the following output to the browser

Could not retrieve records:

If anybody could advise further on this problem it would be greatly appreciated...

the code is

Code: Select all

<?php 
            $mysql = mysql_connect("localhost","anmluser","8uhb2wsx","anmldb");
 
            if(!mysql){
                printf("Connect failed: %s\n", mysql_error());
                exit();
            }else{
            $sql = "SELECT homepage_id, page_title, page_content from tblhomepage WHERE homepage_id=4";
            $res = mysql_query($mysql, $sql);
        
        
            if($res){
                //while($newArray = mysql_fetch_array($res, MYSQL_ASSOC)){
                    $title=$newArray['page_title'];
                    $content=$newArray['page_content'];
                    echo "<center>";
                    echo "<table border='0' cellpadding='6'>";
                    echo "<tr>";
                    echo "<td><font color='orange' size='3pt'><b>".$title."</b></font></td>";
                    echo "</tr>";
                    echo "<tr>";
                    echo "<td>".$content."</td>";
                    echo "</tr>";
                    echo "</table>";
                    echo "</center>";
                //}
            }else{
                printf("Could not retrieve records: %s\n", mysql_error($mysql));
            }
        
            mysql_free_result($res);
            mysql_close($mysql);
            }
            ?>
I'm really very desperate so help is very much appreciated
Post Reply