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.

Dani AI

Generated

As noted, the visible error comes from overwriting the SELECT result resource with the INSERT result. mysql_query() returns a result resource for SELECT but returns TRUE/FALSE for INSERT/UPDATE — assigning that boolean back into the same $result means the next call to mysql_fetch_array($result) receives a boolean, not a resource, and emits the warning. (php.net)

Practical immediate fixes (safe for a quick conversion):

  • Stop assigning the INSERT call to the same variable used for the SELECT result (either remove the assignment or use a different variable name such as $insertOk). This is the root fix that pointed out and hinted at by showing alternate usage of mysql_query.
  • Always check query return values and surface mysql_error() when FALSE, and free the SELECT result with mysql_free_result() when done.
  • If the script will run against a copy of production data, run the migration inside a transaction (if using InnoDB) so partial failures can be rolled back.

For a modern, safer approach, migrate the conversion to PDO (or mysqli) and use prepared statements plus PHP’s password helpers. Example (minimal PDO flow; adapt DSN and options to the environment):

<?php
$pdo = new PDO('mysql:host=HOST;dbname=DB;charset=utf8mb4','USER','PASS', [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  PDO::ATTR_EMULATE_PREPARES => false,
]);

$select = $pdo->query('SELECT username, password FROM users');
$insert = $pdo->prepare('INSERT INTO new_users (username, password) VALUES (:u, :p)');

while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
  // If current column holds plaintext passwords:
  $newHash = password_hash($row['password'], PASSWORD_DEFAULT);
  $insert->execute([':u' => $row['username'], ':p' => $newHash]);
}

Use password_hash() rather than a raw SHA-256 for passwords (it embeds salt and is adaptive); follow established password-storage guidance (Argon2/bcrypt where available) and plan how to handle existing hashed passwords during migration. (php.net)

Longer-term: the old mysql_* extension is deprecated/removed — prefer PDO or MySQLi for new code and migrations. (php.net)

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.