dubz99 Posted August 19, 2008 Share Posted August 19, 2008 As the topic states, I am obviously a beginner and my question should be an easy one. I am attempting to have and if/else statement within and if else statement. Is this possible if so how is it done, below is the code I am working with- it makes sense to me but I am getting errors. Probably something basic that I am overlooking. TIA dubz $stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']); $stock_id_result = mysql_query($stock_id_query) or die("<br />". mysql_error()); $p_stock_id_query = "SELECT stock_id FROM " . TABLE_PRODUCTS . "WHERE " . stock_id . " = " . tep_get_prid($p['id']); $p_stock_id_result = mysql_query($p_stock_id_query) or die("<br />". mysql_error()); if ($stock_id_query = " "){ //Check to see if blank if( mysql_num_rows($p_stock_id_result) > 0 ) { $row = mysql_fetch_assoc($p_stock_id_result); $stock_id_display = $row['stock_id']; return; } else{ print (mysql_error()); } } else { if( mysql_num_rows($stock_id_result) > 0 ) { $row = mysql_fetch_assoc($stock_id_result); $stock_id_display = $row['products_code']; } else { print (mysql_error()); } } Link to comment https://forums.phpfreaks.com/topic/120344-php-101-if-else-within-if-else/ Share on other sites More sharing options...
kenrbnsn Posted August 19, 2008 Share Posted August 19, 2008 What errors are you getting? Ken Link to comment https://forums.phpfreaks.com/topic/120344-php-101-if-else-within-if-else/#findComment-620025 Share on other sites More sharing options...
dubz99 Posted August 19, 2008 Author Share Posted August 19, 2008 Thanks for the reply, Error as follows: Problem (default): undefined <br /> You have an error in your SQL syntax. Check the manual . . to use near '=1991' at line 1 dubz Link to comment https://forums.phpfreaks.com/topic/120344-php-101-if-else-within-if-else/#findComment-620027 Share on other sites More sharing options...
kenrbnsn Posted August 19, 2008 Share Posted August 19, 2008 When you're trying to debug mysql errors, it's always helpful to print out the query and the line number where it occured. For example (taken from your code): <?php $stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']); $stock_id_result = mysql_query($stock_id_query) or die("Problem with the query <span style='color:red'>$stock_id_query</span> on line: " . __LINE__ . "<br />". mysql_error()); ?> Also, your indentations are messed up so it's hard to see which code chunk belongs to which "if" or "while" block. This is also difficult, since you posted only a piece of a larger function. Ken Link to comment https://forums.phpfreaks.com/topic/120344-php-101-if-else-within-if-else/#findComment-620045 Share on other sites More sharing options...
dubz99 Posted August 19, 2008 Author Share Posted August 19, 2008 Ken, Understandable. Like I said beginner. Well I got it to work without the error (missing a space between 'WHERE' and ")but the value is blank? maybe if I explain you could sum it up for me. Hopefully I can explain it well. IF $A is blank Then Do this (second if statement) Then skip next lines IF $A is not blank Then Do this Also is regards to the error the '=1991' is referring to the variable being pulled in this line. $stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']); or $p_stock_id_query = "SELECT stock_id FROM " . TABLE_PRODUCTS . " WHERE " . stock_id . " = " . tep_get_prid($p['id']); as they are the same number. Would elseif work better here? Thanks dubz Link to comment https://forums.phpfreaks.com/topic/120344-php-101-if-else-within-if-else/#findComment-620051 Share on other sites More sharing options...
revraz Posted August 19, 2008 Share Posted August 19, 2008 Are TABLE_PRODUCTS_STOCK and products_id your actual field names? If so, why are they concantenated and out of the string? Link to comment https://forums.phpfreaks.com/topic/120344-php-101-if-else-within-if-else/#findComment-620070 Share on other sites More sharing options...
dubz99 Posted August 19, 2008 Author Share Posted August 19, 2008 Originally I had the following code which worked, but I need to look in another location if the first place it looks is blank. To answer your question: TABLE_PRODUCTS_STOCK is a table name defined elsewhere and products_id a field within the TABLE. $stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']); $stock_id_result = mysql_query($stock_id_query) or die("<br />". mysql_error()); if( mysql_num_rows($stock_id_result) > 0 ) { $row = mysql_fetch_assoc($stock_id_result); $stock_id_display = $row['products_code']; } else { print (mysql_error()); } dubz Link to comment https://forums.phpfreaks.com/topic/120344-php-101-if-else-within-if-else/#findComment-620130 Share on other sites More sharing options...
kenrbnsn Posted August 19, 2008 Share Posted August 19, 2008 After you do <?php $stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']); ?> echo the query <?php $stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']); echo '<pre>' . $stock_id_query . '</pre>'; ?> and see what's in the query. Unless TABLE_PRODUCTS_STOCK and products_id are constants, there will be nothing in the query for those strings. Perhaps you want: <?php $stock_id_query = "SELECT products_code FROM " . $TABLE_PRODUCTS_STOCK . " WHERE " . $products_id . " = " . tep_get_prid($p['id']); ?> Ken Link to comment https://forums.phpfreaks.com/topic/120344-php-101-if-else-within-if-else/#findComment-620176 Share on other sites More sharing options...
dubz99 Posted August 19, 2008 Author Share Posted August 19, 2008 Ken, OK simple mistake on my end, on Line 3 instead of stock_id it should have said products_id! But now it seems to get stuck in a loop and nothing happening. Here is the code, please feel free to chop it up, as I and many others can learn from your knowledge. Thanks again dubz $stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']); $stock_id_result = mysql_query($stock_id_query) or die("<br />". mysql_error()); $p_stock_id_query = "SELECT stock_id FROM " . TABLE_PRODUCTS . " WHERE " . products_id . " = " . tep_get_prid($p['id']); $p_stock_id_result = mysql_query($p_stock_id_query) or die("<br />". mysql_error()); if ($stock_id_query = " "){ //Check to see if blank if( mysql_num_rows($p_stock_id_result) > 0 ) { $row = mysql_fetch_assoc($p_stock_id_result); $stock_id_display = $row['stock_id']; return; } else{ print (mysql_error()); } } else { if( mysql_num_rows($stock_id_result) > 0 ) { $row = mysql_fetch_assoc($stock_id_result); $stock_id_display = $row['products_code']; } else { print (mysql_error()); } } Link to comment https://forums.phpfreaks.com/topic/120344-php-101-if-else-within-if-else/#findComment-620368 Share on other sites More sharing options...
revraz Posted August 19, 2008 Share Posted August 19, 2008 From the code you are showing us, there is nothing more for it to do. What do you mean it get's stuck in a loop? Link to comment https://forums.phpfreaks.com/topic/120344-php-101-if-else-within-if-else/#findComment-620518 Share on other sites More sharing options...
dubz99 Posted August 20, 2008 Author Share Posted August 20, 2008 Hey Thanks for your help! I was able to stop the loop by removing the return; and rather than having a complex if else statement I just made two individual ones. Also I had to clear out some variables after it ran. Below is the finished working code! //Clear variables $stock_id_query = ""; $p_stock_id_query = ""; // Define id for item with attributes $stock_id_query = "SELECT products_code FROM " . TABLE_PRODUCTS_STOCK . " WHERE " . products_id . " = " . tep_get_prid($p['id']); $stock_id_result = mysql_query($stock_id_query) or die("Problem with the query <span style='color:red'>$stock_id_query</span> on line: " . __LINE__ . "<br />". mysql_error()); //Define if for item without attributes $p_stock_id_query = "SELECT stock_id FROM " . TABLE_PRODUCTS . " WHERE " . products_id . " = " . tep_get_prid($p['id']); $p_stock_id_result = mysql_query($p_stock_id_query) or die("Problem with the query <span style='color:red'>$p_stock_id_query</span> on line: " . __LINE__ . "<br />". mysql_error()); if( mysql_num_rows($p_stock_id_result) > 0 ) { $row = mysql_fetch_assoc($p_stock_id_result); $stock_id_display_a = $row['stock_id']; //return; } else { print (mysql_error()); } if( mysql_num_rows($stock_id_result) > 0 ) { $row = mysql_fetch_assoc($stock_id_result); $stock_id_display_attr_a = $row['products_code']; } else { print (mysql_error()); } $stock_id_display = $stock_id_display_a . $stock_id_display_attr_a; $stock_id_display_a = ""; $stock_id_display_attr_a = ""; Thanks again! dubz Link to comment https://forums.phpfreaks.com/topic/120344-php-101-if-else-within-if-else/#findComment-621137 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.