PHP with MySQL by dffhrtcv3

VIEWS: 6 PAGES: 27

									PHP with MYSQL

Please use speaker notes for
  additional information!
<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", "password")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_close($link);
?>
<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", "password", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");

$result = @mysql_query("SELECT idno from donor")
                                                                          My database as
                                                   My table.
    or die("Invalid query: " . mysql_error());                            assigned by
while ( $row = mysql_fetch_row($result))
                                                                          Nevidia.
{
   echo ("<p>" . $row[0] . "</p>");
}
                                              I am now looping
?>                                            through the table
                                              showing one row at a
                                              time starting the index
                                              at 0.
<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", ”password", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");

$result = @mysql_query("SELECT idno from donor")
    or die("Invalid query: " . mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
   echo ("<p>ID= ". $row["idno"] ." </p>");
}
?>
<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", "password", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");

$result = @mysql_query("SELECT idno, name from donor")
    or die("Invalid query: " . mysql_error());

while ($row = mysql_fetch_array($result))
{
   echo ("<br> ID = ". $row["idno"] . "<br> NAME =   " . $row["name"] . "<br>");
}
?>
<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", ”password", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");

$result = @mysql_query("SELECT * from donor")
    or die("Invalid query: " . mysql_error());

while ($row = mysql_fetch_array($result))
{
   echo ("<br> ID = ". $row["idno"] . "<br> NAME = " . $row["name"] . "<br>");
   echo("CITY = " . $row["city"] . "<br> STATE = " . $row["state"] . "<br>");
}
?>
<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", ”password", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");
$state="RI";
$result = @mysql_query("SELECT * from donor where state = '$state'")
    or die("Invalid query: " . mysql_error());

while ($row = mysql_fetch_array($result))
{
   echo ("<br> ID = ". $row["idno"] . "<br> NAME = " . $row["name"] . "<br>");
   echo("CITY = " . $row["city"] . "<br> STATE = " . $row["state"] . "<br>");
}
?>
<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", ”password", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");
$idno='11111';
$result = @mysql_query("SELECT * from donor where idno = '$idno'")
    or die("Invalid query: " . mysql_error());
$row = mysql_fetch_array($result);
   echo ("<br> ID = ". $row[0] . "<br> NAME = " . $row["name"] . "<br>");
   echo("CITY = " . $row["city"] . "<br> STATE = " . $row["state"] . "<br>");

?>
<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", "whatever", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");
$sql = "INSERT INTO donor values ('33333', 'Al Richards', '111 South St', 'Braintree', 'MA',
                     '02184','2002-10-10',300, 'John Smith')";
$result= mysql_query($sql,$link) or die(mysql_error());
echo $result;
?>                                                        Note that I did the insert as one long
                                                           string that I had to wrap to show on the
                                                           slide.




                                                      I then ran connect7.php
                                                      to show that the record
                                                      got added.
<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", "whatever", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");
$sql = "INSERT INTO donor values ('44444', 'Sarah Grove', '123 Elm St', 'Seekonk', 'MA',
               '02345','2001-10-12',300, 'John Smith')";
$result= mysql_query($sql,$link) or die(mysql_error());
echo $result;                                                 Again I wrapped for the slide, the
?>                                                            insert is one line in my code.




                                                Indicates
                                                successful
                                                execution.




                                                                 When I executed
                                                                 insert1a.php a second
                                                                 time this error
                                                                 message was returned.
See insert on
previous slide
<?php
     $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", "whatever", "pgrocer_biz_data")
         or die("Could not connect: " . mysql_error());
     print ("Connected successfully");
     mysql_select_db("pgrocer_biz_data");
$sql = "INSERT INTO donor values ('45654', 'John Doe', '123 Elm St', 'Swansea', 'MA', '02345',
                    '2001-10-12',300, 'John Smith')";
if (mysql_query($sql,$link))                            Again note the wrap that is not in the code.
   {
     echo "record added";
   }
