943,667 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1385
  • PHP RSS
Oct 27th, 2008
0

Urgent error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL re

Expand Post »
Code:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. define( "DATABASE_SERVER", "blah" );
  4.  
  5. define( "DATABASE_USERNAME", "blah" );
  6.  
  7. define( "DATABASE_PASSWORD", "blah" );
  8.  
  9. define( "DATABASE_NAME", "blah" );
  10.  
  11. //connect to the database
  12.  
  13. $mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD) or die(mysql_error());
  14.  
  15. mysql_select_db( DATABASE_NAME );
  16.  
  17. $sql = 'SELECT * FROM `users`';
  18. $result = mysql_query($sql) or die ('Error: '.mysql_error ());
  19.  
  20. while($row = mysql_fetch_array($result))
  21. {
  22.  
  23. $hPassword = hash ( sha256, $row['password']);
  24. $query = "INSERT INTO new_users (username, password)
  25. VALUES('".$row['username']."','$hPassword')";
  26.  
  27. $result = mysql_query($query);
  28. }
  29.  
  30. ?>


I keep getting this error:
PHP Syntax (Toggle Plain Text)
  1. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/g/m/a/gmaster1440/html/vote/convertdb.php on line 20

Yes, credentials are correct, yes 'users' table exists.

Please guys, this is due for a big project tomorrow. I'll appreciate any help I can get.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gmaster1440 is offline Offline
3 posts
since Oct 2008
Oct 27th, 2008
0

Re: Urgent error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL re

try:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. define( "DATABASE_SERVER", "blah" );
  4.  
  5. define( "DATABASE_USERNAME", "blah" );
  6.  
  7. define( "DATABASE_PASSWORD", "blah" );
  8.  
  9. define( "DATABASE_NAME", "blah" );
  10.  
  11. //connect to the database
  12.  
  13. $con = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD) or die(mysql_error());
  14.  
  15. mysql_select_db( DATABASE_NAME );
  16.  
  17. $sql = 'SELECT * FROM `users`';
  18. $result = mysql_query($sql,$con) or die ('Error: '.mysql_error());
  19.  
  20. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  21. {
  22.  
  23. $hPassword = hash ( sha256, $row['password']);
  24. $query = "INSERT INTO new_users (username, password)
  25. VALUES('".$row['username']."','$hPassword')";
  26.  
  27. $result = mysql_query($query);
  28. }
  29.  
  30. ?>
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Oct 28th, 2008
1

Re: Urgent error: Warning: mysql_fetch_array()

Why the $result = mysql_query($query); on the fourth last line? You don't need the $result = on that line as the mysql_query will return TRUE on success. You are then treating this as a MySQL result resource in mysql_fetch_array($result) (because the expression in the why loop is executed every time the while loop tries to run).
See http://au.php.net/manual/en/function.mysql-query.php

You should get rid of the $result = assignment (or use a different variable name if it is needed) and it should work.
Reputation Points: 20
Solved Threads: 13
Junior Poster in Training
humbug is offline Offline
93 posts
since Oct 2005
Oct 29th, 2008
0

Re: Urgent error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL re

wow, i can't believe i missed that. yes humbug is right. when you rename that variable it will work fine.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Supressing printing if no value
Next Thread in PHP Forum Timeline: passing info





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC