Jump to content

how can i make the below code compatble with PHP 5 ?


phpvox

Recommended Posts

how can i make the below code compatble with PHP5.......this works fine with php4......what changed in php5 that it cant connect to database anymore ? thanks

 

<?

  $connected = false;

  function c()

  {

      global $db_host, $db_login, $db_pswd;

 

      $db = @mysql_connect($db_host, $db_login, $db_pswd);

      $connected = true;

      return $db;

  }

 

function q($q_str)

{

    global $db_name, $connected;

    if(!$connected)

    {

    c();

    }

 

    $r = mysql($db_name, $q_str);

    return $r;

}

 

function d($db)

{

    @mysql_close($db);

}

 

function e($r)

{

  if(@mysql_numrows($r))

  return 0;

  else return 1;

}

 

function f($r){

  return @mysql_fetch_array($r);

}

 

function nr($r){

  return @mysql_num_rows($r);

}

?>

 

Link to comment
Share on other sites

what changed in php5 that it cant connect to database anymore ?

 

Nothing, the code is just poorly written in the first place. Remove all the @'s for starters, lets see if you can get an actual error so we can see whats going on.

Link to comment
Share on other sites

Have you tried using <?php instead of just <? ? That changed long before php5.

 

Not necessarily. As long as his hosting service has short tags enabled he will be able to use <? code ?>. But yes, using <?php code ?> may fix the problem.

Link to comment
Share on other sites

I guess a better question is.  What exactly is your script doing on PHP5?  Is it just showing you your PHP code?  If so then Thorpe's suggestion should work, otherwise, we'll need to know more about the problem, like what exactly do you get when you run the script?

Link to comment
Share on other sites

I havent done anything yet, but this is the problem:

 

Warning: mysql() [function.mysql]: Access denied for user 'apache'@'localhost' (using password: NO)

 

this is not due to incorrect database info.....the info is correct, this is a compatibility issue from PHP4 to PHP5

 

I can downgrade to php4 in ONE click from the cpanel, and it works fine

 

but when i click on using PHP5...then i get the above error

 

please dont post telling me to check database info, because it is correct....... something has changed in PHP5 that the script dosent connect to database.

 

this only happenes when i upgrade to php5

 

>: thanks

Link to comment
Share on other sites

Umm... I see your problem.  You should never use the mysql() function.  I couldn't really find any information on it but it seems to use some sort of default configuration to connect to your database which may have been ignored in PHP4 but used in PHP5.  Always use mysql_query() for executing queries in mysql.

Link to comment
Share on other sites

thanks for ur replay..........but i didnt write the code, i bought this script, and the company went down, so i cant get any tech support from them, the script is encoded except that the code i showed you ....so what should i do now ?

Link to comment
Share on other sites

okay what about this code, i found this in a diffrent folder

 

<?

  function c ()

  {

    global $db_host;

    global $db_login;

    global $db_pswd;

    $db = @mysql_connect ($db_host, $db_login, $db_pswd);

    $connected = true;

    return $db;

  }

 

  function q ($q_str)

  {

    global $db_name;

    global $connected;

    global $query_count;

    global $show_debug_info;

    global $queries_str;

    if (!$connected)

    {

      c ();

    }

 

    $r = mysql ($db_name, $q_str);

    if ((ee () AND $show_debug_info))

    {

      echo '' . ': ' . $q_str;

    }

 

    if (!$query_count)

    {

      $query_count = 0;

    }

 

    $queries_str .= '' . $q_str . '<br />';

    ++$query_count;

    return $r;

  }

 

  function d ($db)

  {

    @mysql_close ($db);

  }

 

  function e ($r)

  {

    if (@mysql_numrows ($r))

    {

      return 0;

    }

    else

    {

      return 1;

    }

 

  }

 

  function ee ()

  {

    global $show_debug_info;

    $err = mysql_error ();

    if ($show_debug_info)

    {

      echo $err;

    }

 

    return ($err ? true : false);

  }

 

  function f ($r)

  {

    return @mysql_fetch_array ($r);

  }

 

  function fr ($r)

  {

    return @mysql_fetch_row ($r);

  }

 

  function nr ($r)

  {

    return @mysql_num_rows ($r);

  }

 

  $connected = false;

?>

Link to comment
Share on other sites

Same problem:

$r = mysql ($db_name, $q_str);

 

Should be:

$r = mysql_db_query ($db_name, $q_str);

 

if that doesn't work then just try:

 

<?php
function c() {
global $db_host;
global $db_login;
global $db_pswd;
global $connected;
static $db;
if(!$connected){
	$db = @ mysql_connect($db_host, $db_login, $db_pswd);
	$connected = true;
}
return $db;
}

function q($q_str) {
global $db_name;
global $connected;
global $query_count;
global $show_debug_info;
global $queries_str;
$db = c();

$r = mysql($db_name, $q_str, $db);
if ((ee() AND $show_debug_info)) {
	echo '' . ': ' . $q_str;
}

if (!$query_count) {
	$query_count = 0;
}

$queries_str .= '' . $q_str . '
';
++ $query_count;
return $r;
}
?>

Link to comment
Share on other sites

Err... crap ... forgot to change one more line in the code... heh... guess that's what I get for not testing it :).

Change this:

$r = mysql($db_name, $q_str, $db);

To this:

$r = mysql_db_query($db_name, $q_str, $db);

 

In the code that I gave you as an alternative solution.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.