Jump to content

php mysql output not displaying


moesoap

Recommended Posts

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  :shrug:

 

<?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();

    ?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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' );

Link to comment
Share on other sites

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 . . .

Link to comment
Share on other sites

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 :shrug:

 

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>

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.