mysql_query() expects parameter 2 to be resource, boolean given in [duplicate] - php

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 8 years ago.
I looked at the other answers to problems similar to mine but cant seem to solve this.
Here is the code.
$connection = mysql_connect("localhost","root","starwars");
$conn = mysql_select_db("project", $connection);
// This code assumes $itemID is set to that of
// the item that was just rated.
// Get all of the user's rating pairs
$sql = "SELECT DISTINCT r.itemID, r2.ratingValue - r.ratingValue
as rating_difference
FROM rating r, rating r2
WHERE r.userID=$userID AND
r2.itemID=$itemID AND
r2.userID=$userID;";
$db_result = mysql_query($sql, $conn);
echo "The result is {$db_result}";
$num_rows = mysql_num_rows($db_result)or die('Cannot Execute:'. mysql_error());
The error being displayed is:
Warning: mysql_query() expects parameter 2 to be resource, boolean
given in C:\xampp\htdocs\recomender\ratingfiles\class.rating.php on
line 177
Line 177 is
$db_result = mysql_query($sql, $conn);
And if I echo $conn it gives the value of "1" which I thought was equal to true, thus boolean, any ideas?

Pass $connection as the second parameter, not $conn.
You assign the result of mysql_select_db to $conn, and mysql_select_db returns true or false, not a connection resource.

This probably means $conn is false, meaning it didnt get setup correctly. You may want to check how you have set that up and ensure the database connection details are correct and the server this is running on can access the database server.
Take a look at the return values on this page:
http://php.net/manual/en/function.mysql-connect.php

change first 2 lines to
$conn = mysql_connect("localhost","root","starwars");
mysql_select_db("project", $conn);
or, better, just refrain from using connection variable at all
mysql_connect("localhost","root","starwars");
mysql_select_db("project");
...
$db_result = mysql_query($sql);

$db_result = mysql_query($sql, $connection);
Second argument to mysql_connect must have
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.
http://php.net/manual/en/function.mysql-query.php

Related

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /... on line 21 [duplicate]

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();
}

