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);

}

?>

 

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.

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?

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

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.

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;

?>

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;
}
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.