Jump to content

Help to resurrect an old script


lefreak

Recommended Posts

Hi Guys,

 

First time poster on this forum and from what I can see so far I am going to find this site invaluable

 

I was thumbing through some old folders today and came across a script I had made for me about 6 years ago now back in the old oscommerce 2.2 days.

 

The script or should I say scripts imported thousands of products from an xml file and also did various other update functions etc.

 

Now I thought to myself I wonder if it will work with the latest version incarnation and guess what 'no it does not' lol

 

Anyhoo the first problem is to update the mysql to mysqli commands as it is throwing this error

 

 

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\wamp_server\www\bstrap\admin\mysql.php on line 62

 

the code segment that throws the error is this

function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
	{

		$this->persistency = $persistency;
		$this->user = $sqluser;
		$this->password = $sqlpassword;
		$this->server = $sqlserver;
		$this->dbname = $database;

		if($this->persistency)
		{
			$this->db_connect_id = mysql_pconnect($this->server, $this->user, $this->password);
		}
		else
		{
			$this->db_connect_id = mysqli_connect($this->server, $this->user, $this->password);
		}
		if($this->db_connect_id)
		{
			if($database != "")
			{
				$this->dbname = $database;
				$dbselect = mysqli_select_db($this->dbname);
				if(!$dbselect)
				{
					@mysql_close($this->db_connect_id);
					$this->db_connect_id = $dbselect;
				}
			}
			return $this->db_connect_id;
		}
		else
		{
			return false;
		}
	}

line 62 is this line
 

$dbselect = mysqli_select_db($this->dbname);

Now originally the mysqli were mysql so I tried updating but with lack of know how I am struggling.

 

Any help in showing me the way would be really appreciated.

 

Thanks

 

Mark

Link to comment
Share on other sites

$dbselect = mysqli_select_db($this->dbname);

As the error indicates, you aren't using mysqli_select_db() correctly. See the manual for what it's missing. First parameter should be the resource, not the db name.

 

 

Thank yo for your fast response, the problem is I have already read that page and its about as clear as mud to me lol

Link to comment
Share on other sites

There are 2 types of mysqli functions, one for procedural coding, which you are using, and the other for OOP. See the example in the manual for procedural style.

 

This doesn't have to do with your error, but this is also incorrect as it's using mysql function and not mysqli equivalent.

$this->db_connect_id = mysql_pconnect($this->server, $this->user, $this->password);
Link to comment
Share on other sites

 

There are 2 types of mysqli functions, one for procedural coding, which you are using, and the other for OOP. See the example in the manual for procedural style.

 

This doesn't have to do with your error, but this is also incorrect as it's using mysql function and not mysqli equivalent.

$this->db_connect_id = mysql_pconnect($this->server, $this->user, $this->password);

ok I think this is correct, well the error has gone anyway

$dbselect = mysqli_select_db($this->db_connect_id, $this->dbname);

the next error that is being thrown is this

 

Warning: mysql_query() expects parameter 2 to be resource

 

and the line of code is

$this->query_result = mysql_query($query, $this->db_connect_id);

would I be right in thinking this is the correction

$this->query_result = mysqli_query($this->db_connect_id, $query);

Thanks

 

Mark

Link to comment
Share on other sites

It looks correct, does it work?

 

well sort of, I now get this error

 

Warning: Illegal offset type in unset in C:\wamp_server\www\bstrap\admin\mysql.php on line 112

Warning: Illegal offset type in unset in C:\wamp_server\www\bstrap\admin\mysql.php on line 113

which consists of

unset($this->row[$this->query_result]);
unset($this->rowset[$this->query_result]);
Link to comment
Share on other sites

Hard to tell since that wasn't in your original code. Please post your most recent code.

 

sorry I should have said that this is a completely different function, the full function reads

	function sql_query($query = "", $transaction = FALSE)
	{
		// Remove any pre-existing queries
		unset($this->query_result);
		if($query != "")
		{
			$this->num_queries++;
			//echo "[DEBUG] $query<br>";
			$this->query_result = mysqli_query($this->db_connect_id, $query);
		}
		if($this->query_result)
		{
			unset($this->row[$this->query_result]);
			unset($this->rowset[$this->query_result]);
			return $this->query_result;
		}
		else
		{
			return ( $transaction == 'END_TRANSACTION' ) ? true : false;
		}
	}
Link to comment
Share on other sites

Where is $this->row and $this->rowset being defined? Regardless, $this->query_result wouldn't be an array index of either of those, it's a result. You should probably also check to see if those are isset() before trying to unset() them.

Link to comment
Share on other sites

