0

I need to update table phpbb_points same phpbb_posts

This is my code

<?php
    include 'config.php';
    $link = mysql_connect($dbhost, $dbuser, $dbpasswd) or die('Could not connect');

$db_id = mysql_select_db($dbname) or die('Could not get db');
$d = mysql_fetch_assoc(mysql_query("SELECT * FROM phpbb_users ORDER BY id;"));

mysql_query("UPDATE phpbb_users SET `user_points`='".$d['user_posts']."' WHERE user_id='".$d['user_id']."';") or die(mysql_error());
?>

But it return this

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/**/public_html/forum/point.php on line 7

Please help me, thank in advance

Bala
  • 3,576
  • 10
  • 46
  • 74
David Mcintyre
  • 100
  • 1
  • 12
  • 2
    can't see that function in the code. – yoda Jan 28 '12 at 06:53
  • You have `mysql_fetch_assoc()` in the code but not `mysql_fetch_array()` ? – Leo Haris Jan 28 '12 at 06:56
  • Remove the semicolon `;` from the SQL. BTW mysql_fetch_assoc is a short-cut for mysql_fetch_array. – Joop Eggen Jan 28 '12 at 06:57
  • Where is the mysql_fetch_array statement in your code? Your usage of $d is also incorrect. It is an associative array! `while($d = mysql_fetch_assoc(mysql_query("SELECT * FROM phpbb_users ORDER BY id;")))` perform query – dkulkarni Jan 28 '12 at 06:58
  • 1
    possible duplicate of [php warning mysql_fetch_assoc](http://stackoverflow.com/questions/1901457/php-warning-mysql-fetch-assoc) – fvu Jul 14 '12 at 00:38
  • The mysql extension is outdated and on its way to deprecation. New code should use mysqli or PDO, both of which have important advantages, such as support for prepared statements. Note that `or die(mysql_error())` should never appear in production code, as [`die`](http://www.phpfreaks.com/blog/or-die-must-die) breaks HTML output and database error messages should never be revealed to non-admin users as it [discloses too much information](http://msdn.microsoft.com/en-us/library/ms995351.aspx#securityerrormessages_topic2). A better approach would be to properly implement error handling. – outis Jul 19 '12 at 11:14
  • Don't use [`SELECT *`](http://stackoverflow.com/q/321299/) unless you're writing a DB administration program; select only the columns you need. – outis Jul 19 '12 at 11:15
  • possible duplicate of [mysql_fetch_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – GDP Jul 31 '12 at 14:52

4 Answers4

0

Check $d['user_posts'] in your second query. This is not valid, cause the first query will most likely return more than one row.

Luke
  • 11,426
  • 43
  • 60
  • 69
djot
  • 2,952
  • 4
  • 19
  • 28
0
mysql_fetch_assoc(mysql_query("SELECT * FROM phpbb_users ORDER BY id;"));

You are using (semicolon;) in the sql remove this (semicolon;) from the sql.

Correct one:

$d = mysql_fetch_assoc(mysql_query("SELECT * FROM phpbb_users ORDER BY id"));

Hope this will work.

Luke
  • 11,426
  • 43
  • 60
  • 69
Mohit Singh
  • 142
  • 9
  • Sorry it not work, when i remove "ORDER BY id", MySQL dont return error, but it don't update with my request – David Mcintyre Jan 28 '12 at 07:21
  • Correct update query: mysql_query("UPDATE phpbb_users SET `user_points`='".$d['user_posts']."' WHERE user_id='".$d['user_id']."'") or die(mysql_error()); – Mohit Singh Jan 28 '12 at 07:38
0
$d = mysql_query("SELECT * FROM phpbb_users ORDER BY id;");

while ($row = mysql_fetch_assoc($d) {
    mysql_query("UPDATE phpbb_users SET `user_points`='".$row['user_posts']."' WHERE user_id='".$row['user_id']."';") or die(mysql_error());
}
makriria
  • 383
  • 1
  • 9
-1

REMOVE THE SEMI-COLON IN THAT QUERY

//false code

$d = mysql_fetch_assoc(mysql_query("SELECT * FROM phpbb_users ORDER BY id;"));

//true code

$d = mysql_fetch_assoc(mysql_query("SELECT * FROM phpbb_users ORDER BY id"));
Luke
  • 11,426
  • 43
  • 60
  • 69
Sam Arul Raj T
  • 1,752
  • 17
  • 23