2

I am getting the error (mysql_fetch_row(): supplied argument is not a valid MySQL result resource in) for this query. Why?

<?php

set_time_limit(0);
//MySQL globals
$mysql_server = "***";//change this server for your Drupal installations
$mysql_user = "***";//Ideally, should be root
$mysql_pass = "***";//Corresponding password
$conn = mysql_connect($mysql_server,$mysql_user,$mysql_pass);//MySQL connection string


$query = "select * from dr_wiwe_old_node WHERE type = 'story'";
$noders = mysql_query($query);
var_dump($noders);
while ($row = mysql_fetch_row($noders)) {
$nid = $row[0];
$vid = $row[1];
$type = $row[2];
$title = mysql_real_escape_string($row[3]);
$uid = $row[4];
$status = $row [5];
$created = $row[6];
$changed = $row[7];
$comment = $row[8];
$promote = $row[9];
$moderate = $row[10];
$sticky = $row[11];
//Insertion into node
$query="insert into dr_wiwe_node values('" . $nid . "','" 
    . $vid . "','" . $type . "','','" . $title . "','" . $uid . "','" 
    . $status . "','" . $created . "','" . $changed . "','" . $comment
    . "','" . $promote . "','" . $moderate . "','" . $sticky . "','0','0')";
if (!mysql_query($query)) {
print $query;
}
?>

Where is the error or what can I change?

Borealid
  • 95,191
  • 9
  • 106
  • 122
Lars
  • 39
  • 1
  • 5
  • please be aware that the `mysql_xxx()` functions are deprecated. It is recommended to switch your code to use the PDO library instead. – Spudley Apr 23 '13 at 10:26

4 Answers4

2

When you execute a query, check the return value of the function. If it's false then there was an error with your query.

$noders = mysql_query($query);
if ($noders === false) {
  error_log(mysql_error());
}

Errors can occur for various reasons, including:

  • You are not connected to the MySQL server.
  • You have not selected the right database with mysql_select_db(), so MySQL doesn't know how to find the tables you name in your query. This is the case in your code example.
  • You have an error in your SQL syntax.
  • You don't have privilege to access the tables named in your query.
  • etc.
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Glad to help! Remember to upvote people who helped you on Stack Overflow, and mark the single most helpful answer as the accepted answer. – Bill Karwin Aug 14 '10 at 18:44
1

One of the reason for the

mysql_fetch_row(): supplied argument is not a valid MySQL result resource

is due to:

Wrong host name selected in defining the db or the permission setup for accessing the database is not correct.

For example if you have granted 'admin@localhost' to access a db

and you input the host name explicitly cause issues.

To resolve grant permission something like username@'%' .

Gopinagh.R
  • 4,826
  • 4
  • 44
  • 60
rama
  • 11
  • 1
1

type is a reserved word in SQL. You'll need to enclose it in backticks in your query to use it as a fieldname:

$query = "select * from dr_wiwe_old_node WHERE `type` = 'story'";
Amber
  • 507,862
  • 82
  • 626
  • 550
  • hmmmm...I changed it, but the error is still there..... – Lars Aug 14 '10 at 17:33
  • BTW: IF I do the query in PHPMYAdmin, it works. If i use it in a scipt, it doesn't. – Lars Aug 14 '10 at 17:34
  • Try seeing what the actual SQL error was, then - change your query call to `$noders = mysql_query($query) or trigger_error(mysql_error());` and see what error message it gives you. – Amber Aug 14 '10 at 17:49
0

The problem is that you are recreating a query within your loop of a query. I don't think you can do this without cloning the connection or something.

Mitch Dempsey
  • 38,725
  • 6
  • 68
  • 74