Code:

<?php

define( "DATABASE_SERVER", "blah" );

define( "DATABASE_USERNAME", "blah" );

define( "DATABASE_PASSWORD", "blah" );

define( "DATABASE_NAME", "blah" );

//connect to the database

$mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD) or die(mysql_error());

mysql_select_db( DATABASE_NAME );

$sql = 'SELECT * FROM `users`';
$result = mysql_query($sql) or die ('Error: '.mysql_error ());

while($row = mysql_fetch_array($result))
{
	
$hPassword = hash ( sha256, $row['password']);
$query = "INSERT INTO new_users (username, password) 
VALUES('".$row['username']."','$hPassword')";

$result = mysql_query($query);
}

?>

I keep getting this error:

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.

Recommended Answers

All 3 Replies

try:

<?php

define( "DATABASE_SERVER", "blah" );

define( "DATABASE_USERNAME", "blah" );

define( "DATABASE_PASSWORD", "blah" );

define( "DATABASE_NAME", "blah" );

//connect to the database

$con = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD) or die(mysql_error());

mysql_select_db( DATABASE_NAME );

$sql = 'SELECT * FROM `users`';
$result = mysql_query($sql,$con) or die ('Error: '.mysql_error());

while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{

$hPassword = hash ( sha256, $row['password']);
$query = "INSERT INTO new_users (username, password) 
VALUES('".$row['username']."','$hPassword')";

$result = mysql_query($query);
}

?>

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.

commented: Good Point... +2

wow, i can't believe i missed that. yes humbug is right. when you rename that variable it will work fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.