Warning: mysql_result() [function.mysql-result]: Unable to jump to row -1 - php

This is my page code:
<?php
$dbhost="****";
$dbname = '****';
$dbusername="********";
$dbpass="*****;
$con = mysql_connect("$dbhost","$dbusername","$dbpass");
mysql_select_db($dbname,$con);
?>
<main>
<table width='100%' align='center'>
<tr align='center'>
<?php
$sql = "SELECT * FROM file ORDER BY data";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
$j = $num - 1;
$fyear = mysql_result($result, $j, "data");
$year = 2005;
while ($year <= $fyear)
{
$sql = "SELECT * FROM file WHERE posizione = 'uila/blindi.php' AND YEAR(data) = ".$year." ORDER BY data";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$num = mysql_num_rows($result);
$i = 0;
echo "<td>";
while ($i < $num)
{
$file = mysql_result($result, $i, "pdf");
$name = mysql_result($result, $i, "nome");
$type = mysql_result($result, $i, "type");
$path = mysql_result($result, $i, "posizione");
echo ""?><?php echo $name; ?><br>
<?php
$i++;
}
echo "</td>";
$year++;
}
?>
</tr>
</table>
And this is the page's error:
Warning: mysql_result() [function.mysql-result]: Unable to jump to row -1 on MySQL result index 2 in D:\Inetpub\webs\uilanotiziecom\bindi.php on line 47
The 47th line is : $j = $num - 1;
How can I fix it?

have you tried this? :
if ( $num >= 1 ) {
$j = $num - 1;
}
if your query returns 0 rows you get the error, what you do in that case is up to you

I think the below sql query is returning empty result that is where you are facing the issue.
$sql = "SELECT * FROM file ORDER BY data";
That's why $j value is -1
And FYI,
Please use proper naming conventions and unique variable names as you have used $sql for both the queries

Related

How can I make this code short and easy to read?

I have more than 10 lines like this and I want to echo just 5 rows per page this is my code.
$c = mysqli_fetch_field_direct;
$c1 = $c($result,2)->name;
$c2 = $c($result,3)->name;
$c3 = $c($result,4)->name;
//$cn,...;
echo "$row[2]" ; echo "$c1[2]";
echo "$row[3]" ; echo "$c1[3]";
//echo "$row[n]" , ...;
Before this code I have this:
$sql = "SELECT * FROM sheet1 WHERE id = '$_POST[text]'";
$result = mysqli_query($con,$sql);
If ($row = mysqli_fetch_array($result))
{
//the 10 lines of codes above
}
You may want to loop through these elements:
$sql = "SELECT * FROM sheet1 WHERE id = '$_POST[text]'";
$result = mysqli_query($con,$sql);
If ($row = mysqli_fetch_array($result))
{
for($i = 2; $i < 10; $i++){
$c = mysqli_fetch_field_direct($result,$i)->name;
echo $row[$i] ; echo $c[$i];
}
}

give a unique number from 1 to 200 for each row? assign a number for each row

I have this small code to display 200 rows and I want to give each row a number a per the no of votes.
How can I do it?
This is what I thought of but it does not work.
$sql = 'SELECT * FROM table LIMIT 200;
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
$row = 1;
echo $row++;
echo $row['fullname'];
echo '<br>';
}
the output is making all row output to 1.
edit :
okay so some of the answers did the job but it does not work on the pagination. it counts from 1 on page 2,3,4,5.... heres the code.
<?php
$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);
$result_per_page = 20;
$no_of_result = mysqli_num_rows($result);
$no_of_page = ceil($no_of_result/$result_per_page);
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}
$page_first = ($page-1)*$result_per_page;
$sql = 'SELECT * FROM table LIMIT ' . $page_first . ',' . $result_per_page;
$result = mysqli_query($conn, $sql);
$num = 1;
while($row = mysqli_fetch_assoc($result)) {
echo $num++;
echo $row['fullname'];
echo '<br>';
}
for($page=1;$page<=$no_of_page;$page++){
echo ''.$page.'';
echo ' ';
}
?>
Try to read your code line by line and see what it does.
while($row = mysqli_fetch_assoc($result)) { # > Set variable $row to the next result set or exit when there are none.
$row = 1; # Set variable $row to value 1.
echo $row++; # Increment variable $row to + 1.
echo $row['fullname']; # Row is now an integer, not an array. (You did that 2 statements ago)
echo '<br>'; #
} // end of loop, start again --------------|
A solution would be:
$id = 1;
while($row = mysqli_fetch_assoc($result)) {
echo $id++;
echo $row['fullname'];
echo '<br>';
}
As for the page, you're overwriting the previous value:
for($s=$page;$s<=$no_of_page;$s++){
echo ''.$s.'';
echo ' ';
}

