-1

I have (2) PHP scripts: one written in PHP 4.0 and the second one written in PHP 5.6 (my ISP upgraded the servers and they only run PHP 5.6 now so I had to convert the 4.0 script to 5.6)

However when I use the PHP 4.0 script everything worked perfect prior to the ISP upgrade but when I use the PHP 5.6 script (which was converted), this script does not connect to the mySQL server or return any values

I am wondering if perhaps something is either wrong with the initial variables or perhaps something is missing in the second PHP 5.6 script that was converted from the earlier PHP 4.0 Script

Thanks

Here are the (2) PHP Scripts

The PHP 4.0 Script

    <?php

    $userdb="var1";

    $pass="var2";

    $database="var3";

    mysql_connect("sql.servername.com",$userdb,$pass);


    @mysql_select_db($database) or die ( header('location: status4.htm') );


    $match = "select id from USER_ACCOUNTS where username = '$username' and password = '$password'";

    $qry = mysql_query($match)
    or die ( header('location: status.htm?status=9') );
    $num_rows = mysql_num_rows($qry); 

    // Valid Username and Password

    if ($num_rows > 0) { 

    $qry = "SELECT * FROM USER_ACCOUNTS WHERE username like '%" . $username . "%'";
    $res = mysql_query($qry);
    $output='';


    while($row = mysql_fetch_assoc($res)){

    // loop through all returned results

    $output .= '&viewUsername=' . $row['viewUsername'] . '&viewPassword=' . $row['viewPassword'] . '&username=' . $row['username'] . 

    '&password=' . $row['password'] . '&name=' . $row['name'] . '&title=' . $row['title'] . '&email=' . $row['email'] . '&admin=' . 

    $row['admin'] . '&file=' . $row['file'] . '&file2=' . $row['file2'] . '&file3=' . $row['file3'] . '&file4=' . $row['file4'];

    echo "&status=1";

    echo $output;

    }

    }

    ?>

And here is the PHP 5.6 Script

 <?php

//error_reporting(1);
//ini_set('display_errors', '1');

// mysql connection
$db_host = 'sql.servername.com';
$db_user = 'var1';
$db_pass = 'var2';
$db_name = 'var3';

$dbh = new PDO('mysql:host='.$db_host.';dbname='.$db_name.';charset=utf8', $db_user, $db_pass);

// submit form
if (isset($_POST['submit']))
{

    $username = $_POST['username'];
    $password = $_POST['password'];

    $stmt = $dbh->prepare("SELECT * FROM USER_ACCOUNTS WHERE username = :username AND password = :password");
    $stmt->bindParam(':username', $username);
    $stmt->bindParam(':password', $password);
    $stmt->execute();
    $number_of_rows = $stmt->fetchColumn();

    // Valid Username and Password
    if ($number_of_rows > 0)
    {
        $row = $stmt->fetchAll(); 

        $output = '';

        while($row)
        {
            // loop through all returned results
            $output .= '&viewUsername=' . $row['viewUsername'] . '&viewPassword=' . $row['viewPassword'] . '&username=' . $row

['username'] . '&password=' . $row['password'] . '&name=' . $row['name'] . '&title=' . $row['title'] . '&email=' . $row['email'] . 

'&admin=' . $row['admin'] . '&file=' . $row['file'] . '&file2=' . $row['file2'] . '&file3=' . $row['file3'] . '&file4=' . $row

['file4'];
            echo "&status=1";
            echo $output;
        }
    }

}

?>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    The php4 is so dated, it's not really relevant here. You should try to provide more debugging from the 5.6 script being ran. Comparing the two scripts won't do much for making the 5.6 work. –  Apr 02 '17 at 15:20
  • What errors are you getting? – Hyder B. Apr 02 '17 at 15:20
  • Some differences between both are; that in the 2nd one, you have POST arrays. Use php's error reporting to see what comes of it and exceptions on PDO. – Funk Forty Niner Apr 02 '17 at 15:28
  • @Hyder B the 5.6 Script does not throw any errors when I test it - however I did add some echo statements to test different parts of the script to see if it is working (like right after a successful connection) and it doesn't return any statements. I am wondering if something is not right with the top of the 5.6 script and the initial connection? – Server Programmer Apr 02 '17 at 15:30
  • and instead of `fetchColumn()` try using `rowCount()` – Funk Forty Niner Apr 02 '17 at 15:30
  • Thanks @Fred -ii- testing now, do you see anything wrong with how I have assigned the variables from the first script to the second script? – Server Programmer Apr 02 '17 at 15:34
  • @Fred -ii- I tried swapping out fetchColumn() with rowCount() and it still does not work, I am wondering if the 5.6 script is even connecting successfully to mySQL? – Server Programmer Apr 02 '17 at 15:37
  • @ServerProgrammer seems ok to me. Make sure all POST arrays have value; we don't know its origins (from the form). Do a `var_dump($_POST);` for them and the query also to see what's passing through or not. – Funk Forty Niner Apr 02 '17 at 15:37
  • 1
    @ServerProgrammer if it's not connecting, then use PDO's error handling http://php.net/manual/en/pdo.error-handling.php and using the setAttribute option. You should also uncomment out your error reporting during testing. – Funk Forty Niner Apr 02 '17 at 15:38
  • 1
    @ServerProgrammer have a look at one of my answers http://stackoverflow.com/a/22253579/1415724 it contains a full PDO method and error handling on the connection to check if a row exists. If that works for you, then take it from there and modify it to suit your db/columns. Let me know how things work out. – Funk Forty Niner Apr 02 '17 at 15:42
  • Thanks @Fred -ii-, I uncommented the error reporting and it does not throw back any errors. Trying your last solution and the setAttribute option. Also, is there a simple way to add an echo statement to the top of the script to see if it has successfully connected, something like...echo "&status=ConnectionWorked"; somewhere in the script to see if it return a value for &status? – Server Programmer Apr 02 '17 at 15:42
  • @ServerProgrammer check http://stackoverflow.com/a/6263868/1415724 it's basically a "try/catch"; you could probably use an `if($con){...} else{...}` type of thing also. – Funk Forty Niner Apr 02 '17 at 15:45
  • @Fred -ii- Great previous post, reviewing now. To make sure I am not missing something simple in my conversion, can you post the way you would convert the PHP 4.0 script (I like the security you are implementing to prevent SQL injections)? – Server Programmer Apr 02 '17 at 15:48
  • @ServerProgrammer as I said in a previous post. Base yourself on the PDO version in the link I gave you from one of my answers. My rewriting (basically) the same thing may come out the same also. – Funk Forty Niner Apr 02 '17 at 15:54
  • @Fred -ii- I used the PHP 4.0 script for 5+ years and it worked fine, how does the 5.6 script look to you, do you see anything wrong in the PHP 5.6 script? – Server Programmer Apr 02 '17 at 16:07
  • @Fred -ii- Quick update - I have tried a number of your suggestions in the earlier post and I can't get this to work. I am an app developer (I am not strong on the PHP side) and I did write the original PHP 4.0 script (it took me a few weeks) and it worked perfectly for many years, but I may be over my head. – Server Programmer Apr 02 '17 at 16:41

1 Answers1

0

Solved it - simply replace $REMOTE_ADDR with $_SERVER['REMOTE_ADDR'] in the script to convert from PHP 4.0 to PHP 5.6 :)