I try to get the title and mess in last id5, this is the result.
php code :
<?php
$hostname_mus_db = "localhost";
$database_mus_db = "message";
$username_mus_db = "root";
$password_mus_db = "";
$tf=#mysql_connect($hostname_mus_db,$username_mus_db,$password_mus_db);
if(!$tf)
die('Connection problem1 ..');
$tf_db=mysql_select_db($database_mus_db);
if(!$tf_db)
{
#mysql_close($tf_handle);
die('selection problem2 ..');
}
$co_array=array();
//if (isset($_GET['co'])) {
// $co=$_GET['co'];
$sql = mysql_query("SELECT * FROM mha1 where id>'1'"); /// here i edit the variable
while($row = mysql_fetch_assoc($sql)){
$co_array[]=$row;
}
// }
header('Content-Type:application/json');
echo json_encode(array("date"=>$co_array));
?>
in first the code is :
<?php
$hostname_mus_db = "localhost";
$database_mus_db = "message";
$username_mus_db = "root";
$password_mus_db = "";
$tf=#mysql_connect($hostname_mus_db,$username_mus_db,$password_mus_db);
if(!$tf)
die('Connection problem1 ..');
$tf_db=mysql_select_db($database_mus_db);
if(!$tf_db)
{
#mysql_close($tf_handle);
die('selection problem2 ..');
}
$co_array=array();
if (isset($_GET['co'])) {
$co=$_GET['co'];
$sql = mysql_query("SELECT * FROM mha1 where id>".$co);
while($row = mysql_fetch_assoc($sql)){
$co_array[]=$row;
}
}
header('Content-Type:application/json');
echo json_encode(array("date"=>$co_array));
?>
but the result was
How can I fix it ?? how can I make the result is just id 5 not all id.
I hope i understand your question correctly.
If you want to know the last entry in a table and id is your primary key, use DESCENDING ORDER AND LIMIT
$sql = mysql_query("SELECT id FROM mha1 ORDER BY id DESC LIMIT 1");
If you insert a new dataset and want to know the last inserted id you can use insert_id
$sql = mysql_query("INSERT INTO mha1 (id) VALUES ('')");
$last_id=mysql_insert_id();
You can just get all the results with this query in descending order then last inserted id is on top then fetch eaisly.
$sql = mysql_query("SELECT * FROM tablename ORDER BY id DESC limit 0,1 ");
Please just change this part:
$sql = mysql_query("SELECT * FROM mha1 where id>".$co);
to this:
$sql = mysql_query("SELECT * FROM mha1 where id=".$co);
then $co can be an value of id you want.
Related
I have written a code in PHP and not able to get a response from the code. Following is my code.
<?php
include_once("conectionFile.php");
$db = new Data();
$con = $db->Conn();
$val = $_REQUEST;
$lclQuery = "SELECT * FROM student_details WHERE st_id = ".$val."";
$lclResult = $con->query($lclQuery);
if($row = $lclResult->fetchAll(PDO::FETCH_ASSOC)) {
echo $res = json_encode($row);
}
?>
$lclQuery = "SELECT * FROM student_details WHERE st_id = ".$val."";
You need to change your query as it is not correct also $val is array so you need to use correct key there in order to get value for example $val = $_REQUEST['some_id'] then change query like
$lclQuery = "SELECT * FROM student_details WHERE st_id = '".$val."'";
I have case manager table where i have inserted court table id as foreign key. i want to fetch record from both tables. when using nested while loop it shows only one row data.
$id = $_SESSION['id'];
$query1 = "SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn, "$query1");
while($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$Status = $row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 = "SELECT * from `case_type` where case_id = '$case_type'";
if($result1 = mysqli_query($conn, "$query2")) {
while($row2 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
Because you are overwritting you $result1 change inner query result to $result2 then try
$id = $_SESSION['id'];
$query1 ="SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn , "$query1");
while ($row = mysqli_fetch_array($result1 ,MYSQLI_ASSOC)) {
$Status=$row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 ="SELECT * from `case_type` where case_id = '$case_type'";
if($result2 = mysqli_query($conn , "$query2")){;
while ($row2 = mysqli_fetch_array($result2 ,MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
1st : Because you are overwriting the variable $result1 In second query execution.
if($result1 = mysqli_query($conn , "$query2")){;
^^^^^^^^ ^^
Note : And remove that unnecessary semicolon .
2nd : No need multiple query simple use join
SELECT cm.*,c.* from `case_manager` cm
join `case_type` c
on cm.cas_type=c.case_id
where cm.user_id=$id;
You can use below query to fetch your record:
$query = SELECT case_manager.* ,case_type.case_name FROM case_manager Left JOIN case_type ON case_manager.case_type=case_type.case_id where case_manger.user_id = $id;
While($row = mysql_fetch_array()){
echo $row['case_name'];
}
I want to order data by Id, how can i do this ?
if($_GET["grupid"]>0){
$DUZEN = array();
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"];
$rsDuzen = mysql_query($sql, $conn) or die(mysql_error());
while ($r = mysql_fetch_assoc($rsDuzen)) {
$DUZEN[] = $r;
}
}
i can read all data with this code which have same group id. But data aline random.
You have to use mysql order clause in your query like order by id asc. Which you can use at the end of your query.
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"]." order by id asc";
Your sql query should be like given below...
$sql = "SELECT * FROM siparis_ana where grupid = " . $_GET['grupid'] . " ORDER BY id asc ";
I want to get an id from browser and display some pictures from the database.
If there is no "display2.php?productid=" found, then I want to display default image.
How can I do that?
Here is my code;
$sql = "SELECT * FROM productlist where productid=".$_GET['productid'];
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array())
{
if(null !==($_GET['productid']==$myRow["productid"])){
echo "<img src=".$myRow["productid"].">";
}
else {
echo "<img src="SELECT productimage FROM productlist where productid = 1;">";
}
}
Now I will make it easier to explain for you...
Check this out;
//This part works without any problem
$sql = "SELECT * FROM productlista where productid=".$_GET['productid'];
$result = $mysqli->query($restwo);
while($myRow = $resulttwo->fetch_array())
{
if(null !==($_GET['productid']==$myRow["productid"])){
echo "<img src=".$myRow["productimage"].">";
}
//This part below (that should be default) does not work...
if (!$_GET){
echo "hello world"; }
Asaph pointed out SQL injection. You should bind the parameter (google it), or at the minimum do this:
$defaultImage = "SELECT * FROM productlist WHERE imageSrc != '' OR IS NOT NULL ORDER BY productid DESC LIMIT 1";
// run the query, get the result, create a variable with default image...
$defaultImageSrc = ''; // what you get from the query result
$_GET['productid'] = preg_replace('#[^0-9]#', '', $_GET['productid']);
$sql = "SELECT * FROM productlist where productid=".$_GET['productid'];
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array()) {
if(!$myRow['imageSrc']) $myRow['imageSrc'] = $defaultImageSrc;
echo '<img src="'.$path.'">';
}
If you want either $_GET['productid'] or the max(productid) when $_GET['productid'] is not set, you can use a ternary to change your sql query
$productid = ! empty($_GET['productid']) ? " WHERE productid = ".(int)$_GET['productid'] : " ORDER BY productid DESC LIMIT 1";
$sql = "SELECT * FROM productlist".$productid
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array())
{
echo "<img src=".$myRow["productimage"].">";
}
so if isset($_GET['productid']) your query would be
SELECT * FROM productlist WHERE productid = (int)$_GET['productid']
but if not the default would be
SELECT * FROM productlist ORDER BY productid DESC LIMIT 1
I got a table named "Serials" with 5 comumns
Serial, Code, Name, Redeemed, Redeem_date
i am selecting some fields from that table with this query:
$query = "SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'";
$db->setQuery($query);
$db->query();
But i dont know how to pass these values in the following variables so i can use them in if statements later
$name= //retured value from column Name
$redeemed= //retured value from column Redeemed
$redeem_date= //retured value from column Redeem_date
just like this..
// Your query here..
$query = "SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'";
$db->setQuery($query);
$results = $db->query();
//fetch data and stored into variables
while($row = fetch_array($results)){
$name = $row['Name'];
$redeemed = $row['Redeemed'];
$redeem_date = $row['Redeem_date'];
}
try something like this :
<?php
$result = $db->query("SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'");
while (list($name, $redeemed, $redeem_date) = $result->fetch(PDO::FETCH_NUM)) {
// DO SOMETHING
}
?>
while ($row = $db->fetch()) {
$name= $row['name'];
$redeemed= $row['redeemed'];
$redeem_date= $row['redeem_date'];
}
this one might fetch your results and assign to vars