Simple php/mysql script mysteriously not working properly

software development

#1

Hello there,

My first post here. Not sure I can even ask for such help.

I will try.

I have a very simple script I’m working on. It’s php/mysql script for fetching some products info from database and displaying it on one page.

On my laptop, I have installed apache/php/mysql and the script is working just fine, BUT when I trasnfer files to the dreamhost server, the script doesn’t show anything. Debugging hasn’t helped me.

The code in first page (prodavnica2.php) is here:
[php]

<?php include_once('_inc/common.inc.php'); $products = product_list_by_category(); $prod_no = 0; for($i = 0; $i < count($products); $i++) { ?>
<table cellspacing="10">
<tr>
	<th colspan="4"><?php echo $products[$i]["name"];?></th>
</tr>
<tr>
	<th>Naziv</th>
	<th>Cena</th>
	<th>Kol</th>
	<th>Ukupno</th>
</tr>
<?php

$products_per_category = $products[$i]["products"];
for($j = 0; $j < count($products_per_category); $j++)
{
	?>
		<tr>
			<td><?php echo $products_per_category[$j]["name"];?></td>
			<td width="100" align="right"><?php echo number_format($products_per_category[$j]["price"], 2);?>din/<?php echo $products_per_category[$j]["per"];?></td>
			<td width="100">
			<input type="hidden" name="product[<?php echo $prod_no;?>][product_id]" value="<?php echo $products_per_category[$j]["product_id"];?>">
			<input type="hidden" name="product[<?php echo $prod_no;?>][price]" id="price_<?php echo $prod_no;?>" value="<?php echo $products_per_category[$j]["price"];?>" />
			<select name="product[<?php echo $prod_no;?>][qty]" id="qty_<?php echo $prod_no?>" onchange="calculate_subtotal(this, <?php echo $products_per_category[$j]["price"];?>, 'subtotal_<?php echo $products_per_category[$j]["product_id"];?>');calculate_grand_total();">
				<option value=""> - izaberi - </option>
			<?php
			if($products_per_category[$j]["type_of_qty"] == TYPE_OF_QTY_KG)
			{
			?>

				<option value="0.5">0.5kg</option>
				<option value="1">1kg</option>
				<option value="1.5">1.5kg</option>
				<option value="2">2kg</option>
				<option value="2.5">2.5kg</option>
				<option value="3">3kg</option>
			<?php
			}
			elseif($products_per_category[$j]["type_of_qty"] == TYPE_OF_QTY_KOM)
			{
			?>
				<option value="1">1</option>
				<option value="2">2</option>
				<option value="3">3</option>
				<option value="4">4</option>
				<option value="5">5</option>
			<?php
			}
			?>
			</select>
			</td>
			<td width="100" align="right" id="subtotal_<?php echo $products_per_category[$j]["product_id"];?>"></td>
		</tr>
	<?php
	$prod_no++;
}
?>
</table>
<?php

}

?>
[/php]

The common.inc.php has some common stuff in there along with functions.

[php]

<?php session_start(); error_reporting(1); include('Db.php'); /*********************** CONSTS ************************/ define("TYPE_OF_QTY_KG", 1); define("TYPE_OF_QTY_KG_LABEL", "per kg"); define("TYPE_OF_QTY_KOM", 2); define("TYPE_OF_QTY_KOM_LABEL", "1/2/3/4"); /*********************** FUNCTIONS ************************/ function category_list() { $db = new Db(); $sql = "SELECT * FROM `category` ORDER BY `position` ASC"; // echo $sql; $rows = $db -> select($sql); if(!$rows) return $db -> error(); return $rows; } function product_list_by_category() { $category = category_list(); $db = new Db(); for($i = 0; $i < count($category); $i++) { $sql = "SELECT * FROM product WHERE category_id = ".$db->quote($category[$i]["category_id"]); $rows = $db -> select($sql); $category[$i]["products"] = $rows; } return $category; } ?>

[/php]

And finally, the Db.php

[php]

<?php /** * Database wrapper for a MySQL with PHP tutorial * * @copyright Eran Galperin * @license MIT License * @see http://www.binpress.com/tutorial/using-php-with-mysql-the-right-way/17 */ class Db { // The database connection protected static $connection; /** * Connect to the database * * @return bool false on failure / mysqli MySQLi object instance on success */ public function connect() { // Try and connect to the database if(!isset(self::$connection)) { // Load configuration as an array. Use the actual location of your configuration file // Put the configuration file outside of the document root $config = parse_ini_file('config.ini'); self::$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']); } // If connection was not successful, handle the error if(self::$connection === false) { die($this->connect_error); // Handle error - notify administrator, log to a file, show an error screen, etc. return false; } return self::$connection; } /** * Query the database * * @param $query The query string * @return mixed The result of the mysqli::query() function */ public function query($query) { // Connect to the database $connection = $this -> connect(); // Query the database $result = $connection -> query($query); return $result; } /** * Fetch rows from the database (SELECT query) * * @param $query The query string * @return bool False on failure / array Database rows on success */ public function select($query) { $rows = array(); $result = $this -> query($query); if(!$result) { return false; } while ($row = $result -> fetch_assoc()) { $rows[] = $row; } return $rows; } /** * Fetch the last error from the database * * @return string Database error message */ public function error() { $connection = $this -> connect(); return $connection -> error; } /** * Quote and escape value for use in a database query * * @param string $value The value to be quoted and escaped * @return string The quoted and escaped string */ public function quote($value) { $connection = $this -> connect(); return "'" . $connection -> real_escape_string($value) . "'"; } } [/php] As far as I can see, the connection to the database is established, but if I var_dump results of a first query, I get NULL. As I said, on local server, it works just fine. The script is currently here http://www.plavibrod.com/prodavnica2.php Thank you for your help. Regards, phoenix_ns

#2

[php]
self::$connection = new mysqli(‘localhost’,$config[‘username’],$config[‘password’],$config[‘dbname’]);
[/php]

This won’t work on DreamHost. You need to use a MySQL hostname you’ve created for your account, not “localhost”.


#3

Sorry, yes, I know. I copied code from my local server. On dreamhost one, I put hostname as supposed to. That’s not it.


#5