PHP3 moving to PHP4.. error in file.

This is a discussion on "PHP3 moving to PHP4.. error in file." within the PHP Forum section. This forum, and the thread "PHP3 moving to PHP4.. error in file. are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old May 24th, 2006, 08:22
New Member
Join Date: May 2006
Location: South England
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
PHP3 moving to PHP4.. error in file.

Hi,

My hosting company is upgrading to PHP4 and all my scripts were written in the good/bad old days of PHP3...

I have been told that one of our files produces an errror in PHP4 and I am needing to know the answer - so that I can amend all the PHP3 files that contain this code and get my system working again.

My site uses PHP and MySQL and the error means that scripts are not accessing the database, or at least that is what it appears.

I am NOT very technical and so be gentle with me! Here is the error that I was told about:

return mysql_num_rows($res);

The person who passed this to me then said:

"You will need to find the correct code to use for PHP4 to correct this error".

Obviously I have not a single clue so any help would be appreciated this end.

Many thanks.

Paul
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old May 24th, 2006, 09:09
Up'n'Coming Member
Join Date: Nov 2005
Location: England
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP3 moving to PHP4.. error in file.

mysql_num_rows is a perfectly valid php4 and php5 function, the error must be before that. its likely the error is in setting $res.

post some more code for us to see
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old May 24th, 2006, 09:20
New Member
Join Date: May 2006
Location: South England
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP3 moving to PHP4.. error in file.

Thanks for that - here is the script in full:

<?
//$sql_host_name = "localhost";
//$sql_username = "root";
//$sql_password = "";
//$sql_database_name = "other text here";
$sql_host_name = "localhost";
$sql_username = "admin";
$sql_password = "";
$sql_database_name = "other text here";
//$sql_host_name = "IP address here";
//$sql_username = "root";
//$sql_password = "";
//$sql_database_name = "other text here";

$productsTable = "products";
$merchantsTable = "merchants";
$controlTable = "control";
$categoriesTable = "categories";
$orderheadersTable = "orderheaders";
$orderdetailsTable = "orderdetails";
$invoicesTable= "invoices";
$cartsTable = "carts";
$paymentPlansTable = "paymentPlans";
$stocksTable= "stocks";
function dbConnect() {
global $sql_host_name,$sql_username,$sql_password,$sql_da tabase_name;
mysql_connect($sql_host_name,$sql_username,$sql_pa ssword) OR DIE("Unable to connect to database");
@mysql_select_db("$sql_database_name") or dbCreateDatabase($sql_database_name);
}
function dbClose() {
MYSQL_CLOSE();
}
function dbRunQuery($q) {
return MYSQL_QUERY($q);
}
function dbGetField($res,$field,$number) {
return stripSlashes(mysql_result($res,$number,"$field"));
}
function dbCreateDatabase($db) {
dbRunQuery("create database $db");
@mysql_select_db( "$db") or die("Sorry, can't select or create the database");
}
function dbNumberRows($res) {
return MYSQL_NUM_ROWS($res);
}
function dbGetNextID($name) {
global $controlTable;
$result=dbRunQuery("select * from $controlTable where name='$name'");
$number=dbNumberRows($result);
$next=dbGetField($result,"value",0);
$next++;
$result=dbRunQuery("update $controlTable set value=$next where name='$name'");
return $next-1;
}
function redirect($url) {
print "<script language=JavaScript>self.location.href=\"$url\";</script>";
exit;
}
function redirectTop($url) {
print "<script language=JavaScript>top.location.href=\"$url\";</script>";
exit;
}
?>

I might be barking up the wrong tree - but I saw a forum note on my hosts system talking about php.ini and the following relating to "global" (which means nothing to me!)

It said something about PHP.ini and changing this:

register_globals = On
track_vars = On

The heading was "Why wont my scripts work under PHP4 - hence my thoughts?!

Anyway - have a look at the code and your input is very much appreciated here.

Cheers,

Paul.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old May 24th, 2006, 09:25
Up'n'Coming Member
Join Date: Nov 2005
Location: England
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP3 moving to PHP4.. error in file.

that'll be what it is, you can't register global variables in php4. its turned off in php.ini for security and its highly recommended that you dont turn it back on.

function dbConnect() {
global $sql_host_name,$sql_username,$sql_password,$sql_da tabase_name;
mysql_connect($sql_host_name,$sql_username,$sql_pa ssword) OR DIE("Unable to connect to database");
@mysql_select_db("$sql_database_name") or dbCreateDatabase($sql_database_name);
}

is where the problem is. I think your best option is to turn those functions into a class, then you wont need globals.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old May 24th, 2006, 09:47
New Member
Join Date: May 2006
Location: South England
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP3 moving to PHP4.. error in file.

Phew - sounds like we are getting there. One question...

What's a class and how do I turn the global into one!

Cheers

Paul.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old May 24th, 2006, 11:39
Up'n'Coming Member
Join Date: Jan 2006
Location: East Sussex
Age: 27
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP3 moving to PHP4.. error in file.