Where is $this->row and $this->rowset being defined? Regardless, $this->query_result wouldn't be an array index of either of those, it's a result. You should probably also check to see if those are isset() before trying to unset() them.

 

I will make it easier and post the entire file rather than snippets so you can see

if(!defined("SQL_LAYER"))
{

define("SQL_LAYER","mysql");

class sql_db
{

	var $db_connect_id;
	var $query_result;
	var $row = array();
	var $rowset = array();
	var $num_queries = 0;

	//
	// Constructor
	//
	function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
	{

		$this->persistency = $persistency;
		$this->user = $sqluser;
		$this->password = $sqlpassword;
		$this->server = $sqlserver;
		$this->dbname = $database;

		if($this->persistency)
		{
			$this->db_connect_id = mysql_pconnect($this->server, $this->user, $this->password);
		}
		else
		{
			$this->db_connect_id = mysqli_connect($this->server, $this->user, $this->password);
		}
		if($this->db_connect_id)
		{
			if($database != "")
			{
				$this->dbname = $database;
				$dbselect = mysqli_select_db($this->db_connect_id, $this->dbname);
				if(!$dbselect)
				{
					@mysql_close($this->db_connect_id);
					$this->db_connect_id = $dbselect;
				}
			}
			return $this->db_connect_id;
		}
		else
		{
			return false;
		}
	}

	//
	// Other base methods
	//
	function sql_close()
	{
		if($this->db_connect_id)
		{
			if($this->query_result)
			{
				mysql_free_result($this->query_result);
			}
			$result = mysql_close($this->db_connect_id);
			return $result;
		}
		else
		{
			return false;
		}
	}

	//
	// Base query method
	//
	function sql_query($query = "", $transaction = FALSE)
	{
		// Remove any pre-existing queries
		unset($this->query_result);
		if($query != "")
		{
			$this->num_queries++;
			//echo "[DEBUG] $query<br>";
			$this->query_result = mysqli_query($this->db_connect_id, $query);
		}
		if($this->query_result)
		{
			unset($this->row[$this->query_result]);
			unset($this->rowset[$this->query_result]);
			return $this->query_result;
		}
		else
		{
			return ( $transaction == 'END_TRANSACTION' ) ? true : false;
		}
	}

	//
	// Other query methods
	//
	function sql_num_rows($query_id = 0) {
		return $this->sql_numrows($query_id);
	}

	function sql_numrows($query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			$result = mysql_num_rows($query_id);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_affectedrows()
	{
		if($this->db_connect_id)
		{
			$result = mysql_affected_rows($this->db_connect_id);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_numfields($query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			$result = mysql_num_fields($query_id);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_fieldname($offset, $query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			$result = mysql_field_name($query_id, $offset);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_fieldtype($offset, $query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			$result = mysql_field_type($query_id, $offset);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_fetchrow($query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
$intQueryId = (integer) $query_id;
    $this->row[$intQueryId] = mysqli_fetch_array($query_id); // 212
    return $this->row[$intQueryId]; // 213
		}
		else
		{
			return false;
		}
	}
	function sql_fetchrowset($query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			unset($this->rowset[$query_id]);
			unset($this->row[$query_id]);
			while($this->rowset[$query_id] = mysql_fetch_array($query_id))
			{
				$result[] = $this->rowset[$query_id];
			}
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_fetchfield($field, $rownum = -1, $query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			if($rownum > -1)
			{
				$result = mysql_result($query_id, $rownum, $field);
			}
			else
			{
				if(empty($this->row[$query_id]) && empty($this->rowset[$query_id]))
				{
					if($this->sql_fetchrow())
					{
						$result = $this->row[$query_id][$field];
					}
				}
				else
				{
					if($this->rowset[$query_id])
					{
						$result = $this->rowset[$query_id][$field];
					}
					else if($this->row[$query_id])
					{
						$result = $this->row[$query_id][$field];
					}
				}
			}
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_rowseek($rownum, $query_id = 0){
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
			$result = mysql_data_seek($query_id, $rownum);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_nextid(){
		if($this->db_connect_id)
		{
			$result = mysqli_insert_id($this->db_connect_id);
			return $result;
		}
		else
		{
			return false;
		}
	}
	function sql_freeresult($query_id = 0){
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}

		if ( $query_id )
		{
			unset($this->row[$query_id]);
			unset($this->rowset[$query_id]);

			@mysql_free_result($query_id);

			return true;
		}
		else
		{
			return false;
		}
	}
	function sql_error($query_id = 0)
	{
		$result["message"] = mysqli_error($this->db_connect_id);
		$result["code"] = mysqli_errno($this->db_connect_id);

		return $result;
	}

	function sql_escape($query)
	{
		return mysqli_real_escape_string($this->db_connect_id, $query);
	}

} // class sql_db

} // if ... define

Link to comment
Share on other sites

I have made a bit of progress since last nights issues but inevitably other issues have sprang up lol

 

I have googled my little heart out trying to resolve this one with no luck

 

 

Notice: Object of class mysqli_result could not be converted to int in C:\wamp_server\www\bstrap\admin\mysql.php on line 213

	function sql_fetchrow($query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
    $intQueryId = (integer) $query_id;
    $this->row[$intQueryId] = mysqli_fetch_array($query_id); // 212
    return $this->row[$intQueryId]; // 213
		}
		else
		{
			return false;
		}
	}

the offending line

    $intQueryId = (integer) $query_id;

I would say apart from the last few errors the script is now working and importing products and images into the database successfully.

Link to comment
Share on other sites

It makes no sense to “convert” the script to MySQLi when you merely append an “i” to the function calls and forget half of them. All that gives you is a whole lot of broken code which isn't any better than the one before.

 

The question is what you want to achieve. Are you scared because of the mysql_* deprecation warnings? Don't worry, the PHP core devs are well aware that there are tons of legacy code out there, so they won't just remove the functions tomorrow. And even if that happens somewhere in the distant future, there may be smarter alternatives than a complete rewrite (sticking to an old PHP version, using wrapper functions etc.).

 

So don't just blindly start changing legacy code when there's no reason for it.

 

If you do need to update your database code (why?), then do it right and actually make use of the new features. For example, we now use prepared statements to securely pass values to queries. This escaping stuff is more or less dead. I also wonder why you've picked MySQLi instead of the much more user-friendly and versatile PDO. Just because the name sounded familiar?

Link to comment
Share on other sites

What error? What exactly makes you think there's something wrong with your old script? The only error you've mentioned so far came from your MySQLi update.

 

If your script merely triggers a bunch of deprecation warnings, that doesn't mean you have to do complete rewrite. But even if there actually is a critical error due to some old feature you've used, fixing it should be a matter of minutes. PHP hasn't changed a lot in the last years (besides additional features, of course).

Link to comment
Share on other sites

I have made a bit of progress since last nights issues but inevitably other issues have sprang up lol

 

I have googled my little heart out trying to resolve this one with no luck

	function sql_fetchrow($query_id = 0)
	{
		if(!$query_id)
		{
			$query_id = $this->query_result;
		}
		if($query_id)
		{
    $intQueryId = (integer) $query_id;
    $this->row[$intQueryId] = mysqli_fetch_array($query_id); // 212
    return $this->row[$intQueryId]; // 213
		}
		else
		{
			return false;
		}
	}

the offending line

    $intQueryId = (integer) $query_id;

I would say apart from the last few errors the script is now working and importing products and images into the database successfully.

 

which leads me back to this post

Link to comment
Share on other sites

Your code assumes that the query result can be converted to an integer. This is true in the case of the older mysql resource, but not with the newer mysqli objects. This integer is apparently used to save multiple result sets into the $this->row variable.

 

You could fix the immediate error by just removing that cast and appending the result to the array rather than use an index. However the code using that class may depending on that functionality so removing it would break things in other ways.

 

I happen to agree with Jacques1, just because something is old doesn't mean you need to start fiddling with it if it's still working. And if you are going to, don't just hack away at it randomly, re-work it with modern code, even if that means just re-writing it rather than patching it.

 

Without knowing more about how that class is used by other code it's hard to say what kind of work would be necessary to get it properly converted to mysqli (or PDO). If the querying result indexing "feature" isn't used you could ignore it. If it is, you'll have to emulate it.

Edited by kicken
Link to comment
Share on other sites

edit: pretty much says the same as kicken's post above 

 

the class code you started with was not designed very well. it is just some php functions stuck inside a class definition. the functions in it are for two separate things, the database connection/functions that use the connection and query results. all the code dealing with a query result should have been a separate class.

 

the particular error you are getting is just a symptom of something that worked when using mysql functions, of getting the result resource id as an integer, that does not have any direct replacement of getting an object id as an integer (requires dumping the object handle and parsing the resulting string.)

 

if the application code that makes use of this class expects it to maintain separate query results for multiple queries, there's no clean way of converting it that wouldn't also require making a corresponding change to the application code.

 

if the application code that makes use of this class does not expect it to maintain separate/multiple query results, you can simply eliminate all the nonsense that's trying to use the result id/object id as an array index.

 

if you do want/need to get the object id, see this link - http://www.sitepoint.com/forums/showthread.php?221775-PHP-5-and-Object-id

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.