Jump to content

Recommended Posts

when i run my script it says Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\stuff.php on line 12

but i do include my connection as u can see

<?php 


function stuff($cost){

require_once ('mysqli_connect.php'); 

//Get Stuff How much of that stuff they have.

// Query the database:
	$q = "SELECT item, item_x, item_y FROM `stuff` WHERE user_id = 1";		
	$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
	$stuff = mysqli_fetch_array ($r, MYSQLI_ASSOC);
//End of Hq level

$item = $stuff['item'];
$item_x = $stuff['item_x'];
$item_y = $stuff['item_y'];

//Get there level of the building they wish to upgrade.
	$q = "SELECT level FROM `buildings` WHERE user_id = 1 & building = '$cost'";		
	$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
	$level = mysqli_fetch_array ($r, MYSQLI_ASSOC);
	$buildinglevel = $level['level'];
//testing zone

echo "item: $item<br />item_x: $item_x<br />item_y: $item_y<br />";

//end of testing zone

//Get Stuff.

// Query the database:
	$q = "SELECT item, item_x, item_y FROM `$cost` WHERE level = '$buildinglevel'";		
	$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
	$buidlingstuff = mysqli_fetch_array ($r, MYSQLI_ASSOC);
//End of Hq level

$costitem = $buidlingstuff['item'];
$costitem_x = $buidlingstuff['item_x'];
$costitem_y = $buidlingstuff['item_y'];

echo "<br />costitem: $costitem<br />costitem_x: $costitem_x<br />costitem_y: $costitem_y";


// Assume invalid values:
$im = $imx = $imy = FALSE;

//item

if($item >= $costitem){
$im = 1;
}else{
echo "Not enough";
}

if($item_x >= $costitem_x){
$imx = 1;
}else{
echo "Not enough";
}

if($item_y >= $costitem_y){
$imy = 1;
}else{
echo "Not enough";
}

if($im && $imx && $imy){
echo "ture";
} else {
echo "failed badly";
}


}

?>

 

and this is where i call the function

<?php

require_once ('mysqli_connect.php'); 

//Get HQ level.

// Query the database:
	$q = "SELECT level, points FROM `buildings` WHERE user_id = 1 & building = '$updatebuilding'";		
	$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
 $nowhqlevel = mysqli_fetch_array ($r, MYSQLI_ASSOC);
//End of Hq level

$hqlevel = $nowhqlevel['level'];
$pointsnow = $nowhqlevel['points'];

//Get HQ Buildtime.

// Query the database:
	$q = "SELECT time, points FROM `hq` WHERE level = $hqlevel";		
	$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
 $nowbuildtime = mysqli_fetch_array ($r, MYSQLI_ASSOC);
//End HQ Buildtime.
$points = $nowbuildtime['points'];
   $buildtime = $nowbuildtime['time'];
   $builddate = date("Y-m-d H:i:s", time() + (60 * $buildtime));
   $userid = 1;//turn it to session when programmed login
   
   require_once('stuff.php');
   
//Upgrade to the next level.
if(isset($_POST['update'])){

$buildinghq = $_POST['hq'];

 stuff($buildinghq);

if($im && $imx && $imy){
// Query the database:
$q = "UPDATE construction SET complete = '$builddate', progress = 1 WHERE user_id = $userid LIMIT 1";
		$r = @mysqli_query ($dbc, $q);
} else {
$res = "no enught rescouses";
}


}

//Check to see if any buildings are in progress
$q = "SELECT progress, complete FROM `construction` WHERE user_id = $userid & progress = 1";		
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
$progress = mysqli_fetch_array ($r, MYSQLI_ASSOC);

//Settings
$buildingpro = '0';

if($progress['progress'] == 1){
$msg = "You have 1 building in construction it will be finshished on " .$progress['complete'];

//Check if building is complete
$q = "SELECT complete FROM `construction` WHERE user_id = $userid & progress = 1";		
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
$check = mysqli_fetch_array ($r, MYSQLI_ASSOC);

$date = date("Y-m-d H:i:s");
if($date >= $check['complete']){

$q = "UPDATE construction SET progress = 0 WHERE user_id = $userid & progress = 1 LIMIT 1";
		$r = @mysqli_query ($dbc, $q);

	$upgraded = ++$hqlevel;
	$addpoints = $pointsnow + $points;
	echo $addpoints;
		$q = "UPDATE buildings SET level = '$upgraded', points = '$addpoints' WHERE user_id = $userid & building = 'hq' LIMIT 1";
		$r = @mysqli_query ($dbc, $q);

		$msg = "Building is Complete";

}else{

$buildingpro = "<b>Building in progress</b>";

}
}


?>

<?php
$date = date("Y-m-d H:i:s"); ?>

Link to comment
https://forums.phpfreaks.com/topic/168813-why-does-it-say-this/
Share on other sites

Haven't used mysqli so I am not absolutely sure about this.. but what I think you have the parameters in wrong order. If so change this

$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

 

to this

$r = mysqli_query ($q, $dbc) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

Link to comment
https://forums.phpfreaks.com/topic/168813-why-does-it-say-this/#findComment-890665
Share on other sites

I definitely agree with taquito. At no point in your posted code to you declare $dbc. Furthermore if it is in fact declared, and you just excluded that part, in the global scope outside of the function, you never pass it to the function or declare it as global in the function so that it is accessible. Basically if you declare $dbc prior to calling the stuff function (which you may), you would likely want to add a second parameter to the stuff function for the database connection, and call it $dbc.

 

function stuff ($cost, $dbc) {
..... code .....
}

 

or you could (this is bad practice thought

function stuff($cost) {
global $dbc;
........ code .......
}

 

chris.

Link to comment
https://forums.phpfreaks.com/topic/168813-why-does-it-say-this/#findComment-890672
Share on other sites

Since you are using require_once() and it is being used in the main code, the require_once() is not being executed inside of the function, so $dbc is not being created inside the function.

 

You should pass your exiting $dbc (that was created by the require_once() in the main code) as a parameter into any function that needs it.

Link to comment
https://forums.phpfreaks.com/topic/168813-why-does-it-say-this/#findComment-890673
Share on other sites

The reason for this is because you are using require once in both the global scope and the function scope. since it was already included one time in the global scope, php more or less ignores that second require once statement inside the function because it happens second. Essentially, that line

 

require_once ('mysqli_connect.php');

 

that is inside the function 'stuff' may as well not even be there because it will get skipped. php already included it in global scope.

 

chris.

Link to comment
https://forums.phpfreaks.com/topic/168813-why-does-it-say-this/#findComment-890674
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.