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.
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 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
}
the problem was that in the config file was mysql so i change it to mysqli
but now i cant see the result in the select in the html area
this is the php script
<?php
require 'config.php';
$query = "SELECT cat_id, category FROM categories LIMIT 1";
$result = mysqli_query($con,$query);
if(!$result){
echo 'Query failed : '.mysqli_error();
exit(0);
}
$row = mysqli_fetch_assoc($result);
// mysql_fetch_assoc was the problem print_r($row);
mysql_close($con);
?>
this is the html script
<select name="cat_id"> </select>
this is thr error
Warning: mysqli_query() expects parameter 1 to be mysqli, resource
given in /home/content/60/10533160/html/cms/in.php on line 5
Warning: mysqli_error() expects exactly 1 parameter, 0 given in
/home/content/60/10533160/html/cms/in.php on line 7 Query failed :
it looks like you used mysql not mysqli for your connection.
maybe you should change that, as it says mysqli_query() expects parameter 1 to be mysqli
Your first error is caused because mysqli_fetch_assoc() needs two parameters, a database name and parameter two to be the array you wish to fetch.
Your code should be (presuming $con is the database connection):
mysqli_fetch_assoc($con,$result);
Your second error is simply caused because mysqli_error() expects 1 parameter: The database connection, but you connected with mysql.*. Don't mix the two libraries. mysqli has many improvements over the original mysql extension, so it is recommended that you use mysqli.
Make sure to do some research on Google before asking a question here.
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
mysqli_close($con);
?>
This example is useful for you...
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
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.