"Warning: MySQL_Query(): Supplied Argument Is Not a Valid MySQL-Link Resource"

mysql_query(): supplied argument is not a valid MySQL-Link resource

Your parameters for mysql_query() are in the wrong order.

This

$result = mysql_query($connection, $query)

should be

$result = mysql_query($query, $connection)

http://php.net/manual/en/function.mysql-query.php

resource mysql_query ( string $query [, resource $link_identifier ] )

Update

When I said use mysql_error(), I meant only if there was an apparent error. Try something like this

if (isset($_POST['accession_number'])) {
$accession_number = $_POST['accession_number'];
$query = sprintf('INSERT INTO `top` (accession_number) VALUES (%d)',
$accession_number);
$result = mysql_query($query);
if (false === $result) {
throw new Exception('Error in query you have, hmmm: ' . mysql_error());
}
// and so on

I highly recommend ditching the MySQL library entirely and moving to PDO. Writing the above code makes me feel dirty.

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource

Based on the comments, it sounds like the problem is caused by using require_once() inside a function.

One of two thing is happening. Either:

  1. You've already included Connection.php somewhere else, so when you get to the function, it's not actually included.. because of the once part of require_once.

    or...

  2. It is working the first time you call the function, but the second time you call it, the file has already been included and does not get included again.

The problem is that when the file is first included (assuming that's from within this function), the $connection variable is created in the function scope, and like any other function variable, is gone at the end of the function. When you call the function a second time, the include doesn't happen because you're using require_once.

You could probably fix this by calling require() instead of require_once(), but that will end up reconnecting to the database every time you call the function - which is a lot of unnecessary overhead. It's much cleaner to just move the include outside of the function, and either pass the connection into the function, or use it as a global variable.

That would look like this:

require_once('Connection.php');

function getResult() {
global $connection;

$findQuery = "SELECT * FROM `Keys` WHERE `ID` = '$gID'";
$findResult = mysql_query($findQuery, $connection) or die(mysql_error());
$resultRow = mysql_fetch_assoc($findResult) or die(mysql_error());
}

All that being said, there's 2 major problems with this code.

  1. You're using the mysql_* functions which are deprecated and will soon be removed from new versions of PHP. See this question for more details: Why shouldn't I use mysql_* functions in PHP?

    It's not actually that hard to switch to something like the mysqli_* functions instead - there's a non-object set of functions that are almost identical to what you're using now.

  2. You're including a variable in your query without properly escaping it. At the very least you should be calling mysql_real_escape_string() (or mysqli_real_escape_string()), but a better solution is to look into prepared statements. You can find more information on prepared statements here: How can I prevent SQL injection in PHP?

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/vinem/www/batch/batch_stock.php on line 47

You open your connection which is fine, yet right after it you're closing the connection link. So that connection no longer exists.

Remove this,

mysql_close($link);

Edit 1

Your code is prone to SQL injection, you are still using MySQL even though it has been deprecated, you should use either MySQLi or PDO with prepared statements.

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource

mysql_select_db() expects the second parameter to be a resource identifier = your connection. The problem is, that you are running this as a function, inside which your connection is not established. You have to start your function with something like this:

function Visit($url)
{
$spyware = mysql_connect(); // set this to connect properly
echo $url;
mysql_select_db($database_spyware, $spyware) || die(mysql_error());
// the rest of your function goes on ...

Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource

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.



Related Topics



Leave a reply



Submit