Afsar
Afsar

Reputation: 3124

Unable to fetch result in Mysqli [Undefined offset ]

This is my simple query and even it is not executing , my $total is 3;

$pnc = array();

$pnc[] = ('318','259','789');

$total = count($pnc);

for($p=0;$p<$total;$p++)
{
   echo $query = "select `id` from `patents` where `number`=?";
   $stmt = $mysqli->prepare($query) or $mysqli->error ;
   $stmt->bind_param("s",$pnc[$p]);
   $stmt->execute();
   $stmt->bind_result($id);
   $stmt->fetch();
   echo $id;
}

When i am echoing query i am getting

select `id` from `patents` where `number`='318'

and when i am running this query in phpmyadmin its getting 'id' value , but here it is unable to get the id,

Is this the problem occurs due to Undefined offsetbind_param("s",$pnc[$p]); even i am getting value of each element of the array.

Please give a solution for this thanks.

Upvotes: 3

Views: 885

Answers (2)

user1909426
user1909426

Reputation: 1668

My guess is turned error_reporting off, because

 $pnc[] = ('318','259','789');
 $total = count($pnc);

is giving me a syntax error (PHP 5.4.7).

Try changing those two lines to:

$pnc = array('318','259','789');
$total = count($pnc);

Upvotes: 0

swapnesh
swapnesh

Reputation: 26732

Change -

$pnc = array();
$pnc[] = ('318','259','789');
$total = count($pnc);

TO

$pnc = array('318', '259', '789');
echo $total = count($pnc);

Error in your code --> Error

Upvotes: 2

Related Questions