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:
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 ofrequire_once
.or...
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.
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.You're including a variable in your query without properly escaping it. At the very least you should be calling
mysql_real_escape_string()
(ormysqli_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
What Do \R and \N Mean in PHP (And Other Languages)
Symfony: How to Refresh the Authenticated User from the Database
Get/Set Dpi with PHP Gd/Imagick
How to Verify the Requesting Server in PHP
MySQL Stored Function to Create a Slug
How to Update Timezonedb in PHP (Updating Timezones Info)
Setting Variables on Constructor VS on the Class Definition
Interpreting Return Value of Function Directly as an Array
Mysql: Determine Table's Primary Key Dynamically
Pdo Last Insert Id Always the Right One
Strtolower() for Unicode/Multibyte Strings
How to Set the Value of a Textbox Through PHP
Setting a Cookie in an Ajax Request
How to Iterate by Row Through a MySQL Query in PHP
PHP - Fastest Way to Check Presence of Text in Many Domains (Above 1000)
Displaying Blob Image from MySQL Database into Dynamic Div in HTML