moesoap Posted January 21, 2012 Share Posted January 21, 2012 Hi Guys, I am a complete novice as you will soon notice. Can anyone suggest what I am doing wrong with this code. When I run the query in phpmyadmin it produces the correct answer. However when I try to output on my site with php it returns the result "Array". I am guessing I have oversimplified somewhere, aint got a clue how though <?php include("configure.php"); // To grab the DB info $dbh = mysql_connect ("localhost", DB_SERVER_USERNAME, DB_SERVER_PASSWORD) or die ('<BR> - Could not connect to the database because: '.mysql_error()); mysql_select_db (DB_DATABASE, $dbh) or die(mysql_error( )); $query = "SELECT `options_values_price` FROM `rain_products_attributes` WHERE `products_id` = 526 AND `options_id` = 3 AND `options_values_id` = 3"; $result = mysql_query($query); if (!$result) { $message = "Error! Invalid Query: ".mysql_error()."\n Original Query: ".$query; die($message); } while($row = mysql_fetch_array($result)) { echo $row; } mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/255479-php-mysql-output-not-displaying/ Share on other sites More sharing options...
Pikachu2000 Posted January 21, 2012 Share Posted January 21, 2012 As the name implies, mysql_fetch_array returns an array. When called with no modifying arguments, it returns an array with both enumerated indices, and associative indices. So because in your query string you've SELECTed one field, rain_products_attributes, the array will hold the indices [0] and ['rain_products_attributes'], which can be accessed via the variable name and index name together as echo $row[0]; or echo $row['rain_products_attributes']; Hopefully, that made some sense. Quote Link to comment https://forums.phpfreaks.com/topic/255479-php-mysql-output-not-displaying/#findComment-1309845 Share on other sites More sharing options...
moesoap Posted January 21, 2012 Author Share Posted January 21, 2012 Thanks for replying. Your answer works but I really dont understand why, I got lost at enumerated indices, and associative indices lol. Quote Link to comment https://forums.phpfreaks.com/topic/255479-php-mysql-output-not-displaying/#findComment-1309848 Share on other sites More sharing options...
Pikachu2000 Posted January 21, 2012 Share Posted January 21, 2012 Maybe this will help . . . Enumerated indices, all three of these produce the same array in the end; an array with numeric indices: $array[0] = 'zero'; $array[1] = 'one'; $array[2] = 'two'; // OR // $array = array( 0 => 'zero', 1 => 'one', 2 => 'two' ); // OR // $array = array( 0 => 'zero', 'one', 'two'); Associative indices, the index names are string values: $array['zero'] = 'zero'; $array['one'] = 'one'; $array['two'] = 'two'; // OR // $array = array( 'zero' => 'zero', 'one' => 'one', 'two' => 'two' ); Quote Link to comment https://forums.phpfreaks.com/topic/255479-php-mysql-output-not-displaying/#findComment-1309853 Share on other sites More sharing options...
Pikachu2000 Posted January 21, 2012 Share Posted January 21, 2012 And the PHP manual section on arrays: http://php.net/manual/en/language.types.array.php Quote Link to comment https://forums.phpfreaks.com/topic/255479-php-mysql-output-not-displaying/#findComment-1309854 Share on other sites More sharing options...
moesoap Posted January 21, 2012 Author Share Posted January 21, 2012 brilliant, thank you for your help. I have a lot of reading to do Quote Link to comment https://forums.phpfreaks.com/topic/255479-php-mysql-output-not-displaying/#findComment-1309855 Share on other sites More sharing options...
Pikachu2000 Posted January 21, 2012 Share Posted January 21, 2012 When first learning the concept of arrays, it may be helpful to think of an array as nothing more than a variable with "pockets" into which you can place data, where the index just identifies the location of the "pocket". So if you put "ten dollars" into the ['left_pocket'] of the $blue_jeans array, you could find your "ten dollars" in $blue_jeans['left_pocket']. Then you could use the "ten dollars" to go get me a 6-pack of beer and a sammich . . . Quote Link to comment https://forums.phpfreaks.com/topic/255479-php-mysql-output-not-displaying/#findComment-1309857 Share on other sites More sharing options...
moesoap Posted January 21, 2012 Author Share Posted January 21, 2012 Lol you have put me in the mood for a pint or two now Quote Link to comment https://forums.phpfreaks.com/topic/255479-php-mysql-output-not-displaying/#findComment-1309859 Share on other sites More sharing options...
moesoap Posted January 23, 2012 Author Share Posted January 23, 2012 Can someone help me further with this as I am completely stumped. I have changed the script so that it will pull prices from my database based upon products_id, options_id and options_value_id. Rather than a hard coded options_id etc. However I am confused on how to get the script to identify what the product_id etc is. The answer would be placed into a css table so that when I update my prices in the database it will automatically update them on the product listing page. Can an if statement include an html input name and value? If so, this may work for me. Although then I would be hardcoding product_id, options_id and options_value_id again. awwww I have included the code for both. Any suggestions/advice is greatly appreciated. <?php include("includes/configure.php"); // To grab the DB info $db = mysql_connect ("localhost", DB_SERVER_USERNAME, DB_SERVER_PASSWORD) or die ('<BR> - Could not connect to the database because: '.mysql_error()); mysql_select_db (DB_DATABASE, $db) or die(mysql_error( )); $productPrices = $query; $query = "select pa.options_values_price from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$_GET['products_id'] . "' and pa.options_id = '" . (int)$products_options_names->fields['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id"; $result = mysql_query($query); if (!$result) { $message = "Error! Invalid Query: ".mysql_error()."\n Original Query: ".$query; die($message); } while($row = mysql_fetch_array($result)){ $productPrices = $row[0]; } mysql_close(); ?> <div class="priceslistfull"><p class="quantity">Quantity</p><p class="productprice1">A4 Posters</p> <p class="productprice2">A3 Posters</p><p class="productprice3">A2 Posters</p><p class="productprice4">A1 Posters</p> <p class="productprice5">A0 Posters</p> <div class="priceunderdiv"><p class="quantityunder">250</p><p class="productprice1under">£<?php echo number_format ($productPrices, 2);?></p> <p class="productprice2under">£<?php echo number_format ($productPrices, 2);?></p><p class="productprice3under">£<?php echo number_format ($productPrices, 2);?></p> <p class="productprice4under">-</p> <p class="productprice5under">-</p></div> <div class="productpurchase"><img src="images/purchase_now1.gif" alt="Spacer Image" width="100" /></div> <div class="productpurchase"><form action="index.php?main_page=product_info&cPath=3_143_38&products_id=526/&action=add_product" method="post" enctype="multipart/form-data" name="cart_quantity" > <input type="hidden" name="cart_quantity" value="1" maxlength="6" size="4" /> <input type="hidden" name="products_id" value="526" /> <input name="image" type="image" title="Add to Cart" src="images/purchase_now.gif" alt="Add to Cart" /> <input type="hidden" value="3" name="id[3]" /> </form></div> <div class="productpurchase"><form action="index.php?main_page=product_info&cPath=3_143_42&products_id=536/&action=add_product" method="post" enctype="multipart/form-data" name="cart_quantity" > <input type="hidden" name="cart_quantity" value="1" maxlength="6" size="4" /> <input type="hidden" name="products_id" value="536" /> <input name="image" type="image" title="Add to Cart" src="images/purchase_now.gif" alt="Add to Cart" /> <input type="hidden" value="3" name="id[3]" /> </form></div> <div class="productpurchase"><form action="index.php?main_page=product_info&cPath=3_143_41&products_id=532/&action=add_product" method="post" enctype="multipart/form-data" name="cart_quantity" > <input type="hidden" name="cart_quantity" value="1" maxlength="6" size="4" /> <input type="hidden" name="products_id" value="532" /> <input name="image" type="image" title="Add to Cart" src="images/purchase_now.gif" alt="Add to Cart" /> <input type="hidden" value="3" name="id[3]" /> </form></div> Quote Link to comment https://forums.phpfreaks.com/topic/255479-php-mysql-output-not-displaying/#findComment-1310264 Share on other sites More sharing options...
litebearer Posted January 23, 2012 Share Posted January 23, 2012 here you are overwriting the value while($row = mysql_fetch_array($result)){ $productPrices = $row[0]; } try... while($row = mysql_fetch_array($result)){ echo $row[0] . "<br />"; } or... while($row = mysql_fetch_array($result)){ $productPrices[] = $row[0]; } print_r($productPrices); Quote Link to comment https://forums.phpfreaks.com/topic/255479-php-mysql-output-not-displaying/#findComment-1310267 Share on other sites More sharing options...
moesoap Posted January 23, 2012 Author Share Posted January 23, 2012 Hi litebearer, How can I identify which product_id, option_id, and option_value_id is to be printed without hard coding each individual product_id etc in the script. is there anyway to link the <input value="526" /> and the <input type="hidden" value="3" name="id[3]" /> from the html to determine the output of the script? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/255479-php-mysql-output-not-displaying/#findComment-1310277 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.