Count same name items in mysql table php? - php

How can i count items qho had the same name in a mysql table with php , i tried this but he always print the only first one but i have 5items with the same Custumer name.. this my code..
<?php
include ('Connection.php');
$con= mysqli_connect($host,$user,$pass,$db);
if($_SERVER['REQUEST_METHOD']=='GET'){
$Codigo=$_GET['nbCliente'];
$sql = "
SELECT $Codigo
, COUNT(*)
FROM pedidos
GROUP
BY nbCliente = '$Codigo'
";
$r = mysqli_query($con,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"codigo"=>$res['Codigo'],
"articulos"=>$res['articulos'],
"precio"=>$res['precio'],
"cantidad"=>$res['cantidad'],
"Nomb_Cliente"=>$res['nbCliente'],
"NombVende"=>$res['nbvendedor'],
"image"=>$res['image_path']
)
);
echo json_encode(array("result"=>$result));
mysqli_close($con);
}

I think the query you are looking for should look like this:
select nbCliente, count(nbCliente) from pedidos where nbCliente = $Codigo group by nbCliente

Related

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1

I'm trying to save an id number in my database and I can't seem to store an integer inside the $resultid variable.
Here is my code:
<?php
if (isset($_POST['tittel'])) {
$meny = $_POST['meny'];
$tittel = $_POST['tittel'];
$innhold = $_POST['innhold'];
$con = mysqli_connect('localhost', 'root', '', 'vikerfjell');
if ($con) {
echo "Connected!";
"<br>";
} else {
die("Connection failed!");
}
$menyid = ("SELECT idmeny FROM meny WHERE tekst = '$meny'");
$resultid = mysqli_query($con, $menyid);
$resultarr = mysqli_fetch_assoc($resultid);
$query = "INSERT INTO innhold (tittel, tekst, idmeny) ";
$query. = "VALUES('$tittel', '$innhold', $resultarr)";
$result = mysqli_query($con, $query);
"<br>";
if (!$result) {
die('Query FAILED!'.mysqli_error($con));
}
}
mysqli_fetch_assoc- fetch a result row as an associative array, u need to select the key of the id u are tying to store
$menyid = ("SELECT idmeny FROM meny WHERE tekst = '$meny'");
$resultid = mysqli_query($con, $menyid);
$resultarr = mysqli_fetch_assoc($resultid);
$column_id=$resultarr['id_stored_on_db'];
$query = "INSERT INTO innhold (tittel, tekst, idmeny) ";
$query .="VALUES('$tittel', '$innhold', $column_id)";
Guessing $resultarr holds multiple idmeny, so you really have to loop through each idmeny and create multiple insert statements, as such:
$menyid = "SELECT idmeny FROM meny WHERE tekst = '$meny'";
$result = mysqli_query($con, $menyid);
while ($row = $result->fetch_assoc()) {
$query = "INSERT INTO innhold (tittel, tekst, idmeny) ";
$query .= "VALUES('$tittel', '$innhold', {$row['idmeny']})";
$result = mysqli_query($con, $query);
}
Found the problem. I missed a value in my dropdown code (another php file). Thanks anyway : )

Need Some Help Regarding Fetching Data from Mysql using explode function

In the below script I want to fetch data from mysql using a explode function and also a variable within an explode function.
Here's how I want to get
<?php
include ('config.php');
$track = "1,2,3";
$i = 1
$trackcount = explode(",",$track);
$sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";
$retval = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['track_name']}";
}
mysql_free_result($retval);
?>
This is the code
$sql = "SELECT * FROM tracks WHERE id='$trackcount[$i]'";
I want sql to fetch data from tracks table where id = $trackcount[$i]
Whatever the value of $trackcount[$i] mysql should fetch but it shows a blank screen.
If I put this
$sql = "SELECT * FROM tracks WHERE id='$trackcount[1]'";
It works perfectly
save your $trackcount[$i] in one variable and then pass it in the query as given below
<?php
include ('config.php');
$track = "1,2,3";
$i = 1;
$trackcount = explode(",",$track);
$id=$trackcount[$i];
$sql = "SELECT * FROM tracks WHERE id='$id'";
$retval = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['track_name']}";
}
mysql_free_result($retval);
?>
and one more thing check your previous code with echo of your query and see what is passing ok.
echo $sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";//like this
problem is with your query
$sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";//change
to
$sql = "SELECT * FROM tracks WHERE id='$trackcount[$i]'";
Generally you would want to use the IN operator with this type of query, so for you this would be:-
$sql="SELECT * FROM `tracks` WHERE `id` in (".$track.");";
or, if the $ids are in an array,
$sql="SELECT * FROM `tracks` WHERE `id` in (".implode( ',', $array ) .");";

How to skip duplicate value while fetching from My sql DB