else
   {
     echo "must have been a duplicate";
   }
?>




                                                                     When I ran insert1b.php
                                                                     a second time I got my
                                                                     duplicate message.
<?php
     $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", "whatever", "pgrocer_biz_data")
         or die("Could not connect: " . mysql_error());
     print ("Connected successfully ");
     mysql_select_db("pgrocer_biz_data");
$getidno='45654';
$sql = "DELETE FROM donor WHERE idno = '$getidno'";
if (mysql_query($sql,$link))
   {
     echo "record deleted";
   }
?>
<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", "whatever", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");
$getidno = '33333';
$sql = "UPDATE donor SET stadr = '111 North St' WHERE idno = '$getidno'";
$result= mysql_query($sql,$link) or die(mysql_error());
echo $result;
?>
The update
changes the
street name
from South
to North.
<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", "whatever", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");

$result = @mysql_query("SELECT * from donor")
    or die("Invalid query: " . mysql_error());

while ($row = mysql_fetch_array($result))
{
   echo ("<br> ID = ". $row["idno"] . "<br> NAME = " . $row["name"] . "<br>");
   echo ("STREET ADDRESS = " . $row["stadr"] . "<br>");
   echo("CITY = " . $row["city"] . "<br> STATE = " . $row["state"] . "<br>");
}
?>
<html>
<head>
<title>Insert</title>
</head>
<body>
<form action="insert2.php" method="POST">
IDNO:
<input type="text" name="inidno">
<br>NAME:
<input type="text" name="inname">
<br>STREET ADDRESS:
<input type="text" name="instadr">
<br>CITY:
<input type="text" name="incity">
<br>STATE:
<input type="text" name="instate">
<br>ZIP:
<input type="text" name="inzip">
<br>DATE FIRST CONTRIBUTED:
<input type="text" name="indatefst">
<br>YEAR GOAL:
<input type="text" name="inyrgoal">
<br>CONTACT:
<input type="text" name="incontact">
<br>
<input type="submit" value="Click">
</form>
</body>
</html>
<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", "whatever", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");
$idno=$_POST[inidno];
$name=$_POST[inname];
$stadr=$_POST[instadr];
$city=$_POST[incity];                                                  The code has the insert on
$state=$_POST[instate];                                                one line. I wrapped to fit
$zip=$_POST[inzip];
$datefst=$_POST[indatefst];
                                                                       on the slide.
$yrgoal=$_POST[inyrgoal];
$contact=$_POST[incontact];
$sql = "INSERT INTO donor values ('$idno', '$name', '$stadr', '$city', '$state',
                          '$zip', '$datefst', '$yrgoal', '$contact')";
$result= mysql_query($sql,$link) or die(mysql_error());
echo $result;
?>
<html>
<head>
<title>Insert</title>
</head>
<body>
<form action="delete2.php" method="POST">
Enter the identification number for the record you want to delete.<br><br>
IDNO:
<input type="text" name="inidno">
<input type="submit" value="Click">
</form>
</body>
</html>




<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", "whatever", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");
$idno=$_POST[inidno];
$sql = "DELETE FROM donor WHERE idno = '$idno'";
$result= mysql_query($sql,$link) or die(mysql_error());
echo $result;
?>
<html>
<head>
<title>Insert</title>
</head>
<body>
<form action="update2.php" method="POST">
Change the name for the following id.<br><br>
IDNO:
<input type="text" name="inidno">
<br>NAME:
<input type="text" name="inname">
<br>
<input type="submit" value="Click">
</form>
</body>
</html>



<?php
    $link = mysql_connect("mysql2.nevidia.com", "pgrocer.biz", "whatever", "pgrocer_biz_data")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_select_db("pgrocer_biz_data");
$idno=$_POST[inidno];
$name=$_POST[inname];
$sql = "UPDATE donor SET name = '$name' WHERE idno = '$idno'";
$result= mysql_query($sql,$link) or die(mysql_error());
echo $result;
?>

								
To top