slj90 Posted December 4, 2009 Share Posted December 4, 2009 Hey, I am trying to display a users data right after they have created an account.. The code I have that brings up an error is as follows.. <?php // (2)gather details of CustomerID sent $customerId = $_GET['CustomerID'] ; // (3)create query $query = "SELECT * FROM Customer WHERE CustomerID = $customerId"; // (4) Run the query on the customer table through the connection $result = mysql_query ($query); // (5) print message with ID of inserted record if ($row = mysql_fetch_array($result)) { print "The following Customer was added"; print "<br>Customer ID: " . $row["CustomerID"]; print "<br>First Name: " . $row["Firstnames"]; print "<br>Surname: " . $row["Surname"]; print "<br>User Name: " . $row["Username"]; print "<br>Email: " . $row["Email"]; print "<br>Password: " . $row["Password"]; } ?> The error i get is on line 13 - 'mysql_fetch_array(): supplied argument is not a valid MySQL result resource' Can you see anything wrong with my code? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/183995-help-needed-getting-data-from-database/ Share on other sites More sharing options...
mikesta707 Posted December 4, 2009 Share Posted December 4, 2009 I don't see much wrong. changing your query line to $result = mysql_query ($query) or trigger_error(mysql_error()); for debugging purposes (you want to change it back when it goes into a production environment, but for now, we want to see the mysql error) that error usually happens when the query fails. Showing the mysql error can help pinpoint where the problem lies Quote Link to comment https://forums.phpfreaks.com/topic/183995-help-needed-getting-data-from-database/#findComment-971355 Share on other sites More sharing options...
inet411 Posted December 4, 2009 Share Posted December 4, 2009 Try this: change: $query = "SELECT * FROM Customer WHERE CustomerID = $customerId"; to echo "SELECT * FROM Customer WHERE CustomerID = $customerId"; exit; copy the text that is printed to your screen. Something like: SELECT * FROM Customer WHERE CustomerID = 1234 LIMIT 0 , 30 and if you have phpMyAdmin paste the text into the sql statement box and it will tell you exactly what is wrong: MySQL said: Documentation #1146 - Table 'inet411.Customer' doesn't exist Most likely you will have a customer table but maybe your field is customer_id or something... I noticed you have no validation on the $_GET. What if the $_GET['CustomerID'] = 12qw'?"""; or something... you'll get a mysql error then too if you do not mysql_real_escape_string or if you have magic_quotes off (I'm not telling you to turn magic_quotes on). Quote Link to comment https://forums.phpfreaks.com/topic/183995-help-needed-getting-data-from-database/#findComment-971369 Share on other sites More sharing options...
JAY6390 Posted December 4, 2009 Share Posted December 4, 2009 More than likely the customerId isnt being set via the $_GET, or you need to enclose it in single quotes in the query Quote Link to comment https://forums.phpfreaks.com/topic/183995-help-needed-getting-data-from-database/#findComment-971375 Share on other sites More sharing options...
coolabhijits Posted December 4, 2009 Share Posted December 4, 2009 try this query instead of urs..... $query = "SELECT * FROM Customer WHERE CustomerID = ' " .$customerId." ' "; Quote Link to comment https://forums.phpfreaks.com/topic/183995-help-needed-getting-data-from-database/#findComment-971407 Share on other sites More sharing options...
premiso Posted December 4, 2009 Share Posted December 4, 2009 try this query instead of urs..... $query = "SELECT * FROM Customer WHERE CustomerID = ' " .$customerId." ' "; If CustomerID is an INT, that would throw it off completely, even if it was a string, you have an extra space before and after, which will matter when comparing data. But I take it CustomerID is of type INT, so the quotes do not matter. Let's see what the error is when the OP gets back to us and go from there. Quote Link to comment https://forums.phpfreaks.com/topic/183995-help-needed-getting-data-from-database/#findComment-971441 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.