Friday, October 10, 2014

MySql query to select count of only max value from table in PHP

CREATE TABLE tableName  // Give any name as u like
(
     id int auto_increment primary key,
     party varchar(20),
     constituency varchar(30),
     votes int(11)
);

Then insert some details.
I inserted these details :)

INSERT INTO tableName
          (party, constituency,votes)
VALUES
          ('BJP', 'Surat',1200),
          ('Congress', 'Surat',1100),
          ('BJP', 'Baroda',800),
          ('Congress', 'Baroda',900);

Query :)

mysql_query("select max(votes) from tableName group by constituency");

Output :)


Wednesday, October 8, 2014

PDO search database using LIKE.

First create database and name it as u like. I create database named with `fdb`. And then create table with 4 columns.

DEMO :)

Then insert some details


Example :)

/* here project is the database table name */

<?php
try{
$bdd=new PDO("mysql:host=localhost;dbname=fdb","root","");
}
catch(exception $e){
die("ERROR : ".$e->getMessage());
}

$keyword='ma';  /* You can use any characters as keywords */

$sql="SELECT * FROM project WHERE fullname LIKE :keyword;";
$q=$bdd->prepare($sql);
$q->bindValue(':keyword','%'.$keyword.'%');
$q->execute();

while ($r=$q->fetch(PDO::FETCH_ASSOC)) {
   echo"<pre>".print_r($r,true)."</pre>";
}
?>

Output :)