How to view last inserted value of auto increment field - php

In my database I have a field called spaj_per as an auto increment primary key.
How can I display the lastest inserted value in the field spaj_per. I tried this.
<?php
$con = mysql_connect("localhost", "SuperAdmin", "***");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("sistem_pengurusan_fail",$con);
$q = "SELECT MAX(id) AS spaj_per FROM unit_pengambilan";
$result = mysql_query($q);
$row = mysql_fetch_array($result);
?>
<?php echo $row['spaj_per']; ?>
But it won't work.

You may use
SELECT id FROM mytable ORDER BY id DESC LIMIT 1;
or like
$last_id = mysql_insert_id();

Try this,
$q = "SELECT MAX(spaj_per) AS spaj_per FROM unit_pengambilan";
$result =mysql_query($q);
$row = mysql_fetch_row($result);
echo $row[0];

Use PHP's mysql_insert_id() function

Try the following
<?
$q = "SELECT MAX(spaj_per) AS last_spaj_per FROM unit_pengambilan";
$result =mysql_query($q);
$row = mysql_fetch_array($result);
echo $row['last_spaj_per'];
?>

$row = mysql_fetch_array($result); Change to $row = mysql_fetch_assoc($result); and try

Try with mysql_insert_id
For more reference use following link
http://php.net/manual/en/function.mysql-insert-id.php

Related

How to get total number of row by using this function php mysql?

How to get total number of row by using this function php mysql ?
i use this code for display data from mysql. It's work good,
Buy i want to know can i get total number of row by using this code ?
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
Try this mysql_num_rows ($result)
Use this line of code
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
$number_of_rows = mysql_num_rows($result); // this will return the number of rows found by the $result query.
echo $number_of_rows;
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>

Get all values in column MySQL and count PHP

I try to create a function that will get all values (int) in specific column in my database (MySQL), and create the total of all values. I have an error: Resource id #12
This is my function:
function vuesCours() {
$con = mysql_connect("localhost","root","root");
mysql_select_db("myDataBase", $con);
$result = mysql_query("SELECT SUM(views) FROM articles");
return $result;
}
And this my declaration:
<h4><?php echo vuesCours(); ?></h4>
Thank you in advance for your response.
This is not error just you have to do one thing more after query, so try to replace following code and try again.
function vuesCours() {
$con = mysql_connect("localhost","root","root");
mysql_select_db("myDataBase", $con);
$result = mysql_query("SELECT SUM(views) as total FROM articles");
$row = mysql_fetch_array($result);
return $row['total'];
}
It may help you.
you need to return not results but the value of row. something like this:
function vuesCours() {
$con = mysql_connect("localhost","root","root");
mysql_select_db("myDataBase", $con);
$result = mysql_query("SELECT SUM(views) as total FROM articles");
$row = mysql_fetch_array($result)
return $row['total'];
}
hope this helps.
$con = mysql_connect("localhost","root","root");
mysql_select_db("myDataBase", $con);
$result = mysql_query("SELECT views FROM articles");
$sum=0;
while($row=(mysql_fetch_row($result)))
{
$sum=$sum+$row['views'];
}
echo $sum;
1) i have just changed the select query
2)we have result set of query in $result
3)mysql_fetch_row will return result row as an enumerated array
from which you can access
particular column by it's name or by giving id into subscript brackets in a line $sum=$sum+$row[index/name of column];

Query not returning any results

