• Archives

  • Archive for the ‘PHP’ Category

    Connecting to a database

    Tuesday, December 18th, 2007

    Connecting to a database through php is not a tough task.

    First off all create a new sql database in phpmyadmin and see the privileges section in it for the user/pass by default it is

    Username == “root”

    Password == “”

    In php you need this and your host name it is always localhost

    Syntax:


    $host = "localhost"; //Host name
    $username = "root"; // Username
    $pass = ""; //Password
    $dbname = "useless"; //Database Name

    $connect = mysqli_connect ($host,$username,$pass,$dbname);
    if(! $connect)
    {
    echo “unable to connect to the database”;
    }
    else{
    echo “Connected to database”;
    }
    ?>
    The above script will connect you to the database note you must php 5
    because it uses mysqli functions

    Thanks
    krates

    Using for each to loop through an array

    Sunday, December 16th, 2007

    Sometimes you need to loop through an for this the for each function is used for example.

    $grade = array(“first_name” => “First Name”,
    “middle_name” => “Middle Name”,
    “last_name” => “Last Name”,
    “phone” => “Phone”);

    foreach($grade as $field => $gard)
    {
    echo ”
    $gard The name of the array $field

    “;
    }
    ?>

    Notice the variable $grade arrays starting from first_name is given to $field and then the array value is given to label

    U can use this in many forms

    Thanks
    krates

    Sending a mail from your script

    Thursday, December 13th, 2007

    Many a times you want to send a mail to someone through your website you can easily do that by the php inbuilt function mail

    Syntax:

    mail(to,subject,message,headers,parameters);

    To: == The person email you are sending the mail to

    Subject: == The subject of the email

    Message: == The main email message can consist of the new line character ( \n ) tab character ( \t ) etc

    Parameters: == Optional parameters

    Creating the script to send a mail:

    <?php
    $to = “someone@someone.com”;

    $subject = “Checking Mail Function”;

    $message = “This is the body of the email.”;

    $from = “something@something.com”;

    $headers = “From: $from”;

    mail($to,$subject,$message,$headers);

    echo “Mail Sent.”;
    ?>

    Now this script every time when it will run it will send a mail to the email someone@someone.com

    You can add user input to it by using if , isset functions

    Hint: isset is a function which checks that a form field is filled or not used in db functions also

    Thanks

    krates

    File upload in php

    Saturday, December 1st, 2007

    If you want users to upload files you can do this by using php

    HTML FILE

    <form action=”upload.php” method=”post”
    enctype=”multipart/form-data”>
    <input type=”file” name=”upload” />
    <br />
    <input type=”submit” name=”submit” value=”Submit” />
    </form>

    The file of yours will now consist of a browse button through which a user can select the file he wants to upload

    The upload.php will display the file information and upload the file to the folder upload

    UPLOAD.PHP

    <?php
    if(isset($_FILES[‘upload’])){if ($_FILES[“file”][“error”] > 0)
    {
    echo “Contain error ” . $_FILES[“file”][“error”] . “<br />”;
    }
    else
    {
    echo “File name ” . $_FILES[“file”][“name”] . “<br />”;
    echo “File Type: ” . $_FILES[“file”][“type”] . “<br />”;
    echo “File size: ” . ($_FILES[“file”][“size”] / 1024) . ” Kb<br />”;
    echo “Temp file: ” . $_FILES[“file”][“tmp_name”] . “<br />”;if (file_exists(“upload/” . $_FILES[“file”][“name”]))
    {
    echo $_FILES[“file”][“name”] . ” already exists. “;
    }
    else
    {
    move_uploaded_file($_FILES[“file”][“tmp_name”],
    “upload/” . $_FILES[“file”][“name”]);
    echo “Stored in: ” . “upload/” . $_FILES[“file”][“name”];
    }
    }
    }
    else{
    echo ‘where have you come from ha’;
    }?>
    
    

    Create both the files and upload to the server then create a folder named upload [important] Or else you will get error.

    When done check

    Thanks
    krates

    Numeric array’s in php

    Friday, November 30th, 2007

    Sometimes you need to create many similar variables. So you need arrays

    There are three different kind of arrays:

    • Numeric array
    • Associative array
    • Multidimensional array

    In this post we will only discuss Numeric array:

    Syntax:

    $yourname = array(“Kushagra”,”Sanjay”,”Salman”);

    or

    $yourname[0] = “Kushagra”;

    $yourname[1] = “Sanjay”;

    $yourname[2] = “Salman”;

    Example 1

    <?php

    $yourname[0] = “Kushagra”;

    $yourname[1] = “Sanjay”;

    $yourname[2] = “Salman”;

    echo $yourname[0].” Was first named “.$yourname[1].” And then “.$yourname[2];

    ?>

    Thanks

    krates