homeHome contactContact portfolioPortfolio musicMusic videoVideo historyHistory calendarCalendar rssRSS
karlpox | with GOD all things are possible

PHP Discovery

I just discover a shortcut with PHP and MySQL. First I`ll discuss my problem with you. My problem was I wanted to create a array in my database that would support all the categories my post would need.

For example this post is under the category PHP+MySQL and Tips & Tricks, and I don`t want to create a new table for that so what I did was put everything under 1 field kinda like an array on your database.

For example PHP+MySQL has an ID of 1 and Tips & Trick has and ID of 2. So when I save it it looks like this ,1,2,. Now the data type of this is varchar. So it would be saved in my database like that. So when I want to use that data to view all my categories I simply use the explode function. And since its a varchar after the using the explode function it would automatically be transformed to an integer which I can use as a foreign key to access the ID number on the category table

<?php

//Im using the example above under the variable example.
//Im using the explode function that would break the string every occurrence of a comma (,)
$example = `,1,2,`;
$temp=explode(`,`,$example);

//Use the sizeof function to get the array size of the array $temp
$size=sizeof($temp);
//Increase the $size due to excess of , in the string
$size--;

//I use connect.php to connect directly to my database. Its much easier that way.
include(`connect.php`);

//start with 1 because the string started with a comma.
for ($a=1; $a<$size; $a++)
{
//Your query.
$q="select * from category where cat_id like `$temp[$a]`;
$r=mysql_query($q);
$row=mysql_fetch_array($r,MYSQL_ASSOC);
//Output
echo $row[`cat_name`].`<br>`;
}

?>

By this code I simple broke the string $example to be used as a foreign key for my category table. Take note that the $example variable also came from my database which is my way of saving an array. Hope you get the idea!

Search a string in a string PHP

This is how you search a string from a string. For example you have this sentence.

“I went to the mall yesterday.”

So lets store that in an variable.

$source = "I went to the mall yesterday.";

Now lets look for the word mall. To do that analyze the code below.

if ( strtr($source,"mall") )
{
echo 'String Found!';
}
else
{
echo 'String not Found!';
}

Hope this code can be useful!

Using the explode function PHP

We can use the explode function to separate a sentence into two or more. The string is being separated via a string too.

The explode function will look for a string in a string and separates the string when he encounter the string it is looking for.

To use this function lets take this string for example.
Read more »

Replace a character in a String PHP

