hlo, i have a code... plz tell through this how to show table data from mysql?
tell me the sql query or code and the location where i will insert that

customers.php

<?php 

class Customers
 {

private $name;

private $id;

private $desc;

private $con;

public function __construct($name,$desc)
{

   $this -> name = $name;
   $this-> desc = $desc;
   $this -> con = mysql_connect("localhost", "root", "");
   $this -> db  = mysql_select_db('cust');
}

public function insertData($desc_tbl)
{

   $sql = " insert into $desc_tbl values (NULL,'".$this->name."','".$this->desc."');";
   $res = mysql_query($sql);
   mysql_query($res);  
echo "<center>Record Inserted</center><br>";

$res = mysql_query("SELECT name,desc FROM desc_tbl");


}

public function deleteData($id)
{
    $sql = " delete from desc_tbl";
    $res = mysql_query($sql);
    mysql_query($res);
echo "<center>Record Deleted</center><br>";
}

public function fetchData($id,$name,$desc)
{
    $sql = " select * from desc_tbl";
    $res = mysql_query($sql);
    mysql_query($res[$id]);
echo "<center>Data is</center><br>";

}

public function updateData($desc_tbl,$name,$desc,$id)
{
    $sql = " update desc_tbl set name='$name' , desc='$desc'";
    $res = mysql_query($sql);
    mysql_query($res);
echo "<center>Record Updated</center><br>";
}

}
?>



test.php
--------


<?php 
require_once("customers.php");
$name = "John";
$desc ="Test Desc";
$id = "6";

$c = new Customers($name,$desc);
echo $c->insertData("desc_tbl");



//$c =new Customers($name,$desc,$id);
//echo $c->deleteData("desc_tbl");


?>

Thanks

Recommended Answers

All 2 Replies

change your fetchData method

public function fetchData()
{
    $sql = " select * from desc_tbl";
    $res = mysql_query($sql);

    $data = array();
    while($row = mysql_fetch_assoc($res)){
        $data[] = $row;
    }

    retrun $data;

}

in your test.php

you can then create a table that will echo out the fetchData()

Also this isn't as much related to your original post but I noticed this or something similar appears in almost all your functions:

$res = mysql_query($sql);
mysql_query($res);

I'm not entirely sure what your logic is here but this is essentially calling mysql_query() once to run an SQL query, and then attempting to call it again but passing a resultset instead of an SQL query, which at best will not do anything. You may need to look up specific examples for how to run a SELECT, INSERT, UPDATE, etc query in PHP. Generally speaking INSERT/UPDATE/DELETE queries you run mysql_query() and then mysql_affected_rows() to check if the change was successful. With SELECT queries you run mysql_query() and then run functions like mysql_num_rows($result) to get the number of rows and mysql_fetch_assoc($result) to fetch a row. Poster above me showed an example of mysql_fetch_assoc() to fetch multiple rows of data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.