0

i have a problem with my php and sql code. I trying to make code that when i enter some data that related with the database, the output should show 'zero' if the row user_designid equal to 0 and if vice versa it will show 'one'. but the problem is both of output only show the first anwser ('zero').

Php file (co.php is my config)

 <?php
 require "co.php";
 $link = mysqli_connect($h,$u,$p,$db);

 if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
 }
echo "Connected successfully";

$q = "select * from tab_ss_user where User_name ='".$_POST['name']."'";
$result = mysqli_query($link,$q);


if($result){
echo "<table border='1' >
<tr>
<td align=center> <b>user Id No</b></td>
<td align=center><b>Name</b></td>
<td align=center><b>Password</b></td>
<td align=center><b>responsibility</b></td></td>";

while($data = mysqli_fetch_row($result))
{   
echo "<tr>";
echo "<td align=center><option>$data[0]</option></td>";
echo "<td align=center>$data[1]</td>";
echo "<td align=center>$data[2]</td>";

    //this is the problem
if($data['User_DesignID'] == 0 ){
      echo "<td align=center> zero </td>";
 }
else{
      echo "<td align=center> one </td>";
 }


   echo "</tr>";
 }
   echo "</table>";
 }

  else{
   echo ' Failed';
 }
 ?>

that all thank you.

11
  • Any error in the above code execution? Commented Apr 18, 2017 at 5:02
  • 1
    Try using === instead of ==. stackoverflow.com/questions/80646/… Commented Apr 18, 2017 at 5:04
  • 1
    Inside while add this print_r($data); die;. And provide the output. Commented Apr 18, 2017 at 5:06
  • 1
    where you get "$conn" in your code. Use "$link" Commented Apr 18, 2017 at 5:37
  • @MayankPandeyz no error just the output for if($data) not properly function Commented Apr 18, 2017 at 5:59

3 Answers 3

1

You are not referencing the field correctly See enter link description here

When using mysqli_fetch_row, the result is an associative array so $data['User_DesignID'] will not be referenced correctly.

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

1 Comment

that's correct. i should using the column number instead of column name.
0

please make sure your column name is "User_DesignID"
if($data['User_DesignID'] == 0 ){

1 Comment

you can use mysql_fetch_assoc() for using column name
0

(Posted on behalf of the OP).

Thanks to Jeffrey idea, I have found an answer (please refer to Jeffrey's answer on this page).

And also if you want to use column name instead please use mysqli_fetch_assoc instead of row.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.