1

I am using the code below to return value from sql but the value is displayed 2 times.

$cc=mysqli_connect($server,$user,$pass);
mysqli_select_db($cc,$database);
$sql = "SELECT b_id FROM ``ub_per`` WHERE ``b_email`` = '$mail'";
$res = mysqli_query($cc,$sql);
$row = mysqli_fetch_array($res);
foreach($row as $value){
  echo($value);
}
7
  • 1
    Use single backticks ` for your table and column name. Commented Oct 19, 2015 at 8:07
  • Possibly the content of $mail is contained twice in the table? Use SELECT DISTINCT. Commented Oct 19, 2015 at 8:08
  • have you took a look to your table to see if it must display only one value ? Commented Oct 19, 2015 at 8:08
  • show table structure in your question please Commented Oct 19, 2015 at 8:10
  • 1
    use mysqli_fetch_assoc Commented Oct 19, 2015 at 8:40

2 Answers 2

1

I think the answer is a little more complex than that. So the row $row = mysqli_fetch_array($res); will return an array, I think if you check there will be 2 items in it. Looking like this

$row[0] = id;
$row['b_id'] = id;

now you do

foreach($row as $value){
  echo($value);
}

So will echo id out twice. Use $row = mysqli_fetch_array($res,MYSQLI_ASSOC); To get you what you want.

Sign up to request clarification or add additional context in comments.

Comments

0

It is because query gives you 2 results(rows). Try:

echo '<pre>';
print_r($row);

And you will see if there are more than 1 rows in results.

1 Comment

actually the field i am trying to return is set as unique. so there isno chance of 2 results. but still i can't understand why i am getting this problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.