I know that $wins should be 3. Because I have 3 rows with the integer "1" in the "win" column on table "rated_teams" But for some reason this code will not work. Can you find the problem please? ALSO, I'm aware that some of this is depracted. I'll update the whole page, once I get it at least in working condition.
<?php
$sql = "SELECT SUM(win) FROM rated_teams WHERE server='$server' AND name='$myteam'";
$query = mysql_query($sql, $con)
or die('A error occured: ' . mysql_error());
while ((mysql_fetch_array($query))) {
$wins = $row['SUM(win)'];
}
?>
<h3>Total Wins: <?php echo $wins?> </h3>
Try with
$sql = "SELECT SUM(win) as sum FROM rated_teams WHERE server='$server' AND name='$myteam'";
and while you are getting give like
while ($row = mysql_fetch_array($query)) {
$wins = $row['sum'];
}
And my advice is try to avoid mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements.
You do not set the $row variable. Edit your while to this.
while ($row = mysql_fetch_array($query))
You need to give your calculated column an alias. Try this:
<?php
$sql = "SELECT SUM(win) as sumwin FROM rated_teams WHERE server='$server' AND name='$myteam'";
$query = mysql_query($sql, $con) or die('A error occured: ' . mysql_error());
while ($row = mysql_fetch_array($query)) {
$wins = $row['sumwin'];
}
?>
<h3>Total Wins: <?php echo $wins?> </h3>
Please write sql query right manner.Write like this.
$sql = "SELECT SUM(win) as sumwin FROM rated_teams WHERE server='".$server."' AND name='".$myteam."'";
while ((mysql_fetch_array($query))) {
should be
while ($row = mysql_fetch_array($query) ) {

MySQL/Php: nonrepeating random selection with unique ID selectors

I'm new to using php and mysqp.
Using the code below, I am calling and echoing four names from a mysql database (in a 'names' column) without repeat.
However, what I'd like to do is assign a unique CSS id selector to each name.
The intent is to have four individually styled boxes around the page, each with a different name chosen randomly upon load.
What would be the best code to do this?
(Fyi I am using an xxamp installation.)
Thanks!
<?php
$sandbox = mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("sandbox", $sandbox);
$sql = "SELECT * FROM names ORDER BY RAND() LIMIT 4";
$result = mysql_query($sql, $sandbox);
while ($row = mysql_fetch_array ($result)) {
$name1 = $row['Name'];
echo $name1 . '<br>' ;
}
Would this work for you:
<?php
$sandbox = mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("sandbox", $sandbox);
$sql = "SELECT * FROM names ORDER BY RAND() GROUP BY Name LIMIT 4";
$result = mysql_query($sql, $sandbox);
$i = 0;
while ($row = mysql_fetch_array ($result)) {
echo '<div id=box"'.$i++.'">'.$row['Name'].'</div>';
}
This gives you an unique CSS selector using the primary key in your table?
This should work.
$sandbox = mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("sandbox", $sandbox);
$sql = "SELECT * FROM names ORDER BY RAND() LIMIT 4";
$result = mysql_query($sql, $sandbox);
$i = 0;
while ($row = mysql_fetch_array ($result)) {
$id = $row['ID'];
echo '<div id=box"'.$i++.'">your content</div>';
}

How to I retrieve data from a mysql table cell and put it into a variable in php?

I am creating a function that will retrieve an id number from a table with multiple columns, put it in a variable which i want to use to send as a "claims number" in an email to a claimant and also echo on a confirmation screen. Here's what I have so far.
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$claim_id = $result;
$result = mysql_fetch_array(mysql_query($sql));
$claim_id = $result['id'];
Supposing you are sure that you will receive 1 row.
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$c = mysql_fetch_assoc($result);
$claim_id = $c['lname'];
NB*: And get ready for a lecture. Someone is going to tell you that mysql is depracated.
Start using MySQLi or PDO (recommended).
Try :
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$claim_id = $row[0];
or
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$claim_id = $row['id'];
}
$row = mysql_fetch_array($result);
$claim_id = $row['id'];
please note that you should also check for errors (e.g. if($result === FALSE)). Also, there are other ways to fetch data - I recommend looking at mysql_fetch_array, mysql_fetch_row, mysql_fetch_assoc and mysql_fetch_object
Also, don't rely on the statement always returning exactly one row - make sure that the result is as expected: mysql_num_rows
Give this a try:
$sql = "SELECT id FROM warranty_claim where lname='".$lname."'";
$result = mysql_query($sql);
$objResult = mysql_fetch_array($result);
$claim_id = $objResult['id'];

Categories