This functions replaces all the character in a string to a new character set by the user. So if the string contains 10 character `a` in a string and the users wants to replace it with `b` so all the letter `a` in the string will now then be letter `b’.

So to use that function.
Read more »

Using Sessions

How to use sessions

What is session anyways? According to php.net session is a “way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.” As for me session is like a super global array which can be passed to different pages using the session_start() function. Sessions are very useful especially an on page that requires users to be logged. Because sessions can track weather a user is still logged or not. So let’s get started using sessions in log in cases.

First of all you have to insert the session_start() function at the most top of your coded. So it must be found on line 0 of your codes. For example.

<?php session_start(); ?>

But some servers like mine displays error even if the session_start() is already at the most top of your code so you add this to your code to solve this problem.

<?php
session_cache_limiter(`nocache`);
session_start();
?>

And you need to do a session_start() on every page if you want the user to be logged in order to access the page. So now how do we determine if the session of the user is still active or not? So after you check if the username & password of the user is correct you create a session for the user to know if the user is still active or not. So after checking if the user is valid you do this.

<?php
$_SESSION[`active`] = true;
?>

The word `active` is not constant, you can change it to anything you want. You can even change it to something like $_SESSION[`weeee`] = active; it really doesn’t matter as long as you remember it. So to check if the user is still active, on every page that requires a user to be logged at the most top you do this. Read more »

Avoiding Repost on Refresh

Thanks to Ronald Borla and Google for the idea.

To avoid reposting datas from your form if your going to refresh the page. This problem is very common on forms being submitted to the same page where the form is. The simple solution to this problem is to make a unique session for ever form being submitted. Analyze the code below carefully and you can apply it easily in your website.

<form action="" method="POST">
<input type="hidden" name="uid" value="<?php $_SESSION['uid']=uniqid(); echo $_SESSION['uid']; ?>" />
<p>
<label>Message: </label>
<input type="text" name="msg" />
</p>
<input type="submit" value="Submit" />
</form>

As you can see we have created a unique session via the uniqid() function. Since you are refreshing on the same page make sure to have this on the same page as well most commonly above the form.
<?php
if ( $_POST['uid'] == $_SESSION['uid'] )
{
//your code to be process will be here
//and after processing your code you create another unique session id>
$_SESSION['uid'] = uniqid();
}
?>
 
By those code above you are guaranteed you will avoid reposting of datas upon refresh of page.

A Simple Login Code using PHP+MySQL

First of all you must create a table. For a example we name this table as member.

id username password
1 karlo 123
2 toni 143

So first you must create your own form. That looks like any ordinary login box with a username, password and a login button.

<form method=”POST” action=”">
<p>
<label>Username: </label>
<input type=”text” name=”username” />
</p>
<p>
<label>Password: </label>
<input type=”password” name=”password />
</p>
<p>
<input type=”submit” value=”Login” />
</p>
</form>

As you can see i have set the action=”" to empty so you have to change it to something like process_login.php or any .php page that would verify if the user input is valid or not.
Read more »

Simple guide to PHP+MySQL

Well to solve some of your problems here are some easy guides on how to connect yo MySQL using PHP.

 

first of all this guides uses WAMP you can download it @ WAMP SERVER.

 

So after installing WAMP. You would first need to create a database. Its easy all you have to do is go to http://localhost/phpmyadmin and you can see the create database button there. Just input the database name you want then press create.

 

Afterwards you have to choose your database on the right side and lets start creating some table!

 

First create a simple notepad. And put this codes in side the notepad and save it as anything you want.

 

Create table member
(
m_id int unsigned not null auto_increment primary key,
username varchar(100) not null,
password varchar(100) not null
);
 

 

This means you will create a table with the name of member. With attributes m_id as a unsigned integer and its default value is not null this attribute also does auto_increment and is your primary key for this table. Next you have the m_username & m_password as varchar with a maximum character size of 100 and its not null too.

 

After saving this you will need to import them using phpMyAdmin. You can easily see the import button on the header of phpMyAdmin. After selecting the file and importing voila you have your very own table!

 

To simply add data into your table you must first creat a form. Well i know you can do this without me teaching you. So next after clicking on the submit button you would be directed to the next page and this is where the coding starts!

 

Lets store the datas posted first into variables.

 

$username = $_POST['username'];
$password = $_POST['password'];

 

username & password is not constant it simply depends on whats the name of the input you have coded in the form.

 

Next you have to conncet to a database.

 

$dbhost = ‘localhost’;
$dbuser = ‘dcwarez_karlpox’;
$dbpass = ’secret’;
$db=mysql_connect($dbhost, $dbuser, $dbpass) or die(’Error connecting to mysql’) or die(’Error : ‘ . mysql_error());
$dbname = ‘example’;
mysql_select_db($dbname) or die(’Error : ‘ . mysql_error());

 

Well this code simply means that i have connected to the mysql database with those information. And choose the example as my database (lets just say you have created example as database name from above). Now if you would probaby be wondering whats that dcwarez_karlpox is and secret. Well those are priveledges. You can create them on the phpMyAdmin home page. A priveledge link can be seen there and you have to create your own user. But you can still use the default user which is

 

$dbuser = ‘root’;
$dbpass = ”;

 

With those information above you can connect to a MySQL if you are using WAMP.

 

Now to add datas in your table you can simply do this query

$q="insert into member (m_username,m_password) values (’".$username."’, ‘".$password."’)";

$r=mysql_query($q);

 

Take not that i didnt include the m_id. Simply because its already auto_increment you dont have to add data into it anymore.

Next is to View data from a table you simply do this code.

 

$q="select * from member"

$r=mysql_query($q);

while($row=mysql_fetch_array($r,MYSQL_ASSOC))

{

echo ‘Username: ‘.$row['username'].’ Password: ‘.$row['password'].’<br/>’;

}

 

This code will output all the username and password under  the member table. To get specific datas only you do this by using the where and like functions. Example.

 

$q="select * from member where m_username like ‘karlo’ and m_password like ’sobiaco’;

$r=mysql_query($q);

$row=mysql_fetch_array($r,MYSQL_ASSOC));

echo ‘Username: ‘.$row['username'].’ Password: ‘.$row['password'].’<br/>’;

 

Take note that i didnt use any looping for this. Simply because you are using a member table and you must not have any duplicates in right? Well its your job to find a way to avoid duplicates!

 

Next is to delete data from a database. You simply do this by this code.

$q="delete from member where m_username like ‘karlo’;

$r=mysql_query($q);

This code simply deletes all the rows in the table member that has a m_username of karlo.

 

Lastly we have the UPDATE. To do this just analyze the code below.

 

$q="update member set m_username = ‘karlpox’ where m_username like ‘karlo’";

$r=mysql_query($q);

 

This code simply updates all m_username with a value of karlo into karlpox. You can also update more datas by using this query.

 

$q="update member set m_username = ‘karlpox’, m_password=’12345′ where m_username like ‘karlo’";

 

Now that query replaces both the m_username & m_password of m_username’s which has a value of karlo.

 

Thats all!