Click to See Complete Forum and Search --> : convert PHP5 code to PHP4??


james19
06-18-2008, 08:05 AM
Hi
Ive found a code from the internet which does not work, I think the problem is that I have PHP4 and the code is written in PHP5, does anyone know how to change the code to PHP4??? Can anyone give me a good website which explains the difference in coding in php4 and 5??? The code is shown below:

<?php

$db = new mysqli(‘localhost’, ‘root’ ,”, ‘password’);
if(!$db) {

echo ‘ERROR: Could not connect to the database.’;
} else {
if(isset($_POST[‘queryString’])) {
$queryString = $_POST[‘queryString’];

if(strlen($queryString) >0) {

$query = $db->query("SELECT value FROM something WHERE value LIKE ‘$queryString%’ LIMIT 10");
if($query) {

while ($result = $query ->fetch_object()) {

echo ‘<li onclick="fill(’‘.$result->value.’‘);">’.$result->value.‘</li>’;
}
} else {
echo ‘ERROR: There was a problem with the query.’;
}
} else {

}
} else {
echo ‘There should be no direct access to this script!’;
}
}

?>


Your help would be very much appreciated.
Thanks:)

NogDog
06-18-2008, 03:06 PM
Probably the main problem is the first thing I noticed: the mysqli (http://php.net/mysqli) functions/objects are not available in PHP4. You would need to replace it with the procedural mysql (http://php.net/mysql) functions (note no "i" at the end of the name).

Of course, a much better solution would be to migrate to a host that supports PHP 5 since PHP 4 has already reached its end-of-life status, and not even security patches will be provided after another month or two. Many hosts now provide both PHP4 and PHP5 support, with one running in CGI mode and the other as a web server module, and you specify which to use by the file name's suffix.

james19
06-19-2008, 08:59 AM
Thanks for your help, what difference does the i make and would I have to change other areas of the code if I took out the i???

In regards to using PHP5, this was my university coursework and my uni only has php4. I know that the university year is over and the deadline for this is long gone but I just want to use the website as a means to experiment with new technologies and try new things.
I was just wondering how different is PHP4 compared to PHP5 because if ti is a lot different then their is no poi8nt in me even going any further with PHP4??

NogDog
06-19-2008, 09:14 AM
It is not simply a matter of removing the "i" in "mysqli", as the newer "mysqli" extension is object-oriented whereas the older "mysql" extension is procedural. See the www.php.net/mysql_connect and www.php.net/mysql_query pages for an example of usage.

99+% of PHP4 code will migrate to PHP5 with no problem, though PHP4 code that uses deprecated practices may have problems due to changes in default configuration values (such as the use of register_globals). Anyway, PHP4 really is (or at least should be) on its way out and we all should be migrating to PHP5 or encouraging our hosting providers to make the switch. After all, it probably won't be long before the first PHP6 release candidate is out there. :)