Entries categorized as ‘mysql’
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
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: Bangla, Bangla computing, Unicode based web app
Categories: ACSE · JavaScript · Linux · PHP · Web Developing · mysql · programming
Tagged: jobberbase in bangla, jobberbase localization, l10n, localization
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;
}
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 ($pageno < 1) {
$pageno = 1;
}
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> “;
}
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> “;
}
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
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 );
- }
- if( function_exists( “mysql_real_escape_string” ) )
- {
- $value = mysql_real_escape_string( $value );
- }
- 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: protect sql injection, sql injection
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: hacking, sql hacking, sql injection
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: MAX+mysql, mysql+php+max