How can i insert and retrieve records in php.give me a simple way.?

3 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer
  • 1 decade ago

    Lets say you have the table below...

    tblPerson

    forename Surname Address

    bob wilson 32 fake road

    mark christon 1 fake road

    to add a row you could do something like below...

    connect to the database

    <?php

    $Conn = mysql_connect("localhost", "myuser", "mypassword");

    mysql_select_db("dbTest", $Conn);

    ?>

    Now build your insert Query

    <?php

    $sql = "INSERT INTO tblPerson (Forename, Surname, Address) VALUES ('mick', 'johnson', '12 fake avenue')";

    // Run the insert

    // Tell it what the query is and the connection

    mysql_query($sql, $Conn);

    ?>

    To read...

    <?php

    $sql = "select * from tblPerson"

    // Handle / pointer to the results if you will

    $rs = mysql_query($sql);

    // while $rw contains an array of a row, each time the loop runs

    // it will grab the next row of data, so 1st time it will be bob, second time mark when there are no more rows it will stop since $rw will be equal to null

    while (($rw = mysql_fetch_assoc($rs)) != null)

    {

    echo $rw['Forename']; // would print the forename

    }

    ?>

  • 1 decade ago

    execute the insert with mysql_query for example. the select is the same, but to iterate throught the results, look in php.net for an example. Greets

Still have questions? Get your answers by asking now.