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.
|
|
|
|
|
![]() |
||
PHP3 moving to PHP4.. error in file.
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
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 |
|
|
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
|||
|
|||
|
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 |
|
#9
|
|||
|
|||
|
Re: PHP3 moving to PHP4.. error in file.
You will need to $_POST['']
All your values to get around global variables. |
|
#10
|
|||
|
|||
|
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. |
|
#11
|
|||
|
|||
|
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
|
|
#12
|
|||
|
|||
|
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); } |
|
#13
|
|||
|
|||
|
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 |
![]() |
| Tags |
| php3, moving, php4, error, file |
| Thread Tools | |
|
|
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 |