In a message dated 2/27/2003 12:47:25 PM Pacific Standard Time,
[EMAIL PROTECTED] writes:

>First time poster here and a 1 week old PHP/Mysql programmer
>
>I have bought several books and scoured the net for any and all PHP/MySQL
>tutorials. Here is the problem I am running into:
>
>1. The docs tell how to access MySql through the MySql monitor
>2. any docs that tutor how to make PHP interact with MySql has you doing all
>the functions directly on the page
>3. All the rest are written like VCR instructions (Mine still blinks
>"12:00")
>
>What I am looking for is a tutorial that talks specifically about the
>interaction of PHP forms with MySql
>
>I have figured out how to add records using PHP, and have even figured out a
>crude search function, but being able to update or delete records are
>eluding me.

Here's a mini-tutorial. This is how it basically works. These are the steps:

1. connect to the database
2. send an SQL query to the database
3. If the query sends back some data, it stores it in a temporary table and
you need to get it from that table and do whatever you want to do with it.

Here's code:

For step 1.
$connection = mysql_connect("host","user","password")  or die("can't
connect");
$db = mysql_select_db("dbname") or die("can't connect to db");

For step 2.
$sql = "UPDATE tablename SET custname='$custname'";
{or whatever SQL query you want to execute. If you have books about MySQL,
they will describe the SQL queries you can use.)
$result = mysql_query($sql) or die("couldn't execute query");

For step 3.
If your query returns data that you want to use, such as a SELECT query, the
data is stored in a temporary table and you can use $result to get data from
the temp table. And then you do whatever you want with it. 

To get one row of data, you can use this code:
$row = mysql_fetch_array($result);

Row is now an array with all the fields you selected. You can get any
particular field using $row['fieldname']. You can break it apart using
extract:

extract($row);

Which turns the array into separate variables, given the field names, such as
$fieldname = valuefromdb.

If you have more than one row in your results, you can get all the rows using
a while loop, for example:

while($row = mysql_fetch_array($result))
{
  extract($row);
  echo $custname,$custaddress;
}

This will go through all the rows, echoing the values from the fields,
custname and custaddress.

That's basically it. There are other ways to do these things. But you can do
the basic things you need to do using these PHP statements. You can also look
up the mysql functions in the PHP online manual
(http://www.php.net/manual/en/ref.mysql.php) to see other possibilities for
interacting with MySQL databases from PHP scripts.

The MySQL online manual has a tutorial chapter that you can use. It describes
SQL using the mysql client, but you can read it just to see how to use the
SQL queries. Just put any of the queries described into the $sql variable
below, and that will work. In summary:

$connection = mysql_connect("host","user","password")  or die("Can't connect
to MySQL");
$db = mysql_select_db("dbname") or die("Can't select db");
$sql = "SQL query here";
$result = mysql_query($sql) or die("Couldn't execute query");
And the following only if the query was a SELECT
$row = mysql_fetch_array($result);
extract($row);

Good luck, 

Janet

______________________________
Janet Valade
Author, PHP & MySQL for Dummies
janet.valade.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to