Reputation: 139
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>";
}
Upvotes: 4
Views: 8299
Reputation: 31
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.
Upvotes: 0
Reputation: 1680
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";
Upvotes: 5
Reputation: 6896
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
Upvotes: 2
Reputation: 1262
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++
}
?>
Upvotes: 1