Check if FK order is numbered correctly

I have some absences, and each absence has an FK to an employee, which I wanna display in a generated HTML table. However, if I delete an employee the order changes from 0, 1, 2 to 1, 2 (for example if the second employee got deleted).
This messed up my code, because I need the FK to check where I have to insert the leave (in which <tr>). Here I count the employees:
$result = mysql_query("select count(1) FROM employee");
$row = mysql_fetch_array($result);
$count_user = $row[0];
Later in my code I make a query in a loop. The loop runs as many times as there are users. And heres the problem: if an employee gets deleted it will not reach the other one ($i goes 0 and then 1 if there are 2 users, but if one is deleted the FK is 3, so it would need to go one further).
to
$result = mysql_query("select start, end, type_FK, employee_FK FROM absences where employee_FK = $i");
while ($row = mysql_fetch_assoc($result)) {
$array_absences[] = $row;
}
Has anyone an Idea that is not based on the FK order?
Also this topic needs a better title so feel free to edit it.
<html>
<head>
<title>Absence System</title>
</head>
<body>
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("absence_system", $con);
$result = mysql_query("select count(1) FROM employee");
$row = mysql_fetch_array($result);
$count_user = $row[0];
$result = mysql_query("select count(1) FROM absences");
$row = mysql_fetch_array($result);
$count_absences = $row[0];
$result = mysql_query("select name, surename, employee_ID FROM employee");
while ($row = mysql_fetch_assoc($result)) {
$array_user[] = $row;
}
for($i = 0; $i < $count_user; $i++){
echo '<table = border = 1px>';
echo '</tr>';
$result = mysql_query("select start, end, type_FK, employee_FK FROM absences where employee_FK = $i");
while ($row = mysql_fetch_assoc($result)) {
$array_absences[] = $row;
}
$count = count($array_absences);
echo $count;
print_r($array_absences);
for($j = 0; $j < 32; $j++){
$true = 0;
if($j == 0){
echo '<td>';
echo $array_user[$i]['name'], " ", $array_user[$i]['surename'] ;
echo '</td>';
}
for($k = 0; $k < $count; $k++)
{
$array_absences[$k]['start'] = substr($array_absences[$k]['start'], -2);
$array_absences[$k]['end'] = substr($array_absences[$k]['end'], -2);
$array_absences[$k]['start'] = ereg_replace("^0", "", $array_absences[$k]['start']);
$array_absences[$k]['end'] = ereg_replace("^0", "", $array_absences[$k]['end']);
if($j == $array_absences[$k]['start'] && $array_absences[$k]['employee_FK'] == $i){
$true = 1;
echo '<td>';
echo $array_absences[$k]['type_FK'];
echo '</td>';
}
}
if($j != 0 && $true == 0){
echo '<td>';
echo "$j";
echo '</td>';
}
}
echo '</tr>';
echo '</table>';
}
?>
</body>
</html>
If employee_ID corresponds to employee_FK, then you should not iterate over $count_user, but rather over $array_user - then your select would looke like this:
$result = mysql_query("select start, end, type_FK, employee_FK FROM absences where employee_FK = {$array_user[$i]['employee_ID']}");
while $i will still represent sequence number, because
$count_user == count($array_user)