Warning: mysqli_fetch_object() expects parameter 1 to be mysqli_result, boolean given in [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 3 years ago.
Warning: mysqli_fetch_object() expects parameter 1 to be mysqli_result, boolean given in /public_html/header.php on line 35
In the header.php,
Connect to database:
$connect=mysqli_connect("localhost","$db_name","$password")
$sql_logo="SELECT * FROM `logo`";
$query_logo=mysqli_query($connect,$sql_logo);
On line 35:
<?php
while($result=mysqli_fetch_object($query_logo)){
?>
<img src="res/img/<?php echo $result->logo_img?>">
<?php } ?>
in my database:
the table name is logo and the column name is logo_img.
I don't know what happens, because when I try in XAMPP it's work using that's code.
This $connect=mysqli_connect("localhost","$db_name","$password")
2 things here.
The $db_name, that must be the last parameter and you've a missing closing semi-colon.
The syntax is:
host
username
password (if any)
database
As per the manual
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
http://php.net/manual/en/function.mysqli-connect.php
Make sure you also did properly assign those variables.
If there's no password, you still need the parameter for it, but as empty.
I.e.:
$link = mysqli_connect("127.0.0.1", "my_user", "", "my_db");
^ that is for the password parameter
Check for errors also:
http://php.net/manual/en/mysqli.error.php
Your $query_logo is probably FALSE instead of the results you're trying to fetch. This could mean either your mysqli_connectfails, or the $sql_logo query fails.
Print the errors out using echo mysqli_error($connect) and you will find your answer there.
Also, I'd recommend having a look at object oriented mysqli, looks like you're still figuring stuff out, don't get used to procedural or you'll regret it later.
Please refer the PHP-Manual
http://php.net/manual/en/function.mysqli-connect.php
for getting the better idea of mysqli syntax.
And for displaying records try to use syntax and code given below:
$conn = mysqli_connect("127.0.0.1", "db_username", "db_password", "Database_anme");
$limit = 10; //$_POST['limit'];
$start = 0; //$_POST['start'];
$r = mysqli_query($conn, "SELECT * FROM `TABLE_NAME` DESC LIMIT $start,$limit");
while($quotos = mysqli_fetch_object($r)){
// Your code goes here
}

Warning: mysql_fetch_row() expects parameter 1 to be resource [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
I am receiving the below message when I run this script:
Warning: mysql_fetch_row() expects parameter 1 to be resource, string given in /var/www/html/toolkit/routing.php on line 12
I have ran the query in the mysql console and it prints the correct row. Not sure why I cant get it to show up in php?
routing.php page:
<?php
error_reporting(E_ALL);
////error_reporting(0);
ini_set('display_errors', 'On');
include("db/sbc_config.php");
include("db/mysql.class.php");
$db = new MySQL(true, DB_DATABASE_ROUTING, DB_SERVER, DB_USER , DB_PASS);
if ($db->Error()) $db->Kill();
$searchroute = "SELECT * FROM destination_route as d WHERE d.destPrefix='2146811'";
$result = mysql_fetch_row($searchroute);
echo $result;
?>
sbc_config.php:
<?php
//database server
define('DB_SERVER', "10.10.1.146");
//database login name"
define('DB_USER', "user");
//database login password
define('DB_PASS', "pasword");
//database names
define('DB_DATABASE_ROUTING', "routing");
//smart to define your table names also
define('TABLE_DESTINATION_ROUTE', "destination_route");
?>
mysql_fetch_row takes a cursor and returns the next row in that cursor. You're trying to give it a string. You're missing a step.
You'll have to execute that query first:
$cursor = mysql_query($searchroute); // for example
$result = mysql_fetch_row($cursor);
You have to execute the query before you can fetch results:
$searchroute = "SELECT * ...";
$results = mysql_query($searchroute);
$row = mysql_fetch_row($results);
mysql_fetch_row must be called after mysql_query, you can't pass the query into the fetch row
- see PHP Manual
$db = new MySQL(true, DB_DATABASE_ROUTING, DB_SERVER, DB_USER , DB_PASS);
is that supposedto be MySQLi? The mysql_*() functions do not have an OOP interface. You'd be mising mysqli and mysql calls, which is not supported. They're completely independent of each other internally, and a db handle or result statement from one is NOT useable in the other.

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

This question already has answers here:
Closed 10 years ago.
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?
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.
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.
You are passing a query into mysql_error, you need to pass a link identifier.
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!

mysqli_error() expects exactly 1 parameter, 0 given [duplicate]

This question already has answers here:
Warning: mysqli_error() expects exactly 1 parameter, 0 given error
(4 answers)
Closed 2 years ago.
I am trying to get my head around mysql. Can someone tell my why this mysql query is not working? I am getting the following error:
Warning: mysqli_error() expects
exactly 1 parameter, 0 given in
/home/freebet2/public_html/test.php on
line 11
test.php
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/db.php');
$conn = db_connect();
$result = $conn->query("ALTER TABLE users ADD COLUMN refer_old INT(10) AFTER refer_id");
if(!$result){
echo "Error with MySQL Query: ".mysqli_error();
}
?>
db.php
<?php
function db_connect() {
$result = new mysqli('localhost', 'user', 'password', 'db');
if (!$result) {
throw new Exception('Could not connect to database server');
} else {
return $result;
}
}
?>
If I change the alter string to something like : $result = $conn->query("SELECT * FROM users refer_id"); I get no error for some reason.
You are mixing the object-oriented and the procedural styles of the mysqli API :
You are using object-oriented :
$result = new mysqli('localhost', 'user', 'password', 'db');
And, then, procedural :
echo "Error with MySQL Query: ".mysqli_error();
You should use either OO, or procedural -- but not both ; and if you choose procedural, the functions expect the link identifier passed as a parameter.
For instance, mysqli_error should be called either using the object-oriented API :
$link = new mysqli(...);
echo $link->error;
Or the procedural API :
$link = mysqli_connect(...);
echo mysqli_error($link);
(Of course, it will not change the fact that you are having an error in your SQL query, but it'll allow you to get the error message, which should help finding the cause of that error)
As far as the sql error is concerned, does 'user' have permissions to alter the table?
Use mysqli_error($result) as mysqli_error expects the connection to be passed as a parameter.

Categories