PHP: Fatal Error: Call to a member function winner() on null - php

I'm getting the following errors and do not know how to proceed.
Warning: Creating default object from empty value in
/Applications/XAMPP/xamppfiles/htdocs/ACIT4850_lab1/index.php on line
32
Notice: Undefined variable: game­ in
/Applications/XAMPP/xamppfiles/htdocs/ACIT4850_lab1/index.php on line
17
Fatal error: Call to a member function winner() on null in
/Applications/XAMPP/xamppfiles/htdocs/ACIT4850_lab1/index.php on line
17
Here is what I have
<body>
<?php
// put your code here
$squares = $_GET['board'];
$game = new Game($squares);
if ($game­->winner('x')) // line 17
echo 'You win. Lucky guesses!';
else if ($game­->winner('o'))
echo 'I win. Muahahahaha';
else
echo 'No winner yet, but you are losing.';
?>
</body>
and
<?php
class Game {
var $position = '---------';
function __construct($squares) {
$this­->position = str_split($squares); // line 32
}
function winner($token) {
$won = false;
$result = false;
for($row=0; $row<3; $row++) {
if (($this->$position[3*$row] == $token) &&
($this->$position[3*$row+1]== $token) &&
($this->$position[3*$row+2] == $token))
$result = true;
}
...
if ($result == true)
$won = true;
return $won;
}
}?>
any help would be appreciated.

the error is because you have a strange char in this line
$this­->position = str_split($squares); // line 32
the character is \u00ad
and you have all over your code like: $something\u00ad->
try with this line of code
$this->position = str_split($squares); // line 32
aside from this you have an error in this part of the code
for($row=0; $row<3; $row++) {
if (($this->$position[3*$row] == $token) &&
($this->$position[3*$row+1]== $token) &&
($this->$position[3*$row+2] == $token))
$result = true;
}
you are using
$this->$position
and you need to use
$this->position

Related

Notice: Undefined offset: 0 in C:\xampp\htdocs\isan\hasildata.php on line 21

Hi I receive the following error in my homework and I do not know what is the problem hasildata.php on line 21 :
$dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$from."&destinations=".$to."&key=%20AIzaSyCWpwVwu1hO6TJW1H8x_zlhrLfbSbQ2r3o");
$data = json_decode($dataJson,true);
$nilaiJarak = $data['rows'][0]['elements'][0]['distance']['text'];
$time=$data['rows'][0]['elements'][0]['duration']['text'];
Screenshot
You must check whether your request is OK or not
$dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$from."&destinations=".$to."&key=%20AIzaSyCWpwVwu1hO6TJW1H8x_zlhrLfbSbQ2r3o");
$data = json_decode($dataJson,true);
if ($data['status'] !== "OK") {
// Error
echo $data['error_message'];
// Do something
} else {
$nilaiJarak = $data['rows'][0]['elements'][0]['distance']['text'];
$time=$data['rows'][0]['elements'][0]['duration']['text'];
}

Getting Error: Trying to get property of non-object in

