matthew61773 Posted August 24, 2006 Share Posted August 24, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/18538-php-mysql-help/ Share on other sites More sharing options...
wildteen88 Posted August 24, 2006 Share Posted August 24, 2006 I got just the site for you [url=http://www.php-mysql-tutorial.com/]php-mysql-tutorial.com/[/url]. 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. Quote Link to comment https://forums.phpfreaks.com/topic/18538-php-mysql-help/#findComment-79836 Share on other sites More sharing options...
ronverdonk Posted August 24, 2006 Share Posted August 24, 2006 I am too helpful for this. Only this time: here is your code sample. Do the error checking yourself.[code]<?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 tablefor ($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);?>[/code]Ronald 8) Quote Link to comment https://forums.phpfreaks.com/topic/18538-php-mysql-help/#findComment-79865 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.