How to show the result form getting array? - php

Database :
--> product table
P_id P_name P_uploadKey
1 Cemera 7365
2 Notebook 7222
3 Monitor 7355
4 Printer 7242
--> buy table
B_id P_id B_name date
1 1,3,4 somchai 12/3/2016
2 2,3 kri 12/3/2016
This sql to show the find id on buy table where $_GET['B_id'] = '2' :
$bid = $_GET['B_id'];
$sqlB ="select * from buy where B_id ='$bid' ";
$Recordset2 = mysql_query($sqlB, $connect) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
And this sql code to show the result what is they buy, by get the $row_Recordset2['P_id'] like a 2,3 from code above :
$pid = $row_Recordset2['P_id'];
$sqlp ="select * from buy where P_id ='$pid' ";
$Recordset3 = mysql_query($sqlp, $connect) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
do {
echo $row_Recordset3['P_name']. "<br>";
} while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));
I want the to show like this, how we edit it:
Notebook
Monitor

This is answer i can do it.
$pid = $row_Recordset2['P_id'];
$array = explode(',', $pid);
foreach ($array as $item) {
$sqlp ="select * from buy where P_id ='$item' ";
$Recordset3 = mysql_query($sqlp, $connect) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
do {
echo $row_Recordset3['P_name']. "<br>";
} while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));
}

You can use like below,
$sqlp ="select * from product where P_id IN '($pid)'";
instead of
$sqlp ="select * from buy where P_id ='$pid' ";

Related

use of two queries in a model of MVC

public function getotherclothdetail($id)
{
$qry1 = "SELECT rentprice from cloth_table where id = '$id' ";
$result2 = $this->fetch($qry1);
$qry = "SELECT cloth_table.*, user_table.email from cloth_table, user_table WHERE cloth_table.owner_id = user_table.id And cloth_table.price = $result2[0]['rentprice'] ";
$result = $this->fetch($qry);
return $result;
}
i want to display others cloth details from cloth_table where price is equal to selected cloth id price. help me out.

Error: Array to string conversion error

Firstly I am randomly selecting the ID from my table. That part works fine but the next part doesn't. The next part is selecting the ID's row, e.g. if the ID is 6, then it should select all the fields related to 6.
my table is like this:
------------------------------
|ID|Name|Email |Password|
------------------------------
|1 |Amy |H#gmail.com|jaaaaaaa|
------------------------------
|2 |Bob |1#gmail.com|haaukanm|
------------------------------
|3 |Bill|aa#mail.com|fsoji443|
------------------------------
This is my code:
<?php
include('connect.php');
//select a number between min id and max id
$v = "SELECT ID FROM `tblaccounts` ORDER BY RAND() LIMIT 1";
$result = mysqli_query($connection, $v);
$data2 = mysqli_fetch_array($result);
//var_dump($data2);
$c = "SELECT * FROM `tblaccounts` WHERE ID='$data2'";
$cresult = mysqli_query($connection, $c);
$data3 = mysqli_fetch_array($cresult);
var_dump($data3);
?>
The issue is here:
$c = "SELECT * FROM `tblaccounts` WHERE ID='$data2'";
here $data2 is not a single value, its an array and you are trying to compare it in WHERE like a string, that's why the error. Instead try $data2['id'] like:
$c = "SELECT * FROM `tblaccounts` WHERE ID='".$data2['ID']."'";
or
$c = "SELECT * FROM `tblaccounts` WHERE ID=".$data2['ID']; // Sinlge quote is not required if `ID` is `int`
Because your $data2 is an array, this is should work
include('connect.php');
//select a number between min id and max id
$v = "SELECT ID FROM `tblaccounts` ORDER BY RAND() LIMIT 1";
$result = mysqli_query($connection, $v);
$data2 = mysqli_fetch_array($result);
//var_dump($data2);
$c = "SELECT * FROM `tblaccounts` WHERE ID='".$data2['ID']."'";
$cresult = mysqli_query($connection, $c);
$data3 = mysqli_fetch_array($cresult);
var_dump($data3);
You are getting this error as you are comparing array in where clasue.
Your $data is an array like below
$data = array(
'ID'=>2
'Name'=>'Bob',
'Email'=>'1#gmail.com',
'Password'=>'haaukanm'
);// say record with id 2 is fecthed
So use $data['ID'] in your where clause

