Hi i have a php form and part of this is to get the last "shiftID" row entry in my table and put this into a variable so i can later add 1 to said variable. However the result of the following code returns the information linked below. How do i get the last "shiftID" number by itself into a variable.
<?php
session_start();
include 'dbh.php';
$start = $_GET['starttime'];
$finish = $_GET['finishtime'];
$dat = $_GET['date'];
$id = $_GET['userid'];
$shiftidd = $conn->query("SELECT shiftID FROM shift_user ORDER BY shiftID DESC LIMIT 1");
$row = mysqli_fetch_row($shiftidd);
echo print_r($row[0]);
//$result = $conn->query("INSERT INTO shift (shiftStart, shiftFinish, shiftDate)
//VALUES ('$start', '$finish', '$dat')");
//$sql = $conn->query("INSERT INTO shift_user (shiftID, userID) VALUES ('$shiftidd', '$id')");
//header("Location: shifts.php");
?>
Web page result:
"connected 41"
I'm looking for the number "4" but I'm guessing the "1" is the affected row along with the result? but how do i get rid of the "1"?
Thanks in advance.
1 is a result of print_r() which is superfluous here. Use either echo or print_r, but not both
You can do it like. After you insert query executed use the following function
mysql_query("INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
$id = mysql_insert_id();//will return you the last id inserted into table//
But if you want to find the last id of some table then you have to use the follwoing code:
$shiftidd = $conn->query("SELECT MAX(shiftID) FROM shift_user LIMIT 1");
$row = mysqli_fetch_row($shiftidd);
echo print_r($row[0])///////it will be the last id of the table////////
if you want to find next id please try this
$shiftidd = $conn->query("SELECT MAX(shiftID)+1 FROM shift_user LIMIT 1");
$row = mysqli_fetch_row($shiftidd);
echo print_r($row[0])///////it will be the next id of the table///////////
Can anyone tell me what I am doing wrong here? This code doesn't echo out anything. I would like to see the number of rows containing $tag in the urlslug field.
<?php
$query = mysqli_query($mysqli, "SELECT * FROM tags WHERE urlslug='$tag'");
$num_rows = mysql_num_rows($query);
echo $num_rows;
Thanks! :)
You're using mysqli_query function, but then you have the deprecated mysql_num_rows function. Try mysqli_num_rows instead.
It is not very efficient to select everything in a table, count it, and then throw it away again. You should use the MySQL COUNT function to count the rows:
$result = mysqli_query($mysqli, "SELECT COUNT(*) as numRows FROM tags WHERE urlslug='$tag'");
$data = mysqli_fetch_array($result);
var_dump($data);
try this
mysqli_num_rows($query)
Try this
<?php
$query = mysqli_query($mysqli, "SELECT * FROM tags WHERE urlslug='$tag'");
$num_rows = mysqli_num_rows($mysqli,$query);
echo $num_rows;
?>
my ans with #Sverri M. Olsen's ans Number of rows in a MySQL table?:
When you COUNT(*) it takes in count column indexes, so it will be the best result. Mysql with MyISAM engine actually stores row count, it doensn't count all rows each time you try to count all rows. (based on primary key's column)
If the COUNT(*) is slow, you should run EXPLAIN on the query, and check if indexes are really used, and where should they be added.
and +1 for #Sverri M. Olsen
try this
$query = mysqli_query($mysqli, "SELECT * FROM tags WHERE urlslug='".$tag."'");
$num_rows = mysqli_num_rows($query);
echo $num_rows;
I am trying to perform a query inside a while loop that is getting values from a column. I am trying to query first to get all my values from a column in my DB and then get the count of how many times that value is in that column.
Examples of output trying to get
myValue is in the column 3 times
myOtherValue is in the column 10 times
myOtherOtherValue is in the column 22 times
Example of code
$sql = "SELECT DISTINCT id, columnName FROM tableName";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
function myCount($id)
{
$query = "SELECT COUNT(*) FROM tableName WHERE name = '$id'";
$result = mysql_query($query);
$count = mysql_fetch_array($result);
}
echo "$id is in the column $count[0] times";
}
You can't define a function inside the while loop.
Using COUNT(*) with a GROUP BY name clause, then you could solve the problem with only one query.
I'm trying to get my highcharts chart working and I'm almost there.. I just have one little problem: I need that the value will be the total count of the records at the same day but I'm kinda confused with my code now and the chart is totally messed up..
Here is the code that pulls the data:
<?php
header("Content-type: text/json");
include('../includes/config.php');
$tablename = "analytics";
$result = mysql_query("SELECT COUNT(*) AS count FROM $tablename");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$sql = "SELECT id, date FROM $tablename ORDER BY date";
$result = mysql_query( $sql ) or die("Couldn't execute query.".mysql_error());
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$row['id'] = (int) $row['id'];
$rows[$i] = array(strtotime($row['date'])*1000, $row['id']);
$i++;
}
echo json_encode($rows);
?>
If it will help here is my database values:
insert into `analytics`(`id`,`user`,`item`,`ip`,`country`,`date`) values
(10,1,1,'127.0.0.1','','2011-12-17 06:41:51'),
(11,1,1,'127.0.0.1','','2011-12-17 06:42:23'),
(12,1,1,'127.0.0.1','','2011-12-17 06:43:07'),
(13,1,1,'127.0.0.1','','2011-12-17 06:44:19'),
(14,1,1,'127.0.0.1','','2011-12-17 06:44:21'),
(15,1,1,'127.0.0.1','','2011-12-17 06:44:22'),
(16,1,1,'127.0.0.1','','2011-12-17 06:44:49'),
(17,1,1,'127.0.0.1','','2011-12-17 06:46:59'),
(18,1,1,'127.0.0.1','','2011-12-17 06:47:20'),
(19,1,1,'127.0.0.1','','2011-12-17 06:47:35'),
(20,1,1,'127.0.0.1','','2011-12-17 06:47:42'),
(21,1,1,'127.0.0.1','','2011-12-17 06:48:07'),
(22,1,1,'127.0.0.1','','2011-12-17 06:48:14'),
(23,1,1,'127.0.0.1','','2011-12-17 06:48:29'),
(24,1,1,'127.0.0.1','','2011-12-18 06:49:10'),
(25,1,1,'127.0.0.1','','2011-12-19 07:05:45'),
(26,1,1,'127.0.0.1','','2011-12-20 08:11:32'),
(27,1,1,'127.0.0.1','','2011-12-21 08:26:45'),
(28,1,1,'127.0.0.1','','2011-12-17 08:44:34');
And here is the final result:
I totally lost my self here, can someone help?
EDIT: did what #ajreal said and here is the output:
This is the query to get count for each date
SELECT date, COUNT(*) AS count
FROM $tablename
GROUP BY date;
You can use this query to replace your first query.
And to a loop (like your second query), set $total += $row["count"] to get a grand total like your original first query does
$u_id=$event_assoc['Uniqueid'];
echo $u_id."\n";
$result1 = mysql_query("SELECT * FROM eventdetail WHERE unique_id = '$u_id'", $con1);
while($row = mysql_fetch_array($result1))
{
echo 'in eventdetail'."\n";
$e_id= $row['event_id'];
$destination= $row['destination'];
$uniqueid= $row['unique_id'];
$call_num= $row['channelid'];
}
echo mysql_num_rows($result1);
echo $e_id."\n";
echo $destination."\n";
echo $call_num."\n";
echo $uniqueid."\n";
if(mysql_num_rows($result1)>0)
{
echo 'calculate'."\n";
$result= mysql_query("SELECT sum(billsec)
FROM cdr
WHERE uniqueid = '$uniqueid'", $con2);
$bil = mysql_fetch_array($result);
$bill= (float) $bil['sum(billsec)'];
echo $bill."\n";
this is my code..
whenever i try to execute sum function query it retruns top row's billsec instead of addition of all row's billsec
You are doing a where on an unique ID in the sum query. Remove the where and it'll work
You are using unique id in your query.. And in a table single row will have that id, thats why you are getting top row's billsec... Remove the where clause in second query, and then check.
$result= mysql_query("SELECT sum(billsec)
FROM cdr
WHERE uniqueid = '$uniqueid'", $con2);
i guess there is a bug in this area...look again
The PHP statement for the SQL SUM Query should be:-
$result= mysql_query("SELECT sum(`billsec`), `uniqueid` FROM `cdr` GROUP BY `uniqueid` HAVING `uniqueid` = '$uniqueid'", $con2);
You need to use the clause "GROUP BY" whenever you are using any MySQL Aggregate functions (if needed). Also if you are using any aggregate functions (like "SUM", "MAX" etc), then you cannot use "WHERE" clause on the field (in this case, the field is "uniqueid") which is being used in the "GROUP BY" clause.
Hope it helps.