I have one problem.I am fetching some data from MYSQL table.But there are some duplicate datas. I need to skip those duplicate data.I am explaining my code below.
session_start();
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$colg_id=1;
$dept_id = $_SESSION["admin_dept_id"];
$user_id=$_SESSION["admin_id"];
$connect = mysqli_connect("localhost", "root", "******", "go_fasto");
$result = mysqli_query($connect, "select plan_id,unit_name from db_unit_plan where dept_id='".$dept_id."' and user_id = '".$user_id."' ");
while ($row =mysqli_fetch_assoc($result)) {
$data[] = $row;
}
print json_encode($data);
Here i need if any unit_name column has same type data then how to skip those rows.Please help me.
Change like this with DISTINCT
$result = mysqli_query($connect, "select DISTINCT unit_name,plan_id from db_unit_plan where dept_id='".$dept_id."' and user_id = '".$user_id."' ");
you have to specify the 'DISTINCT' keyword to get unique results from SQL.
so just try changing your select statement to $result = mysqli_query($connect, "select DISTINCT plan_id .. ");
good luck.

MySQL: How to use implode array in WHERE IN clause of MySQL?

I'm having a problem when I try to use implode array in WHERE IN clause and display it. Some of the record in the database has an apostrophe in it.
Example the data in my database are testing,test'ing,tes't. And my code below is how I implode the data to be used in WHERE IN clause.
<?php
$arr = array();
$qry = mysqli_query($con,"SELECT sampleTxt FROM Table")or die(mysqli_error($con));
while(list($txt) = mysqli_fetch_row($qry)){
$t = mysqli_real_escape_string($con,$txt);
$arr[] = "'".$t."'";
}
$sampl = implode(',',$arr);
?>
And here is my sample code on how I used it on WHERE IN clause.
<?php
$qry2 = mysqli_query($con,"SELECT sampleTxt2 FROM Table2 WHERE sampleTxt IN (".$sampl.")")or die(mysqli_error($con));
while(list($txt2) = mysqli_fetch_row($qry2)){
echo $txt2;
}
?>
The output should be
testingtest'ingtes't
but instead the output is just the testing.
Check for below code
<?php
$arr = array();
$qry = mysqli_query($con,"SELECT sampleTxt FROM Table")or die(mysqli_error($con));
while(list($txt) = mysqli_fetch_row($qry)){
//$t = mysqli_real_escape_string($con,$txt);
$arr[] = '"'.$txt.'"';
}
$sampl = implode(',',$arr);
?>
And then
<?php
$qry2 = mysqli_query($con,'SELECT sampleTxt2 FROM Table2 WHERE sampleTxt IN ('.$sampl.')')or die(mysqli_error($con));
while(list($txt2) = mysqli_fetch_row($qry2)){
echo $txt2;
}
?>

Clear SQL table before importing data (php)

Hi guys as the title says i'm trying to clear My SQL table before importing new data , but the problem only the last two rows appears not all of them .
I tried this using "TRUNCATE"
// clear table
$sqli = "TRUNCATE TABLE MyTable";
mysql_query($sqli);
$sql = 'INSERT INTO MyTable VALUES("'.$Data1.'","'.$Data2.'") ';
mysql_query($sql);
mysql_close();
$sql = 'SELECT * FROM MyTable';
$req = mysql_query($sql);
while($data = mysql_fetch_array($req)){
$D1 = $data['data1'];
$D2 = $data['data2'];
echo "$D1<br />
$D2<br />";
}
i can see only the last two rows.
this is full code
part 1 : adding data into MySQL table
<?php
include( "............" );
$table = mta::getInput();
$Kills = $table[0];
$Deaths = $table[1];
// Send infos in MySQL
$base = mysql_connect ('........', '.........', '........');
mysql_select_db ('.........', $base);
$sql = 'INSERT INTO AccountsData VALUES("'.$Kills.'","'.$Deaths.'") ';
mysql_query($sql);
mysql_close();
// Return true if is added
// an other code here
?>
part 2 : get data from Mysql Table
<?php
$base = mysql_connect ('.........', '.........', '.........');
mysql_select_db ('..........', $base);
$sql = 'SELECT * FROM AccountsData';
$req = mysql_query($sql);
while($data = mysql_fetch_array($req)){
$Kills = $data['Kills'];
$Deaths = $data['Deaths'];
echo "$Kills<br />
$Deaths<br />";
}
mysql_close();
?>
before using "TRUNCATE TABLE" it's working fine and i can see all rows
like this:
example
player 1
player 2
player 3
player 4
after using "TRUNCATE TABLE" i can see only the last row
player 4.
All what i need is i want to clear the table before adding new rows.
Sorry it's the first time that i use https://stackoverflow.com/

Categories