Jump to content

Upgrading Php4 to 5 & mysql to mysqli - Warning: mysqli_query() expects parameter 1 to be mysqli, null given


Go to solution Solved by requinix,

Recommended Posts

Hello!

 

I recently switched to a new host who doesn't support PHP4. Everything broke immediately. My website is pretty old and I work/volunteer like a dog, so I've hardly had time to learn. I know I've been intimidated by the time demand of changing a whole website. It's actually not been as hard as I imagined, but I've been stuck on this problem for a few days.

I updated everything to mysqli and it works on some pages, not on others. My research tells me I have a scoping issue, but I'm not sure how to resolve it. I read I could put it in a function, but I'm not quite sure how that would work.
 

I have this file "database.php" which is included by many other files, some of which are included in files themselves

$link = mysqli_connect('localhost', 'username', 'password');
mysqli_select_db($link, 'database') or die (mysql_error());

Below is "layout.php" which is included by another file

include $_SERVER['DOCUMENT_ROOT']."/commons.php"; /* this "includes" database.php */

function body(){
$sql = mysqli_query($mysqli, "SELECT *
FROM articles
ORDER BY article_number
DESC LIMIT 11, 15");

while($row = mysqli_fetch_array($sql)){
stripslashes(extract($row));
}

I'm guessing all the include levels pushe the file out of the scope. Can anyone help this wiery dog?

 

Thank you very much ahead of time!

 

I am just wondering why you're updating from php4 to php5 when the current version of PHP is 7. Sounds like you still need another host.

Edited by benanamen
  • Solution

If you're upgrading then you might as well go for PHP 7: 5.6 is the last of the 5.x series (there is no 6.x) and is no longer receiving feature updates. 7.0 is out, 7.1 is out, and 7.2 is coming out fairly soon. The good news is that 5->7 has fewer big changes than 4->5.

 

Anyway, variables defined outside of a function are not available inside of a function. Files themselves don't have scopes (they kinda do but nothing relevant here) so that's not it.

Not that you've actually said what your problem is - just a vague "it doesn't work".

 

If you want a quick way to get a mysqli object (which is what $mysqli actually is) then you can do

function db() {
	static $link = null;
	if (!$link) {
		$link = new mysqli('localhost', 'username', 'password');
		$link->select_db('database') or die ('Cannot connect to database');
	}
	return $link;
}
and

function body(){
	$sql = db()->query("SELECT *
		FROM articles
		ORDER BY article_number
		DESC LIMIT 11, 15");
1a. Notice that it's the OOP style. PHP 4's object-oriented support was... lacking. In PHP 5+ it's much better. You could stay procedural with the mysqli_* functions but try to learn OOP.

1b. If you really don't want to do it then use db() in place of the $link in the regular function calls.

2. The "or die()" pattern is very bad. Stop doing it and figure out something better.

3. Putting database error messages in the die() is even worse.

 

Likewise,

while($row = mysqli_fetch_array($sql)){
1c. $sql is also an object and you can do $sql->fetch_array() instead.

 

Also,

stripslashes(extract($row));
5. That doesn't work, least of all not the way you think it does.

6. stripslashes() like that is not even needed in PHP 5+.

7. extract()ing values is frowned upon for assorted reasons. Assign variables manually (yes, it's more code) or simply just reference the values in the $row array directly (easier).

  • 2 weeks later...

The main reason I've not jumped from 4 to 7 is because of limited documentation on that. There's a lot of documentation for 4 to 5 and 5 to 7, but unfortunately not 4 to 7. Google search not as good as it used to be IMO either though, in my opinion. I will upgrade this to 7 at some point. I have a really big project to upgrade from 4 at some point, so hopefully breaking my personal site to bits will help me learn :D

The error for the record was "Warning: mysqli_query() expects parameter 1 to be mysqli, null given" though I admit after I read where you said I didn't actually say what the problem was, it took me a little while to realize I only posted it in the title (I copy/pasted the error from my site and CTRL+F to find it in here). I should've posted it in the body as well.
 

Thank you very much for the advice. I did end up using "db() in place of the $link"

 

I have to start using the Object Oriented method and appreciate the encouragement to do so. I just got a book on PHP7 (which is how I learned PHP4). If I had found a book on migration, I would've migrated a long time ago!

 

Thank you again!

The main reason I've not jumped from 4 to 7 is because of limited documentation on that. There's a lot of documentation for 4 to 5 and 5 to 7, but unfortunately not 4 to 7.

Probably because they expect you to read the 4/5 and then 5/7 docs. It's hardly efficient to write upgrade procedures for every combination of possible version upgrades.
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.