注意:未定义偏移:0英寸 [英] Notice: Undefined offset: 0 in
本文介绍了注意:未定义偏移:0英寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
注意:未定义的偏移量:0 in
C:\xampp\htdocs\mywebsite\reddit_vote_tut\src\votes.php on line 41
从这段代码:
<?php
include(config.php);
函数getAllVotes($ id)
{
$ votes = array();
$ q =SELECT * FROM entries WHERE id = $ id;
$ r = mysql_query($ q);
if(mysql_num_rows($ r)== 1)//在表中找到$ id
{
$ row = mysql_fetch_assoc($ r);
$ votes [0] = $ row ['votes_up'];
$ votes [1] = $ row ['votes_down'];
}
返回$ votes;
}
函数getEffectiveVotes($ id)
{
$ votes = getAllVotes($ id);
$ effectiveVote = $ votes [0] - $ votes [1]; //错误发生在这里
返回$ effectiveVote;
}
$ id = $ _POST ['id'];
$ action = $ _POST ['action'];
//获得当前票数
$ cur_votes = getAllVotes($ id);
//好吧,现在更新票数
if($ action =='vote_up')// vote up
{
$ votes_up = $ cur_votes [0] +1; // AND ERROR THROWN HERE
$ q =更新线程SET votes_up = $ votes_up WHERE id = $ id;
}
elseif($ action =='vote_down')
{
$ votes_down = $ cur_votes [1] +1;
$ q =更新线程SET votes_down = $ votes_down WHERE id = $ id;
}
$ r = mysql_query($ q);
if($ r)
{
$ effectiveVote = getEffectiveVotes($ id);
echo $ effectiveVote。votes;
}
elseif(!$ r)//投票失败
{
echoFailed!;
}
?>
解决方案
您要求 0
$ votes
。这是一个不包含该键的数组。
数组 $ votes
没有设置,所以当PHP正尝试访问数组的键 0
,它会遇到[0]和[1]的未定义偏移量并引发该错误。
如果你有一个数组:
$ votes = array('1','2', '3');
我们现在可以访问:
$票[0];
$ votes [1];
$ votes [2];
如果我们尝试访问:
$票[3];
我们将会收到错误Notice:Undefined offset:3
I am getting this PHP error, what does it mean?
Notice: Undefined offset: 0 in
C:\xampp\htdocs\mywebsite\reddit_vote_tut\src\votes.php on line 41
From this code:
<?php
include("config.php");
function getAllVotes($id)
{
$votes = array();
$q = "SELECT * FROM entries WHERE id = $id";
$r = mysql_query($q);
if(mysql_num_rows($r)==1)//id found in the table
{
$row = mysql_fetch_assoc($r);
$votes[0] = $row['votes_up'];
$votes[1] = $row['votes_down'];
}
return $votes;
}
function getEffectiveVotes($id)
{
$votes = getAllVotes($id);
$effectiveVote = $votes[0] - $votes[1]; //ERROR THROWN HERE
return $effectiveVote;
}
$id = $_POST['id'];
$action = $_POST['action'];
//get the current votes
$cur_votes = getAllVotes($id);
//ok, now update the votes
if($action=='vote_up') //voting up
{
$votes_up = $cur_votes[0]+1; //AND ERROR THROWN HERE
$q = "UPDATE threads SET votes_up = $votes_up WHERE id = $id";
}
elseif($action=='vote_down')
{
$votes_down = $cur_votes[1]+1;
$q = "UPDATE threads SET votes_down = $votes_down WHERE id = $id";
}
$r = mysql_query($q);
if($r)
{
$effectiveVote = getEffectiveVotes($id);
echo $effectiveVote." votes";
}
elseif(!$r) //voting failed
{
echo "Failed!";
}
?>
解决方案
You are asking for the value at key 0
of $votes
. It is an array that does not contain that key.
The array $votes
is not set, so when PHP is trying to access the key 0
of the array, it encounters an undefined offset for [0] and [1] and throws the error.
If you have an array:
$votes = array('1','2','3');
We can now access:
$votes[0];
$votes[1];
$votes[2];
If we try and access:
$votes[3];
We will get the error "Notice: Undefined offset: 3"
这篇关于注意:未定义偏移:0英寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文