Seach

Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

how to create a autocomplete text box using php,jquery,mysql

select.php
<?php
@mysql_connect("localhost","root","");
@mysql_select_db("example");
$query="select * from auto";
$tempdata=@mysql_query($query);
$name=array();
while($data=@mysql_fetch_assoc($tempdata))
{
$name[]=$data["name"];
}
echo json_encode($name);
?>

page.php
<head>
    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
      <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
      <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>


    <div class="ui-widget">
    <input type="text" id="automplete-1">
    </div>

<script type="text/javascript">
$(document).ready(function()
{
    var name=<?php include 'select.php'; ?>;
    $("#automplete-1").autocomplete({
        source: name,
        autoFocus:true
    });
});
</script>


how to update database in php using oops concept


public function update($array,$table,$id)//1st paramerter is associative array,2nd is table name,3rd is id
{
    $query="UPDATE $table SET ";
    foreach($array as $key=>$result)
    {
        $query.=$key."="."'$result'".","; //here $key is list of column names and $result is the value corresponding then
       
    }
    $str=strlen($query)-1; //used to find out index of the last , in the $str field
$temp_query=substr_replace($query," ",$str);//used to replace , with " "
$temp_query.=" WHERE id=".$id;
$status=$this->execute_query($temp_query); //executing the result see previous post for what is execte_query()
return $status;
   
}

how to count number of data in a table using oops in php

public function count_data($table)
{
   
    $query="SELECT * FROM $table";
    $result=$this->execute_query($query);
    return @mysql_num_rows($result);   
}

how to select database through oops concept

public function selectdb($dbname)
{
    $this->dbname=$dbname;
if($this->status)
{
    @mysql_select_db($this->dbname);
}
}
you must add class structure before using it create $dbname variable @ prefix used before mysql_select_db for faster execution

how to insert data to database using object oriented approach any table data can be inserted using this code block

public function execute_query($query)
{
    return  $result=@mysql_query($query);
   
}
public function insert($table,$data)

    while( @list($key,$value) = @each($data))
    {
        $field_names[] = $key;
            if((strpos($value,'now()') === false ) and (strpos($value,'date()')=== false) and (strpos($value,'DATE_ADD')=== false))
            $field_values[] = "'$value'";
            else
            $field_values[] = $value;
    }
$query = "INSERT INTO $table (";
$query .= implode(', ', $field_names);
$query .= ") VALUES (" . implode(",", $field_values) . ")";
if($this->execute_query($query))
{
    return true;
    }
    else
    {
        return false;
    }

}

how to connect to database in php using object oriented approach

class database
{
private $dbname;
private $host;
private $user;
private $password;
private $status;
private $result;
private $connection;
public function connect($hostname,$username,$password)
{
    $this->host=$hostname;
    $this->user=$username;
    $this->password=$password;
    if($this->connection=@mysql_connect($this->host,$this->user,$this->password))
    {
        $this->status=true;
    }
    else
    {
    echo "cannot connect to server ".$this->host;
    }       
}
}
at the function call just provide 3 parameters according to your database $this is used to access class variables




how to retrive data from database using codeigniter 3

Blog_view.php

<html>
<body><form method="post">
<table border="1">
<th>Serial Number</th>
<th>Name</th>
<?php
foreach($query as $result)
{?>
<tr>
<td>
<?php echo $result['id'];?>
</td>
<td>
<?php echo $result['name'];?>
</td>
</tr>    
<?php   
}
?>
</table>
</form>
</body>
</html>



how to retrive data from database using codeigniter 2

Blog_view.php
<?php

class Blog_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
   
public function getall()
{
    $this->load->database();//used to load database
$query=$this->db->get('student_details');//used to fetch data from student_details
return $query->result_array();//used to return the result
   
}
}
?>


how to retrive data from database using codeigniter 1

1)download and extract codeigniter to wamp folder
2)open application/config/database.php then change the following
$db['default']['hostname'] = 'your hostname';
$db['default']['username'] = ''your mysql user name";
$db['default']['password'] = ''your mysql password";
$db['default']['database'] = 'your database name';
3)open config folder under that config.php then add
$config['index_page'] = 'index.php'; //this is the page loaded when you press the folder
4)create 3 files under controller,model.view
under controller create Blog.php,under model Blog_model.php,under view create
Blog_view.php

Blog.php

<?php
class Blog extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Blog_model');//used to load the model class
       
    }
   
    public function index()
    {
        $data['query']=$this->Blog_model->getall();//used to call model function
       
if(count($data)>0)
      
        {
                $this->load->view('Blog_view',$data);//used to call view class
       
        }
        else
        {
            echo 'nothing to show';
            exit();
        }
    }
   
}

?>

how to create a table through code in php

<?php
@mysql_connect('localhost','root','');
@mysql_select_db('db');
$query='CREATE TABLE IF NOT EXISTS demo
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(40) NOT NULL DEFAULT ""
)
';
if(@mysql_query($query))
{
    echo 'table created';
}
else
{
    echo 'table not created';
  }
 
?>

How to create a database through code in php

<?php
mysql_connect("localhost","root","");
$query="CREATE DATABASE IF NOT EXISTS db";
$result=mysql_query($query);
if($result)
{
    echo 'database created';
}
else
{
    echo 'database not created';
}

?>