0

Hey,

I am having real trouble with this code, and for the life of me just can't see or figure out why this won't work. It works every other time I have done it.
I have checked, double checked, tripple checked the table it's coming from.
I'm as lost as an ant in the desert.

me code

$user = $_SESSION['user'];
	$productID = $_GET['id'];
	
$isSql = "SELECT * FROM '$user' WHERE product_id ='$productID'";
foreach ($conn->query($isSql) as $row) {
		$isCart = $row['timestamp'];
		$isPrice = $row['in_price'];
}

Please someone tell me what I am doing wrong. By the way, its in PDO so please don't bother replying if you don't use PDO.


Thanks heaps.

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by QWaz
0

You get the error because the query returns an error. Echo a particular query and try it in phpMyAdmin or something similar, see what it outputs, perhaps the quote around your table are messing it up.

Edited by pritaeas: n/a

0

Add the statment above of your for each and check what it returns:

if empty($conn->query($isSql)){
echo "Not work with for each";
}
else echo "It will work";

Also try with the statement instead of above as

if empty($conn->query($isSql) as $row){
echo "Not work with for each";
}
else echo "It will work";

I am not sure above code will work, but the problem is your query doesn't return any arrays if it retuns then your for each loop work.

Edited by rajarajan07: n/a

0

Mr Qwaz, May I know about what you have changed?

I actually marked it as solved after trying my query in phpMyAdmin. and found that the way I put the '' around the table was stuffing it up.

However I still have the same broad issue. as if the product has not been added to the database, it comes up with the same crappy error.

I Like what you put about the

if empty(...)

However I am getting some crazy errors with that. I have never used empty() before and I think I am not using it right.

Do I put it around the other query or after it?
LIKE:

$user = $_SESSION['user'];
	$productID = $_GET['id'];
	
$isSql = "SELECT * FROM '$user' WHERE product_id ='$productID'";
foreach ($conn->query($isSql) as $row) {
		$isCart = $row['timestamp'];
		$isPrice = $row['in_price'];
}
if empty($conn->query($isSql)){
echo "Not work with for each";
}
else echo "It will work";
}

Also kept getting an error saying it was expecting ")" ?? no idea why..

I pretty much want to check if the productID exists in the table. If it doesn't $isCart && $isPrice == 0, else if it does exist, then do that foreach query.

Any ideas?

Thanks for you help

0

Try with one more parenthesis

if (empty($conn->query($isSql))){
echo "Not work with for each";
}
else echo "It will work";
}
0

Try with one more parenthesis

if (empty($conn->query($isSql))){
echo "Not work with for each";
}
else echo "It will work";
}

Hey,

Thanks for your help,

However I finally got it to work using another method. Don't know why I didn't think of it before.

Here is the final working code:

$checkDuplicate = "SELECT COUNT(*) FROM `$user` WHERE product_id = '$productID'";
	$result = $conn->query($checkDuplicate);
	$numRows = $result->fetchColumn();
	// release database resource for next query
	$result->closeCursor();
	if ($numRows) {
		$sqls = "SELECT * FROM `$user` WHERE `product_id` =$productID";
		foreach ($conn->query($sqls) as $rows) {
				$cart = $rows['in_cart'];
				$price = $rows['price'];
		} 
	} else {
		$cart = '0';
		$price = '0';
	}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.