Boolean Island !

Entries categorized as ‘mysql’

Bangla Web App

May 20, 2008 · 5 Comments

Last day one of my friend came to me.He was facing a problem (he solved later).Here is his problem –
The Bangla/Unicode data become ?????? in his page.

I’m gonna tell u today step-by-step what u need to do if u wanna develop Bangla/Unicode based Web App.

#1 . First make the database with CHARACTER utf8 and collation_connection =’utf8_general_ci’
#2 . Make the Tables with same configuration.
#3 . When u connect the database use following 2 lines just below the mysql_select_db()

mysql_query(’SET CHARACTER SET utf8′);
mysql_query(”SET SESSION collation_connection =’utf8_general_ci’”);

#4 . Set the META-TAG as “Content-Type: text/html; charset=UTF-8″

Thats it :D u r done.Here is a sample page what will show data from database

<?
header(’Content-Type: text/html; charset=UTF-8′); //As its php page i dont need the meta-tag so i need to send a header .

mysql_connect(’localhost’,'xxxxx’,'xxxxxx’) or die(’Error In connection’);
mysql_select_db(’test’) or die(’Error In connection(DB)’);

mysql_query(’SET CHARACTER SET utf8′);
mysql_query(”SET SESSION collation_connection =’utf8_general_ci’”);

$re = mysql_query(”SELECT * FROM test”) or die(’Query Problem’);
while($row = @mysql_fetch_assoc($re))
echo $row['name'].PHP_EOL;
?>

Categories: Bangla computing · PHP · Tutorial · Web Developing · localization · mysql · programming
Tagged: , ,

মুক্ত জবস

April 5, 2008 · 14 Comments

শেষ পর্যন্ত জবার বেজ এর লোকালাইজেশনে সফল হলাম :) । এই প্রজেক্টটা অনেক আগে শুরু করে ছিলাম কিন্তু শেষ করতে পারছিলাম না । এটা আমার প্রথম বাংলা প্রজেক্ট ছিল । অনেক কিছুই নতুন শিখলাম । অমিহাসিন ভাইকে আগেই ধণ্যবাদ জানিয়ে ছিলাম এই প্রজেক্টটা আমাকে দেওয়ার জন্য । প্রথমে মনে হয়েছিল যে খুব একটা কঠীন হবে না , আসলেও কঠীন না কিন্তু তারপর ও শেষ করতে পারছিলাম না । জবার বেজ এর ডেভেলপেরকে জানালাম লাভ হলো না । সে বলে সব ঠিক আছে কিন্তু আমার কাছে ঠিকমত কাজ করছিল না । তারপর তুহিন ভইকে জানালাম সমস্যার কথা । উনি আমাকে মোরশেদ ভাই এর সাথে কথা বলতে বললেন , এবং সমস্যার সমাধান হলো । সমস্যাটা ছিল ডাটাবেজে :( । শুধু মাত্র collation type utf8_general_ci করাতে ঝামেলা মিটে গেল :) । গতকাল রাতে জবারবেজ বাংলায় মুক্ত জবস নামে হোসটিং করা হয়েছে । খুব তাড়াতাড়িই ওটা শুরু হবে ।

Categories: ACSE · JavaScript · Linux · PHP · Web Developing · mysql · programming
Tagged: , , ,

Pagination By php & mysql

April 3, 2008 · 3 Comments

Just yesterday i was finding a solution to view data. Actually its called “Pagination” .I have so many row/entity in a table and i want to show 10/5 entity per page then what i have to do ? I just design a algo for this using php and mysql but its very easy and a little bit long so i made a post on phpxperts and Aman Bhai ans me about it.Here it is..

1. Obtain the required page number

This code will obtain the required page number from the $_GET array. Note that if it is not present it will default to 1.

if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
} // if

2. Identify how many database rows are available

This code will count how many rows will satisfy the current query.

$query = “SELECT count(*) FROM table WHERE …”;
$result = mysql_query($query, $db) or trigger_error(“SQL”, E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

3. Calculate number of $lastpage

This code uses the values in $rows_per_page and $numrows in order to identify the number of the last page.

$rows_per_page = 15;
$lastpage = ceil($numrows/$rows_per_page);

4. Ensure that $pageno is within range

This code checks that the value of $pageno is an integer between 1 and $lastpage.

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
} // if
if ($pageno < 1) {
$pageno = 1;
} // if