Classes are an option but will take a bit of work to understand and php4 doesnt handle them brilliantly anyway. Another option would be to change the dbConnect to the following

function dbConnect($host, $usrName, $pWord, $dbName) {
mysql_connect($host,$usrName,$pWord) OR DIE("Unable to connect to database");
@mysql_select_db($dbName) or dbCreateDatabase($sql_database_name);
}

then whenever you call dbConnect change to

dbConnect($sql_host_name, $sql_username, $sql_password, $sql_database_name);

A bit of a pain i know but an option, unless anyone can point out why thats a really bad idea, which i may be! It does mean that you can keep your file containing all connection data in the secure part of your site.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old May 24th, 2006, 12:51
New Member
Join Date: May 2006
Location: South England
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP3 moving to PHP4.. error in file.

Thanks for this... Does this mean that files with text of:
include('include/database.php3');
dbConnect();

Would need changing to read:

include('include/database.php3');
dbConnect($sql_host_name, $sql_username, $sql_password, $sql_database_name);

and in addition the file database.php3 would need to be changed from:

function dbConnect() {
global $sql_host_name,$sql_username,$sql_password,$sql_da tabase_name;
mysql_connect($sql_host_name,$sql_username,$sql_pa ssword) OR DIE("Unable to connect to database");
@mysql_select_db("$sql_database_name") or dbCreateDatabase($sql_database_name);
}

to:

function dbConnect($host, $usrName, $pWord, $dbName) {
mysql_connect($host,$usrName,$pWord) OR DIE("Unable to connect to database");
@mysql_select_db($dbName) or dbCreateDatabase($sql_database_name);
}

I am in the middle of a load of technical people posting me different answers... on one forum (through my hosts) there are even arguments going on between people and I dont understand either of them.

Your solution seems easy... have I missed something?

Regards


Paul.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old May 24th, 2006, 13:19
Up'n'Coming Member
Join Date: Jan 2006
Location: East Sussex
Age: 27
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP3 moving to PHP4.. error in file.

Hi Paul,

Almost, I noticed a small mistake in my original message. This is the function that should replace the old dbConnect function declaration. I would say however that im not expert and it would be a good idea to get this plan checked by others before doing it!

function dbConnect($host, $usrName, $pWord, $dbName) {
mysql_connect($host,$usrName,$pWord) OR DIE("Unable to connect to database");
@mysql_select_db($dbName) or dbCreateDatabase($dbNam);
}

the change is in red
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old May 24th, 2006, 22:37
Most Reputable Member
Join Date: Aug 2005
Location: North Wales, United Kingdom
Age: 21
Posts: 1,093
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP3 moving to PHP4.. error in file.

You will need to $_POST['']
All your values to get around global variables.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old May 25th, 2006, 07:26
Up'n'Coming Member
Join Date: Jan 2006
Location: East Sussex
Age: 27
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP3 moving to PHP4.. error in file.

If he did that wouldnt it put all the values in the header?
If so that would be a bit of a security issue.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old May 25th, 2006, 08:54
Up'n'Coming Member
Join Date: Nov 2005
Location: England
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP3 moving to PHP4.. error in file.

you could always use define(CONSTANT, value); at the top then you can access the constants throughout the db script
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old May 25th, 2006, 09:43
Up'n'Coming Member
Join Date: Jan 2006
Location: East Sussex
Age: 27
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP3 moving to PHP4.. error in file.

Yup much better! No need to change any of the call functions then. So the function def would look like this iZone?

//all these become constants
$sql_host_name, $sql_username, $sql_password, $sql_database_name


function dbConnect($host = $sql_host_name, $usrName = $sql_username, $pWord = $sql_password, $dbName = $sql_database_name) {
mysql_connect($host,$usrName,$pWord) OR DIE("Unable to connect to database");
@mysql_select_db($dbName) or dbCreateDatabase($dbNam);
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old May 25th, 2006, 15:51
Up'n'Coming Member
Join Date: Nov 2005
Location: England
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP3 moving to PHP4.. error in file.

constants dont have $ so you would have

define(sql_host_name, "value");
define(sql_username, "value");
define(sql_password, "value");
define(sql_database_name, "value");

then

function dbConnect($host = sql_host_name, $usrName = sql_username, $pWord = sql_password, $dbName = sql_database_name) {
mysql_connect($host,$usrName,$pWord) OR DIE("Unable to connect to database");
@mysql_select_db($dbName) or dbCreateDatabase($dbNam);
}

i think that should work. Its also good practice to have your constants in upper case
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
php3, moving, php4, error, file

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax error with Ajax file reading Paramiliar JavaScript Forum 0 Aug 12th, 2007 16:48
Keep getting error message Microsoft VBScript runtime error '800a01a8' cpando1974 Classic ASP 2 Aug 7th, 2007 12:00
Creating a log file (text file) and an XML file using XSL kdelacruz Other Programming Languages 1 Nov 4th, 2006 21:12
PHP4/5 XML issues... jimz PHP Forum 1 Aug 22nd, 2006 06:42


All times are GMT. The time now is 07:01.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42