0

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

        $err = mysql_query("INSERT INTO tridy (id,NazevTridy,url) VALUES (
            '$i',
            '$tridy->find('div[class=rozvrhseznam]', 0)->find('a[href]', $i)->outertext',
            '$tridy->find('div[class=rozvrhseznam]', 0)->find('a[href]', $i)->href')");
        mysql_error($err); // line 97

Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in /hosting/www/cran-web.com/www/rozvrh/engine.php on line 97

--- lines 2-6:

$username="*****.com";
$password="*********";
$database="*********";
mysql_connect('127.0.0.1', $username, $password) or die('Could not connect'.mysql_error());
mysql_select_db($database) or die( "Cannot select db.");

I'm getting this error when I try to execute my query. Can you tell what does the error message mean and how to fix it?

Community
  • 1
  • 1
  • You should really save the mysql connection resource, returned by `mysql_connect` somewhere. – poke Feb 06 '10 at 17:58
  • 1
    I know it might be obvious what you want us to do, but you could at least formulate a proper question. – Felix Kling Feb 06 '10 at 17:59

4 Answers4

1

mysql_error($err); remove the argument! It takes link to the resource not number of error.

Link is used to recognise different connections (you can retrieve one using mysql_connect) read about this if u need more.

oneat
  • 10,778
  • 16
  • 52
  • 70
0

You are passing a query into mysql_error, you need to pass a link identifier.

Charlie
  • 11
  • 2
0

mysql_error() expects a "Link resource" and no "result resource". Te correct way would be something like:

$username="*****.com";
$password="*********";
$database="*********";
$connection = mysql_connect('127.0.0.1', $username, $password) or die('Could not connect'.mysql_error());
mysql_select_db($database, $connection) or die( "Cannot select db.");


$err = mysql_query("INSERT INTO tridy (id,NazevTridy,url) VALUES (
        '$i',
        '$tridy->find('div[class=rozvrhseznam]', 0)->find('a[href]', $i)->outertext',
        '$tridy->find('div[class=rozvrhseznam]', 0)->find('a[href]', $i)->href')", $connection);
mysql_error($connection); // line 97

Mind the use of $connection. Wile $connection could be dropped everywhere as in

mysql_error();

Which uses the last opened connection or opens a new one by default. While depending on the default connection is bad. You might also want to look into mysqli or PDO as alternative ways to talk to MySQL.

johannes
  • 15,807
  • 3
  • 44
  • 57
0

Also mind that mysql_query() dealing with INSERT returns true on success and false on failure.

So naming the variable $err is somehow misleading, if($err) would mean no error occurred and vice versa.

Better:

$success = mysq_query("INSERT....");

if(!$success) {
    // use of $connection is pointed to in other answers
    $error_msg = mysql_error($connection); 

    // so some error handling
}

About mysql_error():

Parameter: The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed

and the return value:

Returns the error text from the last MySQL function, or '' (empty string) if no error occurred.

So you also do something with the return value. Just calling mysql_error() is of no use!

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143