#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Can any one help me to convert this php to jsp(small one)?


    PHP Code:
    <?php

    $conn 
    = @mysql_connect('127.0.0.1','root','root');
    if (!
    $conn) {
        die(
    'Could not connect: ' mysql_error());
    }

    mysql_select_db('mydb'$conn);

    ?>
    PHP Code:
    <?php

    $page 
    = isset($_POST['page']) ? intval($_POST['page']) : 1;
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
    $offset = ($page-1)*$rows;
    $result = array();

    include 
    'conn.php';

    $rs mysql_query("select count(*) from users");
    $row mysql_fetch_row($rs);
    $result["total"] = $row[0];

    $rs mysql_query("select * from users limit $offset,$rows");
    $items = array();
    while(
    $row mysql_fetch_object($rs)){
        
    array_push($items$row);
    }
    $result["rows"] = $items;

    echo 
    json_encode($result);

    ?>
    PHP Code:
    <?php

    $id 
    intval($_REQUEST['id']);

    include 
    'conn.php';

    $sql "delete from users where id=$id";
    $result = @mysql_query($sql);
    if (
    $result){
        echo 
    json_encode(array('success'=>true));
    } else {
        echo 
    json_encode(array('msg'=>'Some errors occured.'));
    }

    ?>
    PHP Code:
    <?php

    $firstname 
    $_REQUEST['firstname'];
    $lastname $_REQUEST['lastname'];
    $phone $_REQUEST['phone'];
    $email $_REQUEST['email'];

    include 
    'conn.php';

    $sql "insert into users(firstname,lastname,phone,email) values('$firstname','$lastname','$phone','$email')";
    $result = @mysql_query($sql);
    if (
    $result){
        echo 
    json_encode(array('success'=>true));
    } else {
        echo 
    json_encode(array('msg'=>'Some errors occured.'));
    }

    ?>
    PHP Code:
    <?php

    $id 
    intval($_REQUEST['id']);
    $firstname $_REQUEST['firstname'];
    $lastname $_REQUEST['lastname'];
    $phone $_REQUEST['phone'];
    $email $_REQUEST['email'];

    include 
    'conn.php';

    $sql "update users set firstname='$firstname',lastname='$lastname',phone='$phone',email='$email' where id=$id";
    $result = @mysql_query($sql);
    if (
    $result){
        echo 
    json_encode(array('success'=>true));
    } else {
        echo 
    json_encode(array('msg'=>'Some errors occured.'));
    }

    ?>
    Last edited by requinix; August 19th, 2013 at 07:49 PM. Reason: reformatted, [php] tags
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    What part do you need help with? It looks pretty straight forward. However, I have to warn you that a straight translation of this code from PHP to JSP will result in code that is just as bad as this PHP.

    Basically you can translate this code pretty litterally - open a database connection (google for "JDBC connection JSP"), grab some parameters (request.getParameter or other ways - google for "read parameters jsp") and do some database manipulation (likely gone over in the first google search)

    Having said that, no Java programmer who isn't an ex-PHP programmer would ever do it this way. If this isn't a small learning example then I'd really encourage you to learn a bit about what it is you're trying to do

    Comments on this post

    • requinix agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0
    Why is this PHP bad? The translation is already done

IMN logo majestic logo threadwatch logo seochat tools logo