MySQL- Unable to jump to row 0 on MySQL result index - php

I have got an old site that has recently been displaying an error which is weird as its been untouched for some time. I get the following:
Unable to jump to row 0 on MySQL result index 8
What is the cause of this and how should I fix it?
It is a PHP/MySQL site.

If I remember correctly, this error typically stems from a code segment like the following:
// You probably have some code similar to this
$var = mysql_result( $result, 0, 'column_name');
Where either the query fails or the column doesn't exist. Check that $result is a valid MySQL resource to make sure that the SQL is valid, then make sure you're actually getting results from the database before trying to call mysql_result.
Or, better yet, using mysql_fetch_array instead of manually fetching every column value (if you have multiple columns returned from the query).

Try analysing the result before fetching it.
If result is empty, skip fetching.
$result = mysql_query("SELECT * FROM table1");
if (!$result || !mysql_num_rows($result)) {
die('Empty set.');
}
while ($row = mysql_fetch_array($result)) {
// Your code here
}

Related

I am getting a error in the log, but the scripts do what I want

I am trying to do the following.
SELECT from database and echo, If there are less than 3 results, I want to subtract the amount of results from 3 and use an alternative SELECT string, The code works and does what I expect of it except for the error in the logs.
PHP Warning: mysql_num_rows() expects parameter 1 to be resource,
null given in
This error seems to be because the result is empty, How can I check how many rows in a result without getting an error when the table has no results.
$sqls="SELECT * FROM xxx ORDER by xxx_id ASC LIMIT 0,3";
$objRs = mysql_query($sqls);
The database works, the SQL query works, the row below this is the one giving me the error when I have a NULL result. Is there another way to check the number of rows without getting the server pissed at me
$count = mysql_num_rows($objRS);
If (!empty($count)) {
$pnb = 3;
} else {
while($rows = mysql_fetch_array($objRs, MYSQL_ASSOC)) {
The code "works" by coincidence alone. You have a typo:
$objRs
is not the same thing as
$objRS
Variable names are case-sensitive. But the comparison you have set up doesn't actually check any results. This:
!empty($count)
is just checking if $count has any value. Which it does. Whether that value is a positive number or not, your code doesn't care. But it probably should...

check if PDO query returns results

Assume I have this piece of code:
foreach($creds as $cred){
$prev_fut = $pdo->query("SELECT importo FROM incassi_prev WHERE
data_prev='$data_fut' AND incasso=0 AND
id_cre='{$cred['id_cre']}'")->fetch();
if(count($prev_fut)>0){
//I have found a result
}else{
//I have found nothing
}
}
NOTE: It is an internal query for my application with no data posted by user so I don't worry about SQL injections.
I use to check if count($prev_fut)>0 to see if the query is returning data (if I find any row in the db with these criterias).
My question is:
is this check enough to verify that the query has at least a result? Is it better to perform any other check?
The question is mostly coming from my thoughts about this being in a for loop and is related to the option of emptying/unset the $prev_fut array before starting a new iteration of for loop.
fetchColumn returns a single value of the next row in the result set. count counts the length of an array. Since fetchColumn can't return an array (for most database implementations), using count on it is wrong. You want to test whether $prev_fut is false or not. false would indicate that no result could be fetched, while anything else means a result was fetched:
if ($prev_fut !== false) ..
Having said that, you should really use a COUNT() in the database and check that value:
$result = $pdo->query('SELECT COUNT(*) FROM ...')->fetchColumn();
if ($result > 0) ..
It's much more efficient to have the database count and summarise the result than fetching an entire result set into PHP just for this simple check.

how to print mysql result RESOURCE_ID(7) without mysql_fetch_array

Is there any way to print the resource_id without mysql_fetch_array.I dont want to loop through result.I want print only the first row at top.I know mysql has been depreciated.This is for old project.
You can make use of arrays in your case
$all_rows = array();
.
. // your query
.
while($dbrow = mysql_fetch_array($query))
{
$all_rows[] = $dbrow;
}
$first_row_array = $all_rows[0]; // first row will be stored here
/*
uncomment the below line if you do not want to use the
first row again while looping through the remaining
rows
*/
/* unset($all_rows[0]); */
foreach($first_row_array as $first_row)
{
// do something with first row data
}
foreach($all_rows as $dbrow)
{
// loop through all the rows returned including the first row
}
A resource in itself is a pretty meaningless type. It only means something to specific functions, like mysql_*. When querying the database, there are certain resources allocated on the MySQL server which hold your requested result; PHP doesn't really have access to those results yet. To give you a handle on those resources on the MySQL server, you get a resource type variable. It's basically just your ticket, saying "if you ever want to access that data waiting for you on the MySQL server, use this number."
So, if you want to output the data from the MySQL server, you will have to fetch it from there, e.g. with mysql_fetch_assoc. That then returns the data to you which you can print.
If you just want the first result, just call that function once.

How to determine whether a particular resource has data

I am trying to figure out the proper way to get file location data (for display/editing) from MySQL with PHP. So far I've got these three parts. $resfile is a resource getting the actual array. Would I then test with an if statement, or would I have to use a while loop to iterate over the array (which, as far as I know, should only have ONE value)
First part:
$resfile = mysql_query('SELECT file_loc WHERE org_id = '.$org);
Do I use this?
if (!$resfile) {
}
Or this?
while ($filerow = mysql_fetch_array($resfile)) {
}
Or both?
The mySQL library has a function for counting the rows of a result set:
if (mysql_num_rows($resfile) > 0) .......
You need to use both. If the query returns false, then there was an error executing your query. If there is no data returned in the query, (it will still return true) then you need to use fetch_array to get the data.

Does MySQL SELECT * with a LIMIT 1 return FALSE on no results?

I've been working on a database wrapper class and every now and then the server goes away... Typical 2006 error message for MySQL. I've wrapped logic around handling reconnecting to the database and that seems to be working. What's interesting to me is that this query:
SELECT id FROM pixel WHERE (id = 522574) AND (advertiser_entity_id = 45574) LIMIT 1
Executing that line in PHPMYADMIN yields an empty set. Executing that via the database class returns false.
Has anyone seen this behavior? No mysql_errorno or error messages are coming back.
$result = mysql_query($query, $this->database_connection);
if (false === $result) {
// handle error here
}
else { return $result; }
It isn't an error to not find a matching row. If it doesn't find any records that meet your WHERE conditions, that's not an error, it just returns an empty result. If you're writing your own DB layer, it also shouldn't treat it as an error - the query was run just fine, it just didn't find any matches.
If you're asking, though, why your code returns false, it's probably just because usually languages treat a zero or null value as "false" in a boolean context.

Categories