0

Dear friends,
Please guide me I want to display data in PHP form from mysql database. my code is below

<?PHP

//connect to database
$username = "root";
$password = "";
$database = "mydatabase";
$server = "127.0.0.1";

$conn= mysqli_connect($server,$username,$password,$database);
       // checkintin connection

       if($conn->connect_error){
                              die("".$conn->connect_error);
        }

             // select data

    $sql = "SELECT * FROM myguests";
    $result=$conn->query($sql);

                               if($result->num_row>0){
                               //output data of each row
                                    while($row=$result->fetch_assco()){
                                          echo "ID:".$row["id"];

                                    }
                                }   
                                    else{

                                    echo "no any record is in table";
                                        }

    $conn->close();                             



?>

what is there problem?

6
Contributors
16
Replies
70
Views
2 Years
Discussion Span
Last Post by zebnoon
Featured Replies
  • 1

    Maybe line 23: `while($row=$result->fetch_assco()){...` It should probably be: while($row=$result->fetch_assoc()){... *assoc* stands for associative; this method returns a row in an associative array where field names are keys. Read More

  • **WOOOOOOOOOOWW... That was sooooo noob from my part...** The problem with your script is that you connect "procedurally" and then work with mysqli "objectually". Mysqli has dual interfaces: a procedural one and an object-oriented one. You connected to the database using the procedural interface, but after that you worked with … Read More

1

Maybe line 23: while($row=$result->fetch_assco()){...

It should probably be:

while($row=$result->fetch_assoc()){...

assoc stands for associative; this method returns a row in an associative array where field names are keys.

Edited by broj1

-1

or maybe you can use fetch_array().

 $query = mysql_query(select column from table);

 while($row=mysql_fetch_array($query)){ 
    echo $row['column'];
 }
Votes + Comments
Gives a mysql answer to a mysqli question...
0

dear Friends,
here is error message....

Notice: Trying to get property of non-object in C:\wamp\www\db\viewdb.php on line 31

and line no 31 is

$result->row_num>0;
Please, guide me...

0

Dear Adrian!
yes i used

if($result->num_rows){
                     while($row=$result->fetch_assoc()){

                                               echo "".$row["id"];


                     }

}

but error message is same...plz help me.

Trying to get property of non-object in C:\wamp\www\db\viewdb.php on line 31

and 31 line is
if($result->num_rows>0)

0

Trying to get property of non-object in C:\wamp\www\db\viewdb.php on line 31

That means that $result is false because your query failed.

0

yes priteas, no any record shown .... but why?My query seems correct......

$sql="SELECT * FROM mydatabase";

plz guide me.

1

WOOOOOOOOOOWW... That was sooooo noob from my part...

The problem with your script is that you connect "procedurally" and then work with mysqli "objectually". Mysqli has dual interfaces: a procedural one and an object-oriented one. You connected to the database using the procedural interface, but after that you worked with a $conn which was not an object (I sure hope that you understand what I mean...).

Instead of connecting like this:
$conn= mysqli_connect($server,$username,$password,$database);

..., you should connect like this:
$conn = new mysqli($server,$username,$password,$database);

Edited by Adrian_5

0

dear Adrian here is all my correct code..

$username = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";

$conn= new mysqli($server,$username,$password,$database);
       // checkinting connection

       if($conn->connect_error){
                        die("connection failed:".$conn->connect_error);
        }
       //select data from database

        $sql="SELECT id,firstname FROM myguests";
        $result=$conn->query($sql);


     **if($result->num_rows>0){** //in this line is shown error of non object //property

           While($row=$result->fetch_assoc()){
                    echo "ID: ".$row["id"];        

           }
         }
         else{
              echo"record 0";
              }
              $conn->close();
?>

is there anything wrong? But same message is yet...:(((((((

0
if(!$result){
die($conn->error);}

here is correct code solution of my problem..anyways thank u for alls.

0

Dear Friends,
Please , check this code for registration form PHP ,data is not entered in database what is problem?

 $sql="SELECT L1 FROM login1 WHERE L1='$uname'";
                     $result=$conn->query($sql);

                     if ($result->num_rows==1){
                       echo("user name is not available ...");

                     }
                     else{
                              $query="INSERT INTO login1(L1,L2) VALUES ('$uname',$pword)";
                              if($conn->query($query)==true){
                              echo ("data has entered sucessfully...");
                              }
                              else{
                                echo("data is not entered due to any problem");
                              }

                     //echo("User name is available..");

                     }

after exicuted got
message "data is not entered due to any problem"

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.