Recent Articles
Home » Posts filed under MYSQL
Showing posts with label MYSQL. Show all posts
Showing posts with label MYSQL. Show all posts
Thursday, December 5, 2013
Thursday, December 5, 2013
- 0 Comments
Hiiiiii, people who are new-new php developers came across a situation when they have to develop a user login system. It seems to be a very hard to develop login system by validating user details from database for the new developers but actually this task is very. You can send the username and password values to the next page or same page by just html form POST method or using AJAX. But this tutorial is taking into cosideration that a developer is not having or completely unaware of knowledge of how to use AJAX for data sending , So, we will be just using core php to implement a login system.
I this tutorial, user is validated with the values exist i database, session variables like user id & username are set so that you can use that variables on anyother pages, form variables are sent to next page.
Demo :
Code Begins :
File Structure :
Main Directory --
--> check.php
--> index.php
--> home.php
check.php :
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('test', $link);
if (!$db_selected) {
die ('Can\'t use test table : ' . mysql_error());
}
$username=$_POST['username'];
$password=$_POST['password'];
$query="select * from user where username='".$username."'";
$result=mysql_query($query);
$records=mysql_fetch_array($result);
if($records)
{
if($password==$records['password'])
{
session_start();
$_SESSION['username']=$records['username'];
$_SESSION['userid']=$records['id'];
header('Location: home.php');
}
else
{
$msg="Password Incorrect";
header('Location: index.php?msg='.$msg);
}
}
else
{
$msg="Username Incorrect";
header('Location: index.php?msg='.$msg);
}
?>
index.php :
<!DOCTYPE HTML> <html> <head> <title>Login System</title> </head> <body> <h1>Login Form</h1> <?php if(isset($_GET['msg'])) { $msg=$_GET['msg']; echo '<p><font color="red">'; echo $msg; echo '</font></p>'; } ?> <form action="check.php" method=POST> <label for="username">Username</label> <input type="text" name="username" id="username" placeholder="username"><br> <label for="password">Password</label> <input type="password" name="password" id="password" placeholder="password"><br><br> <input type="submit" value="Submit"> </form> </body> </html>
home.php :
<?php
session_start();
$username=$_SESSION['username'];
$userid=$_SESSION['userid'];
echo "You Are Logged IN -<b>".$username."</b> and your ID is -<b>".$userid."</b>";
?>
To run this tutorial , you need a database and a table containing users records. In this tutorial I am using a database namely - "test" with a table name - "user". This "user" table is having three columns as follows : id, username and password .
Saturday, October 26, 2013
Saturday, October 26, 2013
- 0 Comments
Hiiiiiii, This post is for all those who are developing custom Wordpress plugin and for those who want to write some custom codes to the wordpress folders and they want to execute some database query in the same manner as wordpress does.
The steps shown in this post are very easy and by using these steps you can very frequently execute any of your query in wordpress.
Steps To Execute Query In Wordpress :
1. Include the "wp-load.php" file in your code. "wp-load.php" is placed in the main wordpress directory in wp-admin,wp-content is placed. If I am developing a custom file in wp-admin directory then I'll write "require_once('../wp-load.php');" to include this "wp-load.php" file.
2. Write "global $wpdb;" to use wordpres global object "$wpdb" and functions performed by it.
3. Then write your query as shown below :
$sql="update ".$wpdb->prefix."posts SET
post_title = '".$title."',
post_content = '".$contecnt."'
WHERE post_id = '".$post_id."'";
Here $wpdb->prefix returns the prefix you have used for your database tables at the time of installation. I have used wp_ as a prefix for my tables so by writing $wpdb->prefix."posts" , I'll get my table with tablename as "wp_posts".
4. Now write $wpdb->query( $sql); to execute your query.
Hope you like this post……..Please Comment…………!!!!!!!!!
Wednesday, October 23, 2013
Wednesday, October 23, 2013
- 0 Comments
Hiiiiii, we know that when we write a insert query and we have to insert a new data and column in a Insert Query then it is very easy but what happen you have add new data in new column name in somewhat big query, It will be very difficult to do so. For that purpose , in this post I'll be telling you how to write this much flexible query in which you can add new data and column name very easily.
In the query writing method which I'll be showing here, it is very easy to add new column data and also this query looks good and is in easy readable form.
CODE BEGINS :
$insert_sql =" insert into tablename SET
username = '".$username."',
comment = '".$comment."',
star = '".$star."',
date_from = '".$date_from."',
date_to = '".$date_to."',
added_on = NOW(),
email = '".$email."',
status = 'unapproved' ";
username = '".$username."',
comment = '".$comment."',
star = '".$star."',
date_from = '".$date_from."',
date_to = '".$date_to."',
added_on = NOW(),
email = '".$email."',
status = 'unapproved' ";
Hope you like this post……..Please Comment…………!!!!!!!!!
Monday, September 30, 2013
Monday, September 30, 2013
- 0 Comments
Hiii, we all know that using with functions in our code makes our workflow very easier and our work also gets easy. It is very good if we have the already built raw database functions for basic and simple tasks like database insertion and edit. So, here is the simple class and function to create fancy and flexible ADD and EDIT FUNCTIONS.
Here's the code :
1. FIrstly create a class (dbClass.php) for all database functions like add and edit as shown below:
<?php
class dbScheme
{
/** database connection username*/
var $dbUser = "";
/**database connection password*/
var $dbPass = "";
/**database name*/
var $dbName = "";
/** database connection host*/
var $dbHost = "localhost";
/**database connection link*/
<?php
class dbScheme
{
/** database connection username*/
var $dbUser = "";
/**database connection password*/
var $dbPass = "";
/**database name*/
var $dbName = "";
/** database connection host*/
var $dbHost = "localhost";
/**database connection link*/
}

class dbClass extends dbScheme
{
/** database query execution result*/
var $res;
/**temporary variable for miscallenous purposes*/
var $temp;
/** temporary variable for miscallenous purposes*/
var $dbType = 'mysql';
{
/** database query execution result*/
var $res;
/**temporary variable for miscallenous purposes*/
var $temp;
/** temporary variable for miscallenous purposes*/
var $dbType = 'mysql';
/** Buffer variable for setting the flag of query buffering.*/
var $dbBuffer = true;
var $dbBuffer = true;
/**Sql variable for sql statement.*/
var $sql;
/**database connection link*/
var $dbLink;
/**database selected Resource*/
var $sql;
/**database connection link*/
var $dbLink;
/**database selected Resource*/
var $dbConn;
/** Constructor of class*/
function dbClass( $dbName=NULL, $dbUser=NULL, $dbPass=NULL, $dbHost=NULL, $dbType=NULL )
{
$this->dbName = ( !empty( $dbName ) )?$dbName:$this->dbName;
$this->dbUser = ( !empty( $dbUser ) )?$dbUser:$this->dbUser;
$this->dbPass = ( !empty( $dbPass ) )?$dbPass:$this->dbPass;
$this->dbType = ( !empty( $dbType ) )?$dbType:$this->dbType;
$this->dbHost = ( !empty( $dbHost ) )?$dbHost:$this->dbHost;
function dbClass( $dbName=NULL, $dbUser=NULL, $dbPass=NULL, $dbHost=NULL, $dbType=NULL )
{
$this->dbName = ( !empty( $dbName ) )?$dbName:$this->dbName;
$this->dbUser = ( !empty( $dbUser ) )?$dbUser:$this->dbUser;
$this->dbPass = ( !empty( $dbPass ) )?$dbPass:$this->dbPass;
$this->dbType = ( !empty( $dbType ) )?$dbType:$this->dbType;
$this->dbHost = ( !empty( $dbHost ) )?$dbHost:$this->dbHost;
}
/** Function to connect to the database*/
function connect()
{
if( $this->dbType = 'mysql' )
{
//$this->dbLink = mysql_connect($this->dbHost,$this->dbUser,$this->dbPass,1,131072) or die("Cannot connect with specified database".mysql_error());
$this->dbLink = mysql_pconnect($this->dbHost,$this->dbUser,$this->dbPass,131072) or die("Cannot connect with specified database".mysql_error());
$this->dbConn = mysql_select_db( $this->dbName , $this->dbLink );
return $this->dbLink;
}
}
/** Function to Disconnect to the database*/
function disconnect()
{
if( $this->dbType = 'mysql' )
{
mysql_close($this->dbLink);
}
}
/** Function to execute a query in the database*/
function query( $sql )
{
$this->res = mysql_query( $sql) or die( "Error in sql statement:<br/>".$sql."<br/>".mysql_error());
$this->temp = mysql_insert_id();
return $this->res;
}
/** Function to insert record in a database*/
function insert_into_table($table_name, $arr_fields){
function connect()
{
if( $this->dbType = 'mysql' )
{
//$this->dbLink = mysql_connect($this->dbHost,$this->dbUser,$this->dbPass,1,131072) or die("Cannot connect with specified database".mysql_error());
$this->dbLink = mysql_pconnect($this->dbHost,$this->dbUser,$this->dbPass,131072) or die("Cannot connect with specified database".mysql_error());
$this->dbConn = mysql_select_db( $this->dbName , $this->dbLink );
return $this->dbLink;
}
}
/** Function to Disconnect to the database*/
function disconnect()
{
if( $this->dbType = 'mysql' )
{
mysql_close($this->dbLink);
}
}
/** Function to execute a query in the database*/
function query( $sql )
{
$this->res = mysql_query( $sql) or die( "Error in sql statement:<br/>".$sql."<br/>".mysql_error());
$this->temp = mysql_insert_id();
return $this->res;
}
/** Function to insert record in a database*/
function insert_into_table($table_name, $arr_fields){
if(count($arr_fields)>0){
$i = 0;
$sql = "insert into ".$table_name."( ";
foreach($arr_fields as $field => $field_value ){
$sql .= ($i==0)?"":",";
$sql .= $field;
$i++;
}
$i = 0;
$sql = "insert into ".$table_name."( ";
foreach($arr_fields as $field => $field_value ){
$sql .= ($i==0)?"":",";
$sql .= $field;
$i++;
}
$sql .=") values(";
$i = 0;
foreach($arr_fields as $field => $field_value ){
$sql .= ($i==0)?"":",";
$sql .= " '".$field_value."' ";
$i++;
}
$sql .=")";
$this->query($sql);
}
$i = 0;
foreach($arr_fields as $field => $field_value ){
$sql .= ($i==0)?"":",";
$sql .= " '".$field_value."' ";
$i++;
}
$sql .=")";
$this->query($sql);
}
return "Record Added Successfully";
}
/** Function to update records in a database */
function update_table($table_name, $arr_fields, $primary_key, $primary_key_id){
}
/** Function to update records in a database */
function update_table($table_name, $arr_fields, $primary_key, $primary_key_id){
if(count($arr_fields)>0 && $primary_key!="" && $primary_key_id!=""){
$i = 0;
$sql = "update ".$table_name." set ";
foreach($arr_fields as $field => $field_value ){
$sql .= ($i==0)?"":",";
$sql .= $field."= '".$field_value."' ";
$i++;
}
$sql .=" where ".$primary_key."='".$primary_key_id."' ";
$this->query($sql);
}
return "Record Updated Successfully";
}
}
?>
$i = 0;
$sql = "update ".$table_name." set ";
foreach($arr_fields as $field => $field_value ){
$sql .= ($i==0)?"":",";
$sql .= $field."= '".$field_value."' ";
$i++;
}
$sql .=" where ".$primary_key."='".$primary_key_id."' ";
$this->query($sql);
}
return "Record Updated Successfully";
}
}
?>
2. Then create your custom class (in my case abc.php) extending (dbClass.php ) write the code given below :
<?php
class abc extends dbClass{
var $table_name;
class abc extends dbClass{
var $table_name;
/** Constructor to specify table name which will used in the whole document */
function abc($table_name)
{
$this->table_name = $table_name;
}
function abc($table_name)
{
$this->table_name = $table_name;
}
/** Function to add/edit a record from a table */
function addEditabc($arr_fields, $primary_key_name, $primary_key_value)
{
if($primary_key_value!="" && $primary_key_value!=0 && !empty($primary_key_value))
{
$prompt = $this->update_table($this->table_name, $arr_fields, $primary_key_name, $primary_key_value);
}
else
{
$prompt = $this->insert_into_table($this->table_name, $arr_fields);
}
return $prompt;
}
function addEditabc($arr_fields, $primary_key_name, $primary_key_value)
{
if($primary_key_value!="" && $primary_key_value!=0 && !empty($primary_key_value))
{
$prompt = $this->update_table($this->table_name, $arr_fields, $primary_key_name, $primary_key_value);
}
else
{
$prompt = $this->insert_into_table($this->table_name, $arr_fields);
}
return $prompt;
}
}
?>
?>
Note: If you are using this method, then you have to pass an array to addEditabc() function as parameter which must contain all the values/indexes with the same name as that of database table.
Hope you like this post……..Please Comment…………!!!!!!!!!
Monday, September 23, 2013
Monday, September 23, 2013
- 0 Comments
Hiii, you all know the basic sql queries like insert,update etc but majority of us are unaware of many useful functions that mysql provides.
Lets take an example that if you want to update a column value – “www.example.com” in a table to value – “localhost/example”. then you will simply use a update query as :
update tablename set columnnname=”localhost/example” where =”www.example.com”;
and your work will be done but what if you want to replace -”www.example.com” in column having value- -”www.example.com/categories” with value -”localhost/example” to make the final value of column look like – “localhost/example/categories”……..this can’t be useful if records are very large like if you want to setup a wordpress site running live to your local system…….then this replacement with updation is done by mysql replace() function .
replace() function syntax :
replace(columnname,”value to be replace”,”value to be replace with”);
replace() function usage :
UPDATE tablename SET columnname = REPLACE(columnname, 'http://www.example.com', ‘localhost/example’);
the above mentioned query replaces every 'http://www.example.com' with ‘localhost/example’ in that column whose name is mentioned as columnname in that particular table mentioned in tablename.
Hope you like this post……..Please Comment....!!!!!!
Originally posted at Mycodestock.
Subscribe to:
Posts (Atom)



