注意:未定义的偏移量:0英寸

时间:2011-07-01 14:51:06

标签: php html

我收到这个PHP错误,这是什么意思?

Notice: Undefined offset: 0 in 
C:\xampp\htdocs\mywebsite\reddit_vote_tut\src\votes.php on line 41

从这段代码:

<?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!"; 
} 
?>

15 个答案:

答案 0 :(得分:72)

您要求0的密钥$votes的值。它是一个不包含该键的数组。

未设置数组$votes,因此当PHP尝试访问数组的键0时,它会遇到[0]和[1]的未定义偏移量并抛出错误。

如果您有阵列:

$votes = array('1','2','3');

我们现在可以访问:

$votes[0];
$votes[1];
$votes[2];

如果我们尝试访问:

$votes[3];

我们将收到错误“通知:未定义的偏移量:3”

答案 1 :(得分:7)

使用print_r($votes);检查数组$votes,您会看到那里不存在密钥0。它将返回NULL并抛出该错误。

答案 2 :(得分:7)

这个回答帮助了我https://stackoverflow.com/a/18880670/1821607 压榨的原因 - 指数0没有设定。简单的$array = $array + array(null)就行了。或者您应该检查索引0上的数组元素是否通过isset($array[0])设置。第二种变体对我来说是最好的方法。

答案 3 :(得分:3)

getAllVotes()未返回索引为01的数组。通过在结果上调用var_dump()确保它返回您想要的数据。

答案 4 :(得分:2)

首先,检查数组是否确实存在,您可以尝试类似

if (isset($$votes)) {
    // Do bad things to the votes array
}

答案 5 :(得分:1)

正如所解释的那样,因为$ cur_votes [0]中没有数据,因此会抛出错误。为了确保您的代码正常工作,请在执行“$ votes_up = $ cur_votes [0] +1;”之前回显$ cur_votes [0]值以查看是否存储了任何值。当然,没有存储的价值。

答案 6 :(得分:1)

function getEffectiveVotes($id) 

根据函数头,只有一个参数变量($id)。 因此,在第27行,votes[]数组未定义且超出范围。你需要添加另一个 函数头的参数值,以便函数getEffectiveVotes()知道期望两个参数。我生锈了,但这样的事情会起作用。

function getEffectiveVotes($id, $votes)

我不是说这是应该怎么做,但你可能想研究PHP如何 传递其数组并决定是否需要显式声明通过引用传递它

function getEffectiveVotes($id &$votes)    <---I forget, no time to look it up right now.

最后,使用两个参数调用函数getEffectiveVotes(),无论它应该被调用。

干杯。

答案 7 :(得分:1)

尝试使用以下命令播种数据:php artisan db:seed

答案 8 :(得分:0)

如果省略括号,PHP默认会分配键。

试试这个:

$votes = $row['votes_up']; 
$votes = $row['votes_down']; 

答案 9 :(得分:0)

您可能已经知道错误。这是由于尝试访问空数组或尝试访问数组的空键值。在我的项目中,我正在处理这个错误,计算数组并​​显示结果。

你可以这样做:

if(count($votes) == '0'){

    echo 'Sorry, no votes are available at the moment.';
}
else{
    //do the stuff with votes
}

count($votes)计算$votes数组。如果它等于零(0),您可以显示自定义消息或重定向到某个页面,否则您可以使用$votes执行操作。通过这种方式,您可以删除PHP中的Notice: Undefined offset: 0通知。

答案 10 :(得分:0)

我也遇到了这个问题,解决方案很简单,不要在代码中对数组索引位置进行硬编码。
代替 $data[0]['somekey'] foreach($data as $data_item) { echo $data_item['somekey']; }
如果存在数组或更多,您可以在循环内执行所需的操作,但是如果未定义,则不会带来错误。您还可以在此之上添加其他检查。 如果只想要第一个位置或其他内容,也可以添加一个变量并将其在for in循环中递增,以限制循环。

答案 11 :(得分:0)

就我而言,这是一个简单的类型

$_SESSION['role' == 'ge']

我缺少正确的右括号

$_SESSION['role'] == 'ge'

答案 12 :(得分:0)

如果您使用的是dompdf / dompdf,并且在vendor / dompdf / dompdf / src / Cellmap.php中发生错误,则 看来我们在update_row_group方法中使用了错误的帧ID。初步测试似乎证实了这一点。虽然这可能是因为严格来说这是分页表格问题,但我的测试床中没有太多文档具有分页表格。

您可以尝试将800行更改为:

$ r_rows = $ this-> _ frames [$ g_key] [“ rows”];
($ g_key代替$ r_key)

https://github.com/dompdf/dompdf/issues/1295

答案 13 :(得分:0)

改为使用mysql行

mysql_fetch_row($ r)

同时考虑使用mysqli或PDO

?>

答案 14 :(得分:-2)

只是警告使用:

error_reporting(0);

它表示我们不初始化数组并直接为索引赋值。

somefunction{
$raja[0]="this";
$raja[1]="that";
}

代替:

somefunction{
$raja=array(0=>'this',1='that');
//or
$raja=array("this","that");
}

它只是通知,不会产生任何输出错误或任何意外的输出。