This question already has answers here:
Warning: mysql_query(): 3 is not a valid MySQL-Link resource
(4 answers)
Closed 9 years ago.
ERROR:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /var/www/247QC/system/core.php on line 21
Invalid query: Whole query: SELECT * FROM users WHERE username='matt' AND password='5657572fc913e2d2a9548ba4f4'
From my knowledge I have not done anything wrong in my code but I thought I would ask, since the last time I used MySQL was 2 years ago, things might of changed.
I am wondering what is the error and how do i fix it, I am at the point after googling for the last hour and results saying it was a connection issue and after testing it was connecting to the server correctly (using the hostname not IP address)
The MySQL engine it is using is InnoDB and Collation latin1_swedish_ci
code
$r_hostname = "monitor";
$r_username = "QCSYSTEM";
$r_password = "123456";
$link = mysql_connect($r_hostname,$r_username,$r_password);
$db = mysql_select_db('QCSYSTEM', $link);
$Password = sha1($_POST['password']);
$username = $_POST['username'];
$query = sprintf("SELECT * FROM users WHERE username='%s' AND password='%s'",
mysql_real_escape_string($username),
mysql_real_escape_string($Password));
$result = mysql_query($query,$db);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
} else {
print "success";
}
The second argument to mysql_query should be a link identifier. You are using $db as the second argument, which is nothing but a boolean value. Try this..
$result = mysql_query($query,$link);
or just don't pass any second argument.
Related
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 4 years ago.
I've spent the past 2 hours trying to solve this one error. I am a complete rookie so I dont know what's going on. Here's the code, please help:
<?php
header('Access-Control-Allow-Origin: *');
$host="localhost"; // Host name
$username="id11111_ab"; // Mysql username
$password="*****"; // Mysql password
$db_name="id11111_cd"; // Database name
$tbl_name="ef"; // Table name
// Connect to server and select database.
$link = mysqli_connect($host, $username, $password, $db_name);
// Retrieve data from database
$sql = "SELECT * FROM scores ORDER BY score DESC LIMIT 10";
$result = mysqli_query($link,$sql);
// Start looping rows in mysql database.
while($rows=mysqli_fetch_array($result)){
echo $rows['name'] . "|" . $rows['score'] . "|";
// close while loop
}
?>
mysqli_query() returns false if it fails. Subsequently, the mysqli_fetch_array() function is being passed this false boolean value, on which it can't operate. You'd be wise to check the value that mysqli_query() returns isn't false prior to attempting to retrieve the resource. E.g.:
$result = mysqli_query($link,$sql);
if (!$result) {
die('Query failed');
}
It seems like the mysql connection is not established properly.
Check for errors using this:
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
This question already has an answer here:
"Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource"
(1 answer)
Closed 7 years ago.
I'm trying to use some simple code that's used elsewhere to access content from a MySQL database.
Code...
$rec_sessions_array = array();
$sql = "SELECT member_id FROM " . TABLE_PREFIX . "rec_sessions WHERE course_id = $course_id";
$result = mysql_query($sql, $db);
if($result && mysql_num_rows($result) > 0){
while ($row = mysql_fetch_assoc($result)) {
$rec_sessions_array[] = $row['member_id'];
}
}
I keep getting "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in [path to my script] on line 367". Line 367 is the $result = mysql_query($sql, $db); from above.
When I echo $sql it's correct. When I run that query directly via PHP MyAdmin, it works fine.
Any ideas?
Also, before you say anything about getting away from mysql functions, I know. I'm just trying to augment something that already exists.
Utilizing global $db; higher in the code seems to have fixed this.
Credit goes to "Dagon" in the comments.
This question already has answers here:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]
(6 answers)
Closed 8 years ago.
I am getting a warning message which I don't understand why and unable to resolve, (see below)
Warning: Supplied argument is not a valid MySQL result resource in /detail.php on line 34
Here is my code:
$rs = mysql_query($strSQL);
$strSQL = "SELECT * FROM <tablename> WHERE id=" . $_GET["serviceName"];
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) **(line 34) here ***
{
echo $row['ID']."<br />";
echo $row['serviceName']."<br />";
// Close the database connection
mysql_close();
?>
</dl>
<p>Return to the list</p>
</body>
</html>
thanks in advance, I am not getting any of the data on this webpage either,thanks...singhy
The query is failing - you need to wrap quotes around strings in MySQL:
$strSQL = "SELECT * FROM gu_service_cat WHERE id = '" .
$_GET["serviceName"] . "'";
plus, the $rs should be BELOW the $strSQL...
You need to do it like this
$strSQL = "SELECT * FROM gu_service_cat WHERE id=" . $_GET["serviceName"];
$rs = mysql_query($strSQL);
Because before setting value to the variable you are using it in the query. This is why it is throwing the error.
This question already has answers here:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]
(6 answers)
Closed 9 years ago.
<?php
{
session_start();
include "dbconnect.php";
$target1=$_SESSION['target1'];
echo $target1;
$query = "SELECT * FROM userpictures where pictures = $target1";
$result = mysql_query($query);
var_dump($result);
while($row=mysql_fetch_object($result))
echo $row;
{
$_SESSION['picid1']=$row['picid'];
//$_SESSION['picid1']=$row->picid;
echo $_SESSION['picid1'];
}
}
?>
it is returning me the output as
images/2101.jpgbool(false)
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource
it is giving the target1 but not the picid
please help.
Try this, Added ' - pictures = '$target1'
$query = "SELECT * FROM userpictures where pictures = '$target1' ";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_object($result))
{
$_SESSION['picid1']=$row->picid;
echo $_SESSION['picid1'];
}
Use,
$row->picid;
instead of
$row['picid']
Note: Use mysqli_* fucntions or PDO instead of mysql_* functions(deprecated)
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 9 years ago.
struggling with my web design assignment. I've been following a tutorial to add in a search feature for my website, but I've been getting the following error:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /search.php on line 31
line 31 is (or was)
<pre>if(mysqli_num_rows($results) >= 1)</pre>
That was the original error. as per instructions in the comments, I've since revised the code:
<pre>
<?php
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyword']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter the name/brand of what you're looking for.";
exit();
}
//database connection info
$host = "localhost";
$db_name = "sookehhh_shopsy_db";
$username = "sookehhh_shopsy";
$password = "xxxx";
//connecting to server and creating link to database
$link = mysqli_connect($host, $username, $password, $db_name) or die('Could not connect: ' . mysqli_connect_error());
//MYSQL search statement
$query = "SELECT * FROM sookehhh_shopsy_db WHERE name LIKE '%" . mysqli_real_escape_string($link, $searchTerm) . "%'";
// original query$query = "SELECT * FROM sookehhh_shopsy_db WHERE name LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
//added suggestion below - not sure if correct place?
if (!$result) {
die(mysqli_error($link));
}
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "Product Name: " . $row['name'] . "<br />";
$output .= "Price: " . $row['price'] . "<br />";
}
echo $output;
}
else
echo "There was no matching record for that item " . $searchTerm;
?>
</pre>
made necessary changes and updated yet again -
now the only error message I'm getting here is "Table 'sookehhh_shopsy_db.sookehhh_shopsy_db' doesn't exist"
I'm assuming that I need to change the username, perhaps because it's too similar?
Anywho, thanks for your help so far, and I apologise for my complete ignorance.
I've been trying to teach myself, but unfortunately time is a luxury I just don't have at the moment.
The problem is your query returned false meaning there was an error in your query. After your query you could do the following:
if (!$result) {
die(mysqli_error($link));
}
Or you could combine it with your query:
$results = mysqli_query($link, $query) or die(mysqli_error($link));
That will print out your error.
Also... you need to sanitize your input. You can't just take user input and put that into a query. Try this:
$query = "SELECT * FROM shopsy_db WHERE name LIKE '%" . mysqli_real_escape_string($link, $searchTerm) . "%'";
In reply to: Table 'sookehhh_shopsy_db.sookehhh_shopsy_db' doesn't exist
Are you sure the table name is sookehhh_shopsy_db? maybe it's really like users or something.