phantom552 Posted October 30, 2012 Share Posted October 30, 2012 (edited) I can't seem to get this thing to work.... 2 hours I've been tinkering with it to no success. The query itself seems to run, but when I try to echo the results nothing happens aside from the 'hello' & HTML code echos. Anyone see a blatant flaw in my code? the SKUID & DBROW echos are a test to make sure theyre set when I load the page, and they are perfect. I should also mention that dbrow is actually a column of the MySQL database, im not sure why I went with 'row' in the variable. include 'php/opendb.php'; echo $skuid; echo $dbrow; mysql_select_db("brownells_api_data") or die(mysql_error()); $query = "SELECT * FROM Ammunition WHERE $dbrow = $skuid" or die(mysql_error()); $sql = mysql_query($query); $row = mysql_fetch_array($sql); $prodimg = $row['Thumbnail Image']; echo $row['Thumbnail Image']; echo "<div class=toparea>"; echo "<div class=imgarea>"; echo "$prodimg"; echo "</div>"; echo "</div>"; echo $row['Description']; echo 'hello'; Edited October 30, 2012 by phantom552 Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/ Share on other sites More sharing options...
phantom552 Posted October 30, 2012 Author Share Posted October 30, 2012 (edited) Also this is run in a local development environment where I have full server access & control, if that information helps anybody. Edited October 30, 2012 by phantom552 Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388604 Share on other sites More sharing options...
PFMaBiSmAd Posted October 30, 2012 Share Posted October 30, 2012 Putting - or die(mysql_error()); on the end of the $query = "...." string assignment statement doesn't tell you anything, since a string assignment statement doesn't have anything to do with mysql or a mysql_error. You would want to put the or die(....); statement on the end of your mysql_query() statement. Also, you should have php's error_reporting set to E_ALL and display_errors set to ON in your master php.ini on your development system so that php will help you by reporting and displaying all the errors it detects. Stop and start your web server to get any changes made to the master php.ini to take effect and confirm that the two settings got changed by using a phpinfo statement in case the php.ini that php is using is not the one that you changed. Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388607 Share on other sites More sharing options...
sumpygump Posted October 30, 2012 Share Posted October 30, 2012 First, you should set your die() to the next statement. The $query = "..."; statement is just setting a string and will not evaluate to false. Like so: $query = "SELECT * FROM Ammunition WHERE $dbrow = $skuid"; $sql = mysql_query($query) or die(mysql_error()); I am not sure of what the values of $dbrow and $skuid are or what are the field names in the table, so I don't know what might be going wrong with the actual data returned. After $row = mysql_fetch_array($sql); add a line with var_dump($row); to see what the value of $row is. Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388608 Share on other sites More sharing options...
ManiacDan Posted October 30, 2012 Share Posted October 30, 2012 You say nothing happens, so there's two options: 1) both $dbrow and $skuid are empty legitimately and you need to debug your included code. 2) You have errors turned off and most of this code isn't working. When you echo those variables, nothing happens...except maybe warnings about how the variables don't exist. Put this at the top of your script: error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388612 Share on other sites More sharing options...
codydaig Posted October 30, 2012 Share Posted October 30, 2012 You have a mixture of trying to handle multiple rows returned, a single row returned, and only god knows what. So.... I would recommend: mysql_result($query, $x, "ID"); include 'php/opendb.php'; echo $skuid; echo $dbrow; mysql_select_db("brownells_api_data") or die(mysql_error()); $query = "SELECT * FROM Ammunition WHERE $dbrow = $skuid" or die(mysql_error()); $sql = mysql_query($query); // You Do Not Need this row // $row = mysql_fetch_array($sql); $prodimg = mysql_result($query, $x, "Thumbnail Image"); echo mysql_result($query, $x, "Thumbnail Image"); echo "<div class=toparea>"; echo "<div class=imgarea>"; echo "$prodimg"; echo "</div>"; echo "</div>"; echo mysql_result($query, $x, "Description"); echo 'hello'; Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388613 Share on other sites More sharing options...
ManiacDan Posted October 30, 2012 Share Posted October 30, 2012 Don't use mysql_result, it's a slower alternative to mysql_fetch_array Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388616 Share on other sites More sharing options...
phantom552 Posted October 30, 2012 Author Share Posted October 30, 2012 (edited) Adding a var_dump line greets me with: resource(7) of type (mysql result) additionally, the 2 variables are not set in the code shown, they are set earlier and echoed, $dbrow being the title of the column I want to query ("SKU") and $SKUID being a product SKU carried over from the previous page in the URL & retrieved with $_Get To be clear, I am attempting to retrieve data (Product photos, descriptions, & such) from a single row of my 'Ammunition' table in which the value of the SKU colum matches the value stored in $SKUID. I apologise if I sound like an idiot, but I just started using PHP yesterday, so this is a learning experience. Edited October 30, 2012 by phantom552 Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388617 Share on other sites More sharing options...
ManiacDan Posted October 30, 2012 Share Posted October 30, 2012 You said nothing happened aside from the HTML output. Did you forget about the other echo lines? Are they working or not? Echo the query. Run the query by hand in the database. If your SKU has letters in it, you need to quote it in the query. Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388619 Share on other sites More sharing options...
phantom552 Posted October 30, 2012 Author Share Posted October 30, 2012 OK, sorry for the ambiguity. Here is what I currently see on-page: 105201030-105200989-20256 SKU bool(false)hello Which are the variable echos, the var_dump line, and "hello" echo. If I run the SQL query directly in my database as "SELECT * FROM Ammunition WHERE SKU = 105201030-105200989-20256", I get nothing but: MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0003 sec ) Which I find odd since if I enter PHPMyAdmin and open the table in question, the 4th row of the table has "105201030-105200989-20256" in the SKU column. All in all, I assume my query syntax is fubar somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388621 Share on other sites More sharing options...
Pikachu2000 Posted October 30, 2012 Share Posted October 30, 2012 By not enclosing the value in quotes, you're causing a mathematical operation to be performed in the SKU = comparison. Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388624 Share on other sites More sharing options...
sumpygump Posted October 30, 2012 Share Posted October 30, 2012 (edited) The problem is that you need quotes in your SQL query: $query = "SELECT * FROM Ammunition WHERE $dbrow = '$skuid'"; So the resulting SQL must be: SELECT * FROM Ammunition WHERE SKU = '105201030-105200989-20256'; This is because your SKUs contain non-numeric characters (-). Edited October 30, 2012 by sumpygump Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388625 Share on other sites More sharing options...
phantom552 Posted October 30, 2012 Author Share Posted October 30, 2012 Got it! Thanks for the brainstorming session! Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388626 Share on other sites More sharing options...
phantom552 Posted October 30, 2012 Author Share Posted October 30, 2012 Sumpygump, I came to that conclusion right before reading your post lol. I shouldve just ran the query inside PHPmyA to begin with. Oh well, live and learn Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388627 Share on other sites More sharing options...
Pikachu2000 Posted October 30, 2012 Share Posted October 30, 2012 The query would still succeed, so you wouldn't have gotten any error message, just an empty results set. It still wouldn't be obvious why you weren't getting what you expected. The query that was executing was equivalent to SELECT * FROM Ammunition WHERE SKU = -20215. Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388629 Share on other sites More sharing options...
Christian F. Posted October 30, 2012 Share Posted October 30, 2012 Also, don't forget to use mysql_real_escape_string () on the value, when inserting it into the SQL sentence. Otherwise you're open to SQL injection attacks. You should also validate the input on retrieval, and inform the user if it's invalid. To do this you'll want to use a Regular Expression, to ensure that the input conforms to the pattern you're expecting: if (!preg_match ('/^\\d+\\-\\d+\\-\\d+\\z/', $skuid)) { // Validation failed, inform user and show the form anew. } If the three number sequences of the SKU ID has a minimum and/or maximum length, you can set it as well. This is done by replacing the plus sign (following the \\d) with a {#,#} quantifier. Where the # is to replaced by a number, in the min,max pattern. If the latter is not set to a number, it denotes that only the minimum length is required. In other words \\d{8,} expects 8 or more digits, to be valid. You can find more information about Regular Expressions on Regular-Expression.info. Quote Link to comment https://forums.phpfreaks.com/topic/270060-sql-query-where-have-i-gone-wrong/#findComment-1388669 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.