How combine two query in mysql which have all different column name

this is the query for search product. The data have product listing from the different two table.
$where = " Jewellery_Title like '%$id%' or Jewellery_SKU like '%$id%' and Jewellery_Status='Active'";
// $where_s = implode(' and ', $where);
$sql = "select * from jewellery where (".$where."".$jewellery_name." ) $metal_where order by rank asc";
$res1 = query($con,$sql);
$total1 = row($res1);
$where = " Ring_Title like '%$id%' or Ring_SKU like '%$id%' $ring_name
and Ring_Status='Active'";
$sql = "select * from rings where (".$where.") $metal_where order by rank asc";
$res2 = query($con,$sql);
$total2 = row($res2);
Use alias in mysql query as i mention above and run the array merge .
$sql=select jewelleryname as name from jewellery
$sql1=select ringname as name from rings
// more code
$total = row($res1);
$total1 = row($res2);
$C = array_merge($total , $total1);
if you want join query use this
select jewelleryname as name from jewellery
UNION ALL
select ringname as name from rings

Displaying based on Results in Mysql Query

I have an orders table in mysql and in that for some orders I set particular order status like 'review'.
I want to setup a way if any order placed by a particular customer(first and last name) for whom i have previously set order status as 'review' to display a warning in the list.
$sql = "select * from order where firstname = ".$firstname." AND lastname = ".$lastname." AND order_status = 'review';";
$SQLresult = mysql_query("$sql", $DBcon_MySQL);
while($row = mysql_fetch_array($SQLresult)) {
foreach($row as $row){
$result = "warning!";
echo $result;
}
}
The above code does not display anything. please let me know how to fix this.
[EDIT After Applying Answer]
This is how i am using it.
<td width="200">
<?
$sql = "select * from cust_order where firstname = '$firstname' AND lastname = '$lastname' AND order_status = 'review'";
$SQLResult = mysql_query("$sql", $DBcon_MySQL);
while($row = mysql_fetch_array($SQLResult )) {
//$result;
foreach($row as $row ){
//$result="";
$result = "Warning!";
}
?>
<p><? echo $result;?></p>
<?} ?>
</td>
How should i insert a check that it should display warning only once No matter how many orders from single customer are marked as review, display warning only once?
try this,
$sql = "SELECT
*
FROM
`order`
WHERE
firstname = '$firstname' AND lastname = '$lastname' AND
order_status = 'review' LIMIT 1";
$SQLresult = mysql_query($sql, $DBcon_MySQL);
while($row = mysql_fetch_array($SQLresult)) {
foreach($row as $row){
$result = "warning!";
echo $result;
}
}
Please be informed that mysql functions are deprecated and not recommended. USE MySQLi or PDO instead. have a reference from following queries.
http://php.net/manual/en/book.mysqli.php
http://php.net/manual/en/book.pdo.php

SQL query to return mixed tables

Hello I have the following url
http://www.test.nl/test.php?itemnr=123
In my database in table items2 my columns are:
itemnr | itemId | Description | etc
Because I want to show the itemnr AND the itemId on this page I have this query:
<?php
$itemnummer = intval($_GET['itemnr']);
$itemIDnummer = "SELECT DISTINCT itemId from items2 where itemnr = '" .$itemnummer. "'";
$resultaat = mysql_query($itemIDnummer) or die(mysql_error());
echo "$resultaat";
?>
Can anyone see my fault?
Add so that your code looks like this
<?php
$itemnummer = intval($_GET['itemnr']);
$itemIDnummer = "SELECT DISTINCT itemId from items2 where itemnr = '" .$itemnummer. "'";
$resultaat = mysql_query($itemIDnummer) or die(mysql_error());
$fetchaat = mysql_fetch_assoc($resultaat);
echo $fetchaat["itemId"];
?>
Updated Question:
Because I want to show the itemnr AND the itemId on this page I have this query:
<?php
$itemnummer = intval($_GET['itemnr']);
$itemIDnummer = "SELECT DISTINCT itemId, itemnr from items2 where itemnr = '" .$itemnummer. "'";
$resultaat = mysql_query($itemIDnummer) or die(mysql_error());
$fetchaat = mysql_fetch_assoc($resultaat);
echo $fetchaat["itemId"];
echo $fetchaat["itemnr"];
?>

Categories