Hi,

I'm not very good at PHP so sorry if this is stupid but I have a database with a table called item. I want to retrieve certain items based on specific criteria so I have written this function:

function get_item ($item_no, $supp_id, $cat) {
	   	global $connection;
	   	if ($item_no && !$supp_id && !$cat){
	   		$query = "SELECT *
	   			      FROM item
	   			      WHERE item_id = $item_no";
	   	}
	   	else if (!$item_no && $supp_id && $cat) {
	   		$query = "SELECT *
	   			      FROM item
	   			      WHERE supp_id = $supp_id and cat_id = $cat";
	   	}
	   	else if (!$item_no && !$supp_id && $cat) {
	   		$query = "SELECT *
	   			      FROM item
	   			      WHERE category = $cat";
	   	}
	   	else if (!$item_no && $supp_id && !$cat) {
	   		$query = "SELECT *
	   			      FROM item
	   			      WHERE supplier = $supp_id";
	   	}
	   	$item_set =  mysql_query($query, $connection);
	   	confirm_query($item_set);
	   	return $item_set;
  }

I have a php page with this:

$item_no = NULL;
  $supp_id = 4;
  $cat = 3;
  $item_set = get_item($item_no, $supp_id, $cat);
  while ($row = mysql_fetch_array($item_set)) {
  echo "<h1>".$row['item_name']."</h1><br /> ".$row['description']." <br /> £".$row['price']."<br /><img src='".$row['image']."'></p>";}

All I get is a blank page. No errors but no data either.

Can anyone help, please?

Thanks in advance.

Recommended Answers

All 5 Replies

Hi Roybut,

First of all let me say there are no stupid questions, only those too stupid to ask. :)

Ok, now I have thought of a couple of things that could be happening here. First of all, are the two code snippets in separate php files? If so, have you included the first file in the second by a call to include or require? Also, is the get_item function a function inside a class? If so, it needs to be called by classname::get_item(...); One way to check that your query is being created correctly is to put an echo statement immediately before the call to mysql_query, like so:

echo $query;
item_set = ... ;

This will tell you if your query is correct or not, just don't forget to remove the line later after you have finished debugging.

Anyway, this might be a start to helping you work out what is going on. Let us know if this hasn't fixed it.

Cheers,
d

Also, double check the spelling of your column names in the item table. The calls to $row etc must match the column names.

Hi D,

Thanks for the help. I did have a couple of field names wrong (late night working:zzz: ) and have changed them. I still get nothing displayed. I tried echoing $item_set and it simply prints "Resource id #6" on the page.

I do have the function in a separate file but i have included the functions file and called it. Other functions work so it is definitely not that.

I don't know how to create a class so I'm sure it is not that, either.

I have checked the function again to make sure nothing seems out of place and it looks fine.

Thanks,

Roy

UPDATE:

I have indeed been stupid! I found that I was searching for things that did not exist. I was looking in category and supplier but not all suppliers have items in all categories. I think it was just bad luck that I chose to test the function with mis-matching values. Little sleep and little PHP knowledge makes for big headache!

Thanks for your input, D.

Ah the classic bane of many a programmer, lack of sleep. I find that a good cure for this is caffeine and lots of it :P Anyway, glad you found your problem. Happy programming!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.