How to add a </tr><tr> automatically after every 2 <td>

i am retrieving results from MySQL with PHP based on user search
i want to add a automatically after every 2 s in the result
<?php
include_once("config.php");
isset( $_REQUEST['name'] ) ? $name=$_REQUEST['name'] : $name='';
$name = mysql_real_escape_string( $name );
if (strlen($name) >= 4) {
$sql = "select * from places where speciality like '%$name%'";
$rs = mysql_query( $sql ) or die('Database Error: ' . mysql_error
$num = mysql_num_rows( $rs );
if($num >= 1 ){
echo "<table id='result-table'><tr>";
while($row = mysql_fetch_array( $rs )){
echo "<td>row[cname]</td>"; //here i want to add </tr><tr> after 2 <td>s
}
}else{
echo "no records found</table>";
}
}
?>
Used $i = 1; if ($i % 2 == 0) and $i++;
<?php
include_once ("config.php");
isset ($_REQUEST['name']) ? $name = $_REQUEST['name'] : $name = '';
$name = mysql_real_escape_string($name);
if (strlen($name) >= 4) {
$sql = "select * from places where speciality like '%$name%'";
$rs = mysql_query($sql) or die('Database Error: '.mysql_error $num = mysql_num_rows($rs);
if ($num >= 1) {
$i = 1;
echo "<table id='result-table'><tr>";
while ($row = mysql_fetch_array($rs)) {
echo "<td>row[cname]</td>"; //here i want to add </tr><tr> after 2 <td>s
if ($i % 2 == 0)
echo "</tr><tr>";
$i++;
}
}
else {
echo "no records found</table>";
}
}
?>
echo "<table id='result-table'><tr>";
$currentCount = -1;
while($row = mysql_fetch_array($rs))
{
echo "<td>row[cname]</td>";
$currentCount = ($currentCount + 1) % 2;
if($currentCount == 1)
{
echo '</tr><tr>';
}
}
I suggest not putting them into a table at all; instead simply put them into <div> elements, with float:left and width:50% styles.
If you must put them into a table, and you want to do it the way you asked, you can use PHP's modulo operator (%) to divide the current record number by 2 and get the remainder. If it's 1, then add the <tr> tag:
if(++$rowNum % 2) {print "</tr><tr>";}

PHP Counting MYSQL rows issue?

I am trying to make it to if I have at least 1 team created for one person, it will display the team. If they don't have a team, it will say no teams. It works if the person has at least 1 team, but nothing shows if the person is not on a team. How do I fix this?
<?php
$sql = mysql_query("SELECT * FROM teams WHERE players LIKE '%$sessiongamt%'") or die("Could not allocate information!");
$num = 0;
while($row = mysql_fetch_assoc($sql)){
$num = ++$num;
$amount1 = mysql_num_rows($sql);
$name = $row["name"];
$teamrank = $row["rank"];
$teamlink = $row["link"];
$players = $row["players"];
$teamid = $row['id'];
if($amount1 < 1){
$teams = "No Teams";
echo "$amount";
}else{
$teams = "$name";
echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}
}print "$amount1";
?>
Look at the third line of this code - put it in your code on the same place instead of your line.
if($amount1 < 1){
$teams = "No Teams";
echo $teams;
}else{
$teams = "$name";
echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}
Put the counting outside the while... loop
$sql = mysql_query("SELECT * FROM teams WHERE players LIKE '%$sessiongamt%'") or die("Could not allocate information!");
$amount1 = mysql_num_rows($sql); //<---- this should fix it
$num = 0;
while($row = mysql_fetch_assoc($sql)){
$num = ++$num;
$name = $row["name"];
$teamrank = $row["rank"];
$teamlink = $row["link"];
$players = $row["players"];
$teamid = $row['id'];
$teams = "$name";
echo "<a href='$teamlink?id=$teamid'>$teams</a>";
}
if($amount1 < 1){
$teams = "No Teams";
}
print "$amount1";
?>

Categories