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///////////
I am trying to finish my question of the day script. I have a random number generator (external) which is inserted into a table daily. I take that number and pull the corresponding question from another table.
// Query 1
$query1 = "SELECT $field1 FROM $table1 WHERE id=1";
$result1 = mysqli_query($con1, $query1);
// Query 2
$query2 = "SELECT $field2 FROM $table2 WHERE QNum = $result1";
$result2 = mysqli_query($con2, $query2);
// Display question
while($row = mysqli_fetch_array($result2)) {
echo $row['question'];
}
$result1 is pulling a random number, lets say its 9. When $result1 is used to pull the question, it doesn't work but when I replace $result1 with number 9, it works. I experimented with syntax and eventually figured it could be a problem with a string vice an integer.
I tried to cast it as an integer but it keeps assigning the value of $result1 to 1. I am at a loss. I don't understand if the string is a 9, how converting it to an integer would change its value.
I feel like I've tried everything after days of experimenting out there but I am sure it is something very simple. Please help.
You're right, it's something very simple. You need to fetch the row to get the value:
$row = mysqli_fetch_assoc($result1);
$qnum = $row[$field1];
$query2 = "SELECT $field2 FROM $table2 WHERE QNum = $qnum";
You seem to understand this in general, since you call mysql_fetch_array to get the results of the second query. Why should the first query be any different?
However, you can do all this in one query:
$query2 = "SELECT t2.$field2
FROM $table1 t1
JOIN $table2 t2 ON t1.$field1 = t2.QNum
WHERE t1.id = 1";
Have you checked whats returned in $result1 ??
it should be an object which cannot be used inside the second mysql query. First you have to get the value from that query by fetching the query.
$result1 = mysqli_query($con1, $query1);
$row = mysqli_fetch_array($result1, MYSQLI_NUM);
$random_number = $row[0];
this $random_number then can be used in the second query. hope this will fix your error.
So I have a mysql table that contains an integer in each row. What I am trying to do is get the sum of the integers. Here is what I currently have that is working.
$cn = 0;
$sql = mysqli_query($con,"SELECT * FROM members WHERE member='$userid'");
while($row = mysqli_fetch_array($sql)) {
$i = $row['number'];
$cn = $cn + $i;
}
echo $cn;
So I make $cn equal zero then each time it goes through the loop it will add the number from the matching row.
Does anyone have a better idea on how to accomplish this? Thanks!
You don't need to use PHP or here a loop for that, because your database can do the job for you.
$sql = mysqli_query($con,"SELECT sum(number) FROM members WHERE member='$userid'");
If you want the sum of the column number for each member with a particular user id
$sum = 0;
$sql = mysqli_query($con,"SELECT SUM(number) as number_total FROM members WHERE member='$userid'");
while($row = mysqli_fetch_array($sql)) {
$sum = $row['number_total'];
}
$sum will have the total
Every time when I'm running the following PHP code I'm getting output
Booking Confirmed! Congratulation. Your Booking Id is: 6
for successful booking.
The bookingid is a sequence and then I'm getting that booking id form the inserted values to get the current value of the sequence. So I don't know what I'm doing wrong.
<html><body>
<?php
$con = oci_connect("system", "password", "localhost/XE");
if (!$con) {
$m = oci_error();
exit('Connect Error ' . $m['message']);
}
$thid = $_GET["hid"];
$trno = $_GET["rno"];
$tgid = $_GET["gid"];
$sd = $_GET["sdate"];
$ed = $_GET["edate"];
$dchange = "ALTER SESSION SET NLS_DATE_FORMAT= 'YYYY-MM-DD'";
$stid1 = oci_parse($con,$dchange);
oci_execute($stid1);
$c1 = "SELECT * FROM B WHERE HOTELID = '$thid' AND ROOMNO = '$trno' AND ((STARTDATE < '$sd' AND ENDDATE > '$sd') or (STARTDATE < '$ed' AND ENDDATE > '$ed') or (STARTDATE >= '$sd' AND ENDDATE <= '$ed'))";
$c2 = oci_parse($con, $c1);
oci_execute($c2);
$row = oci_fetch_row($c2);
if(!$row)
{
$temp = "INSERT INTO B VALUES(bno.nextval,'$thid','$trno','$tgid','$sd','$ed')";
$stid = oci_parse($con,$temp);
oci_execute($stid);
oci_free_statement($stid);
//$c7 = "SELECT bookid FROM B WHERE HOTELID = '$thid' AND ROOMNO = '$trno' AND GUEStID = '$tgid' AND STARTDATE = '$sd' AND ENDDATE = '$ed'";
//printf("<h3>Booking Confirmed! Congatulation. </h3>") ;
**$c8 = oci_parse($con, "SELECT MAX(BOOKID) FROM B");
oci_execute($c8);
printf("<h3>Booking Confirmed! Congratulation. Your Booking Id is: %u</h3>", $c8);**
}
else
printf("<h3>Booking already exist. </br>Please try with another search.</h3>");
?>
</n> <form action="index.php"><input type="submit" value="BACK" />
</form>
</BODY>
a few suggestions for you.
you should be using bind variables and not splicing literals in your SQL statements (unless you want the performance of your DB to degrade as you fill the shared pool up).
SELECT MAX(BOOKID) FROM B is the wrong and unsafe way to get the booking id. as if 2 sessions in parallel made a booking, you could get wrong results (you can also get wrong results if the max id in the table is higher than the current sequence value). instead do select bno.currval from dual or use the returning clause as part of the insert (assuming PHP works with that)
on your insert you should specify the column names for good practice . i.e do INSERT INTO B (BOOKID, HOTELID, ROOMNO, STARTDATE, ENDDATE) VALUES(bno.nextval....
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