5. Construct LIMIT clause

This code will construct the LIMIT clause for the sql SELECT statement.

$limit = ‘LIMIT ‘ .($pageno - 1) * $rows_per_page .‘,’ .$rows_per_page;

6. Issue the database query

Now we can issue the database qery and process the result.

$query = “SELECT * FROM table $limit;
$result = mysql_query($query, $db) or trigger_error(“SQL”, E_USER_ERROR);
… process contents of $result …

7. Construct pagination hyperlinks

Finally we must construct the hyperlinks which will allow the user to select other pages. We will start with the links for any previous pages.

if ($pageno == 1) {
echo ” FIRST PREV “;
} else {
echo ” <a href=’{$_SERVER['PHP_SELF']}?pageno=1′>FIRST</a> “;
$prevpage = $pageno-1;
echo ” <a href=’{$_SERVER['PHP_SELF']}?pageno=$prevpage‘>PREV</a> “;
} // if

Next we inform the user of his current position in the sequence of available pages.

echo ” ( Page $pageno of $lastpage ) “;

This code will provide the links for any following pages.

if ($pageno == $lastpage) {
echo ” NEXT LAST “;
} else {
$nextpage = $pageno+1;
echo ” <a href=’{$_SERVER['PHP_SELF']}?pageno=$nextpage‘>NEXT</a> “;
echo ” <a href=’{$_SERVER['PHP_SELF']}?pageno=$lastpage‘>LAST</a> “;
} // if

But the main thing is its same algo which one i designed. :)

Note : Above tutorial is collected from this link .

Categories: PHP · Web Developing · mysql · programming

SQL Injection Again ??

March 18, 2008 · 3 Comments

Few days ago i wrote a post on SQL Injection and its protection.Here is a better way to protect the Database from SQL Injection.Just write a function like this

function safe_query( $value )
{
if( get_magic_quotes_gpc() )
{
$value = stripslashes( $value );
}
//check if this function exists
if( function_exists( “mysql_real_escape_string” ) )
{
$value = mysql_real_escape_string( $value );
}
//for PHP version < 4.3.0 use addslashes
else
{
$value = addslashes( $value );
}
return $value;
}
Now use it like this
$id = safe_query($_POST['product_id']);
$sql = “Select quantity,price from $product where id=’$id’ ;”;
$re = mysql_query($sql);

Categories: PHP · Web Developing · mysql · programming
Tagged: ,

SQL Injection,R u safe ?

March 3, 2008 · 3 Comments

Hello guys today i’ll discuss about SQL Injection.This is a technique by with any hacker can destroy your database.

Example : Suppose our page is

include ‘connect.php’;
$pass=$_POST['pass'];
$user=$_POST['user'];
$sql=”select * from $table where pass=’$pass’ and name=’$user’;”;
$re=mysql_query($sql);

if(mysql_num_rows($re)==1)
{
$_SESSION['db_is_loggged_in']=true;
header(’location:admin.php’);
exit();
}
else echo “Wrond pass or user name”;

if any user input is like “‘OR 1″ then somthing will happen unexpected.Hackers can do something like more.But how can we protect our page ?

Solution :

// Make a safe query
$query = sprintf(”INSERT INTO products (`user`, `pass`,) VALUES (’%s’, ‘%s’), mysql_real_escape_string($product_name, $link),
mysql_real_escape_string($product_description, $link),”;

Now u can protect ur database from hackers. :)

Categories: PHP · Web Developing · mysql · programming
Tagged: , ,

Error With MAX() in MySQL

February 29, 2008 · 3 Comments

Today i was trying to get the maximum value of a column in mysql.In mysql “select max(column_name) from table_name ;” works fine.But i was facing problem to get the value by mysql_query() in php.My code was

$re = mysql_query(”Select max(id) from test”);
echo $re;

It shows wrong output.

I just change it to

$sql=”select max(id) from test;”;
$val=mysql_query($sql);
$row=mysql_fetch_assoc($val);
echo $row['max(id)'];

And its work fine :)

Categories: PHP · Web Developing · mysql · programming
Tagged: ,