I am getting this Trying to get property of non-object in at if ($fetch_size_id->num_rows > 0) and at else if ($fetch_size_id->num_rows == 0).
Can't figure out what is causing it.
The if conditions don't work as well...
When I var_dump($fetch_size_id) I get bool(false).
$fetch_size_id = $conn->query("
SELECT
`sizes`.`size_id`,
`sizes`.`width`,
`sizes`.`ratio`,
`sizes`.`construction`,
`sizes`.`diameter`
FROM `sizes`
WHERE
`sizes`.`width` = '".$conn->real_escape_string($row['5'])."'
AND `sizes`.`ratio` = '".$conn->real_escape_string($row['6'])."'
AND `sizes`.`construction` = '".$conn->real_escape_string($construction)."'
AND `sizes`.`diameter` = '".$conn->real_escape_string($row['7'])."
");
if ($fetch_size_id->num_rows > 0) {
$fetch_size_id = $fetch_size_id->fetch_object();
$temp_size_id = $fetch_size_id->size_id;
} else if ($fetch_size_id->num_rows == 0) {
//create new
} else {
//error
}
Am I overlooking something?
You are not checking variable $fetch_size_id
if (isset ($fetch_size_id){
if ($fetch_size_id->num_rows) {
$fetch_size_id = $fetch_size_id->fetch_object();
$temp_size_id = $fetch_size_id->size_id;
} else{
//create new
}
} else {
//error
}

PHP Fatal error: Call to a member function asXML() on a non-object for Google Calendar XML

I need some help with this code. It was running perfectly up until a couple of weeks ago.
<?php
date_default_timezone_set('America/New_York');
$cache_time = 3600*12;
$feedRegular = "https://www.google.com/calendar/feeds/invernesscalvary.com_fipcro22aiul8uaa2hg80h6gmc%40group.calendar.google.com/public/basic?orderby=starttime&singleevents=true&futureevents=true&max-results=999&sortorder=a";
$feedFeature = "https://www.google.com/calendar/feeds/invernesscalvary.com_8ohrv5fhqf7o2r9jhgsr2q2764%40group.calendar.google.com/public/basic?orderby=starttime&singleevents=true&futureevents=true&max-results=3&sortorder=a";
$cache_file_reg = $_SERVER['DOCUMENT_ROOT'].'/cache/gcal.xml';
$timedif_reg = #(time() - filemtime($cache_file_reg));
$xmlRegular = "";
if (file_exists($cache_file_reg) && $timedif_reg < $cache_time) {
$str_reg = file_get_contents($cache_file_reg);
$xmlRegular = simplexml_load_string($str_reg);
} else {
$xmlRegular = simplexml_load_file($feedRegular);
if ($f_reg = fopen($cache_file_reg, 'w')) {
$str_reg = $xmlRegular->asXML();
fwrite ($f_reg, $str_reg, strlen($str_reg));
fclose($f_reg);
} else { echo "<p>Can't write to the cache.</p>"; }
}
$xmlRegular->asXML();
$cache_file_fea = $_SERVER['DOCUMENT_ROOT'].'/cache/gcal_featured.xml';
$timedif_fea = #(time() - filemtime($cache_file_fea));
$xmlFeature = "";
if (file_exists($cache_file_fea) && $timedif_fea < $cache_time) {
$str_fea = file_get_contents($cache_file_fea);
$xmlFeature = simplexml_load_string($str_fea);
} else {
$xmlFeature = simplexml_load_file($feedFeature);
if ($f_fea = fopen($cache_file_fea, 'w')) {
$str_fea = $xmlFeature->asXML();
fwrite ($f_fea, $str_fea, strlen($str_fea));
fclose($f_fea);
} else { echo "<p>Can't write to the cache.</p>"; }
}
if (is_object($xmlFeature)) {
$xmlFeature->asXML();
}
$n = 0;
foreach ($xmlRegular->entry as $entry) {
$n++;
$ns_gd = $entry->children('http://schemas.google.com/g/2005');
$eDate = date("M", strtotime($ns_gd->when->attributes()->startTime));
$eDay = date("j", strtotime($ns_gd->when->attributes()->startTime));
$eTime = date("g:ia", strtotime($ns_gd->when->attributes()->startTime));
$eLink = date("Y/m", strtotime($ns_gd->when->attributes()->startTime));
$title = str_replace(" & ", " & ", $entry->title);
$where = $ns_gd->where->attributes()->valueString;
$link = $entry->link->attributes()->href;
echo "<li>\n";
echo "<span class=\"eDate\">$eDate <span class=\"eDay\">$eDay</span> # $eTime</span><a rel=\"$eLink\" title=\"# $where\" href=\"#\">";
echo "<p class=\"eTitle\">$title</p></a>\n";
echo "</li>\n\n";
if($n == 3) break;
}
?>
FIXED Fatal error: Call to a member function asXML() on a non-object in /home/calvary/public_html/index.php on line 83
FIXED If I do a var_dump($xmlRegular) after the simplexml_load_string($str_reg) I get a boolean false.
UPDATE Warning: main() [function.main]: Node no longer exists in /home/calvary/public_html/index.php.new on line 132 now shows up twice for each line 132, 133, 134, 136, and 139 which are the lines starting with $eDate $eDay $eTime $eLink and $where.
You're getting this error because the $xmlRegular variable is not an object. Likely, it's a Boolean with a false value. This is because the simplexml_load_string or simplexml_load_file is failing to return an object. I believe the most probable reason would be simplexml_load_file does not have read permissions or cannot find the file.
I'd recommend var_dump'ing a value and then running an 'exit;' to see what the value at that point is. This way you can see which value is incorrect.
Please let us know if you make any progress on this and I'd be happy to provide more input!
EDIT: Going directly to the URL you are using ($feedRegular) I get a Forbidden 403 error. This would cause your $xmlRegular to be false instead of an object.

Fatal error: Call to a member function fetch_assoc() on a non-object in CTBans - Sourcebans

I use CTBans from https://forums.alliedmods.net/showthread.php?t=166080&page=7 and problem is here: Fatal error: Call to a member function fetch_assoc() on a non-object in /home/web/xxx/pages/page.ctbanlist.php on line 74
On 74 line is:
while ($row = $query->fetch_assoc())
{
$row['communityid']=SteamIDToFriendID($row['steamid']);
if($row['ctbantime'] > 0)
{
if($row['ctbantime']<60)
$row['ctbantime']=$row['ctbantime']." minutes";
else if($row['ctbantime'] < 3600)
$row['ctbantime']=round($row['ctbantime']/60, 2)." hours";
else
$row['ctbantime']=round($row['ctbantime']/(24*60), 2)." days";
}
else
{
if($row["active"]==0)
$row['ctbantime'] = "Expired";
else
$row['ctbantime'] = "Never";
}
if($row['admin']=="CONSOLE")
$row["adminname"] = "CONSOLE";
else
{
$admin = $GLOBALS['db']->GetRow("SELECT user FROM sb_admins WHERE authid=\"".$row['admin']."\"");
$row['adminname'] = $admin["user"];
}
if($row['reason'] == "")
{
$row['reason'] = "Geen reden opgegeven";
}
$data[]=$row;
}
What is wrong? When I connect the database, nothing happens (no content). Version SourceBans is 1.4.11
Thank you for helping and answer.

call to undefined function msg()

i m getting an error in the php code..
Fatal error: Call to undefined function msg()
but the function is already defined in the code here
whenever i click on a login button this login script runs
<?php
mysql_select_db("elunika", $con);
$a = $_POST['em'];
$b = $_POST['pwd'];
$c = $_POST['log'];
$b = md5($b);
if (isset($c)) {
$q = mysql_query("select * from registeration where email='$a' and password='$b'");
$r = mysql_num_rows($q);
if ($r) {
$_SESSION["Authenticated"] = 1;
$_SESSION['id'] = $a;
}
else {
$_SESSION["Authenticated"] = 0;
}
if ($_SESSION["Authenticated"] === 0) {
die(msg(0, "Incorrect Information"));
}
else {
session_write_close();
echo msg(1, "profile.php");
}
function msg($status, $txt)
{
return '{"status":' . $status . ',"txt":"' . $txt . '"}';
}
}
?>
Only unscoped functions are defined at compile-time (prior to execution). Your msg function however is just going to be defined if the if branch (the if (isset(…)) on line 9) is entered; so it's only going to be defined at the moment where the executor reaches it.
But in your code msg() is already called before the function declaration was encountered at run-time. Moving the function declaration up (= before the msg() call) should help:
function msg ($status, $txt) {
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
if($_SESSION["Authenticated"] === 0) {
die(msg(0,"Incorrect Information"));
} else {
session_write_close();
echo msg(1,"profile.php");
}

Categories