How to print row number in mysql in PHP application

8.1k views Asked by At

I have a mysql table need to display the data along with the row number in front end aplication. The following query works perfectly in phpMyadmin

SET @row_num=0; SELECT (@row_num:=@row_num+1) AS num,INV,DOS,PTNAME,BAL,PROV     from sheet;

But when i use the same code in php projects it is returning the following error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL

Below is my code:

<?php 
$conn = mysql_connect("localhost","root","");
mysql_select_db("omega",$conn);
$sel="SET @row_num=0; SELECT (@row_num:=@row_num+1) AS             num,INV,DOS,PTNAME,BAL,PROV from sheet";
$sqlquery=mysql_query($sel);

while($dis=mysql_fetch_array($sqlquery))
{
echo"<tr>";
echo "<td>".$dis['num']."</td>";
echo "<td>".$dis['INV']."</td>";
echo "<td>".$dis['DOS']."</td>";
echo "<td>".$dis['PTNAME']."</td>"; 
echo "<td>".$dis['BAL']."</td>";    
echo "<td>".$dis['PROV']."</td>";       
echo"</tr>";
}
4

There are 4 answers

1
dhruv jadia On BEST ANSWER

change from

$sel="SET @row_num=0; SELECT (@row_num:=@row_num+1) AS num,INV,DOS,PTNAME,BAL,PROV from sheet";

to

$sel="SELECT s.*, @rownum := @rownum + 1 AS num FROM sheet s, (SELECT @rownum := 0) r";
11
Panda On

Your query doesn't work because you set 2 queries in mysql_query which is not supported. Instead use mysqli_multi_query() function.

Anyway, mysql_* functions are already deprecated, so use mysqli_* functions instead.

$conn = mysqli_connect("localhost", "root", "", "omega");

$sel = "SET @row_num=0;";
$sel .= "SELECT (@row_num:=@row_num+1) AS num, INV, DOS, PTNAME, BAL, PROV from sheet";
$sqlquery = mysqli_multi_query($conn, $sel);

while($dis = mysqli_fetch_array($sqlquery))
{

// rest of your code
1
J Ha On

i took a look and think you were tried to make complex, see the code below, it's easy for your purpose

<?php 
$conn = mysql_connect("localhost","root","");
mysql_select_db("omega",$conn);
$sel="SET INV,DOS,PTNAME,BAL,PROV from sheet";
$sqlquery=mysql_query($sel);
$i=0;
while($dis=mysql_fetch_array($sqlquery))
{
echo"<tr>";
echo "<td>".$i."</td>";
echo "<td>".$dis['INV']."</td>";
echo "<td>".$dis['DOS']."</td>";
echo "<td>".$dis['PTNAME']."</td>"; 
echo "<td>".$dis['BAL']."</td>";    
echo "<td>".$dis['PROV']."</td>";       
echo"</tr>";
$i++
}
?>
0
Pankaj On

Here I have solution for you , How you will get total rows number using PHP nd MYSQL . Please see below code.

    <?php
           $pdoconnection = 
          "mysql:host=localhost;dbname=database_name;charset=utf8mb4";
           $options = [
          PDO::ATTR_EMULATE_PREPARES => false,
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
         PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
      ];
   try {
        $pdo = new PDO($pdoconnection, "userName", "passWord", $options);
        $emp=$pdo->prepare("SELECT count(*) FROM emp_tab");
        $emp->execute();
        $emprow = $emp->fetch(PDO::FETCH_NUM);
      //total count
       $empcount = $emprow[0];
       } catch (Exception $e) {
          error_log($e->getMessage());
             exit('oops ! some problems');
          }
 ?>

I hope , you will get some idea using in php and mysql to get total number of rows.