Jump to content


Photo

php MySQL help


  • Please log in to reply
2 replies to this topic

#1 matthew61773

matthew61773
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 24 August 2006 - 02:07 PM

question I have been set and am having triouble with!

Write a PHP script, including the approriate SQL, that shows how to read data from and write data to the following MySQL database table

(PRODUCT)

Prod-Id            Product      Price      No_in_stock
    0                  Hammer      £5.00            22
    1                  Pump          £3.00            4   
    3                  Spanner      £2.00            6

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 August 2006 - 02:12 PM

I got just the site for you php-mysql-tutorial.com/. Go through tutorial numbers 4 through to 7. Go through number 3 first for the SQL basics.

What you learn there should help you complete you mini assignment.

#3 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 24 August 2006 - 02:59 PM

I am too helpful for this. Only this time: here is your code sample. Do the error checking yourself.
<?php
// Setup your article array, you can do this a zillion different ways
$a = array();
$a[0]['id']    = 0;
$a[0]['prod']  = 'Hammer';
$a[0]['price'] = 5.00;
$a[0]['no']    = 22;
$a[1]['id']    = 1;
$a[1]['prod']  = 'Pump';
$a[1]['price'] = 3.00;
$a[1]['no']    = 4;
$a[2]['id']    = 3;
$a[2]['prod']  = 'Spanner';
$a[2]['price'] = 2.00;
$a[2]['no']    = 6;

// setup connection to db
$conn = mysql_connect("localhost", "ronverdonk", "ronnie09") or die(mysql_error());
mysql_select_db("vwso") or die(mysql_error());

// execute the CREATE TABLE statement
$create = "CREATE TABLE IF NOT EXISTS myproducts ( 
                          id          INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
                          prod_id     INT NOT NULL, 
                          product     VARCHAR(64) NOT NULL, 
                          price       DECIMAL(5,2) NOT NULL, 
                          no_in_stock INT NOT NULL)";
$res = mysql_query($create) or die(mysql_error());

// insert each array entry into the table
for ($i=0; $i < count($a); $i++) {
   $var1 = $a[$i]['id'];
   $var2 = $a[$i]['prod'];
   $var3 = $a[$i]['price'];
   $var4 = $a[$i]['no'];
   $insert = "INSERT INTO myproducts (prod_id, product, price, no_in_stock) VALUES (
                  $var1, '$var2', $var3, $var4)";
   $res = mysql_query($insert) or die(mysql_error());
}

// SELECT all rows from table
$b = array();
$select = "SELECT prod_id, product, price, no_in_stock FROM myproducts";
$res = mysql_query($select) or die(mysql_error());
// retrieve all rows from SELECT result and store in array
$i=0;
while ($row = mysql_fetch_assoc($res)) {
   $b[$i]['id']    = $row['prod_id'];
   $b[$i]['prod']  = $row['product'];
   $b[$i]['price'] = $row['price'];
   $b[$i]['no']    = $row['no_in_stock'];
   $i++;
}
print '<pre>';
print_r($b);
?>

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users