Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/183995-help-needed-getting-data-from-database/
Share on other sites

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

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).

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.