I have a database of order status' and I'd like to return the amount of rows for which the value of that row is anything but Complete.
How would I go about this? I have the following which shows how much rows has 'New' as the status for the order. But I'd like to show all rows except ones with the od_status of Complete.
<?php
$query = "SELECT od_status FROM tbl_order WHERE od_status = 'New'";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
echo "$num_rows";
?>
<?php
$query = "SELECT od_status FROM tbl_order WHERE od_status != 'Complete'";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
echo "$num_rows";
?>
$query = "SELECT od_status FROM tbl_order WHERE od_status <> 'Complete'";
$query = "SELECT COUNT(*) FROM tbl_order WHERE od_status != 'Complete'";
$result = mysql_query($query);
var_dump($result);
COUNT to count rows and != to select where is not equal to Complete.
Related
I want to show the count of users which have the status 1 (see code) within PHP MySQL.
<?php
// something like this
$result = mysqli_query($mysqli, "SELECT COUNT(*) FROM users WHERE status = '1'");
echo "$result";
?>
Try this:
$query = "SELECT COUNT(*) as countvar FROM users where status = '1'";
$result = mysqli_query($con,$query);
$row = mysqli_fetch_array($result);
$count= $row['countvar '];
I have two tables "BANDS" & "adres". In table "BANDS" there is a BOEKERID which is the same ID as the corresponding ID in "adres".
The bands is always different ofcourse and the BOEKER can be the same. Also because of adress change i have the 2 dfferent tables.
Now, i tried this to get the info:
$sql1 = "SELECT BANDID, NAAMBAND, CONTACTBAND, BOEKERID FROM `BANDS` ORDER BY $field $sort";
$sql2 = "SELECT ID, NAAM FROM `adres` WHERE ID = $BOEKERID";
$result1 = mysql_query($sql1) or die(mysql_error());
$result2 = mysql_query($sql2) or die(mysql_error());
while($row1 = mysql_fetch_array($result1))
while($row2 = mysql_fetch_array($result2))
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>
<td>'.$row2['NAAM'].'</td>';
Somebody can help me ?
If i use your sql :
$sql1 = "SELECT BANDID, NAAMBAND, CONTACTBAND, BOEKERID FROM `BANDS` ORDER BY $field $sort";
$result1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>';
$sql2 = "SELECT ID, NAAM FROM `adres` WHERE ID = $row1['BOEKERID']";
$result2 = mysql_query($sql2) or die(mysql_error());
while($row2 = mysql_fetch_array($result2)){//change this while if $sql2 all time return only 1 result
echo '<td>'.$row2['NAAM'].'</td>';
}
echo'</tr>';
}
With join (http://sql.sh/cours/jointures/inner-join):
$sql1 = "SELECT NAAM,BANDID, NAAMBAND, CONTACTBAND, BOEKERID FROM `BANDS` INNER JOIN `adres` WHERE `BANDS`.BOEKERID = `adres`.id ORDER BY $field $sort";
$result1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>
<td>'.$row1['NAAM'].'</td>
</tr>';
}
Try Sql join,
SELECT
b.BANDID, b.NAAMBAND, b.CONTACTBAND, b.BOEKERID, a.id, a.naam
FROM BANDS b
LEFT JOIN adres a ON b.boekerid=a.id
ORDER BY $field $sort
Also don't try to use mysql since it's deprecated, instead try switching to MySQLi or PDO
i guess you need to do something like this:
$sql1 = "SELECT a.BANDID, a.NAAMBAND, a.CONTACTBAND, a.BOEKERID, b.NAAM FROM BANDS a, adres b WHERE a.BANDID = b.ID AND ID = $BOEKERID ORDER BY $field $sort";
$result1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1))
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>
<td>'.$row1['NAAM'].'</td>';
Please correct me if im wrong
Why isnt this working?
mysql_select_db('a2943462_Pages');
$num_rows = mysql_query("SELECT COUNT(*) FROM PagesInfo");
echo $num_rows;
mysql_query("INSERT INTO `a2943462_Pages`.`PagesInfo`(`ID`, `Title`, `Video`, `Posted`) VALUES ('".$num_rows."', '".$Title."', '".$Embed."', '".$name."')");
printf("Last inserted record has id %d\n", mysql_insert_id());
Because it now echoes:
Resource id #9
but i tought it would echo 4 instead because i have 3 rows inside the Table
Change this:
$num_rows = mysql_query("SELECT COUNT(*) FROM PagesInfo");
To this:
$num_rows = mysql_result(mysql_query("SELECT COUNT(*) FROM PagesInfo"), 0);
This is the PHP method:
$sql="SELECT * FROM pages_info";
$result = mysql_query($sql, $con) or die (echo "$sql" );
$rows = mysql_num_rows($result);
you should do two queries:
first query what you want to show:
SELECT SQL_CALC_FOUND_ROWS * FROM PagesInfo
and second query to get number rows returned:
SELECT FOUND_ROWS()
The in your insert the number of rows returned
The query in php is
$rows = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM PagesInfo");
$rs = mysql_query("SELECT FOUND_ROWS()");
$r = mysql_fetch_row($rs);
mysql_free_result($rs);
$row_num = $r[0];
I have a problem with my code:
<?php
echo $s_country;
$sql2="SELECT prod_id FROM tbl_order_item WHERE order_id='$order_id'";
$res=mysql_query($sql2) or die(mysql_error());
$i=0;
$j="";
while($rs=mysql_fetch_array($res)){
$j = $rs['prod_id'];
if(trim($s_country)=='US' || trim($s_country=='United States' )){
$sql3 = "SELECT shipping_us FROM tbl_product WHERE prod_id=".$j;
$res=mysql_query($sql3) or die(mysql_error());
$shipping_cost1=mysql_fetch_object($res);
}
$i++;
}
?>
What I actually want to do is to fetch the products id from the table tbl_order_item and with that products id select the shipping cost for those ids from the table tbl_product.
For example, if there is two product ids, it should select shipping cost for both the ids.
But here it only works for one product id like this:
SELECT shipping_us FROM tbl_product WHERE prod_id=526
But what I'm trying to do is :
SELECT shipping_us FROM tbl_product WHERE prod_id=526
SELECT shipping_us FROM tbl_product WHERE prod_id=527
you could use OR, like
SELECT shipping_us FROM tbl_product WHERE prod_id=526 OR prod_id=527
you can also use it like this with IN
SELECT shipping_us FROM tbl_product WHERE prod_id IN (526,527)
First Solution:
Change
$res=mysql_query($sql3) or die(mysql_error()); // you have overwritten result set of first query
$shipping_cost1=mysql_fetch_object($res);
To
$newres=mysql_query($sql3) or die(mysql_error());
$shipping_cost1=mysql_fetch_object($newres);
Second Solution (much better):
$sql2="SELECT GROUP_CONCAT(prod_id SEPARATOR ',') as pid FROM tbl_order_item WHERE order_id='$order_id'";
$res=mysql_query($sql2) or die(mysql_error());
if(mysql_num_rows($res) > 0){
$rs=mysql_fetch_array($res);
$sql3 = "SELECT shipping_us FROM tbl_product WHERE prod_id IN (".$rs['pid'].")";
$res=mysql_query($sql3) or die(mysql_error());
while ($row = mysql_fetch_array($res)){
echo $row['shipping_us']
}
}
Recommendations:
1.Learn to prevent from MySQL Injections: Good Link
2.Mysql extension is not recommended for writing new code. Instead, either the mysqli or PDO_MySQL extension should be used. More reading: PHP Manual
my pblm was solved... and special tnx to GBD..
here is my final code...
<?php
echo $s_country;
$sql2="SELECT GROUP_CONCAT(prod_id SEPARATOR ',') as pid FROM tbl_order_item WHERE order_id='$order_id'";
$res=mysql_query($sql2) or die(mysql_error());
$shipping_cost1 = 0;
if(mysql_num_rows($res) > 0){
$rs=mysql_fetch_array($res);
if(trim($s_country)=='US' || trim($s_country=='United States' )){
$sql3 = "SELECT shipping_us FROM tbl_product WHERE prod_id IN (".$rs['pid'].")";
$res=mysql_query($sql3) or die(mysql_error());
if(mysql_num_rows($res) > 0){
while ($row = mysql_fetch_array($res)){
$shipping_cost1 = $shipping_cost1 + $row['shipping_us'];
}
}
} else {
$sql3 = "SELECT shipping_outside FROM tbl_product WHERE prod_id IN (".$rs['pid'].")";
$res=mysql_query($sql3) or die(mysql_error());
while ($row = mysql_fetch_array($res)){
$shipping_cost1= $shipping_cost1 + $row['shipping_outside'];
}
}
}
?>
I have a query that gets 5 lines of data like this example below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
}
I want to run a query inside each results like this below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
but for some reason it's only display the first line when I add the query inside it. I can seem to make it run all 5 queries.
SELECT
t1.ref,
t1.user,
t1.id,
t2.domain,
t2.title
FROM
table AS t1
LEFT JOIN anothertable AS t2 ON
t2.domain = t1.ref
LIMIT
0, 5
The problem is that inside the while-cycle you use the same variable $result, which then gets overridden. Use another variable name for the $result in the while cycle.
You change the value of your $query in your while loop.
Change the variable name to something different.
Ex:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$qry = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$rslt = mysql_query($qry) or die(mysql_error());
if (mysql_num_rows($rslt) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
Use the following :
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query_domain = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result_domain = mysql_query($query_domain) or die(mysql_error());
if (mysql_num_rows($result_domain) )
{
$row_domain = mysql_fetch_row($result_domain);
$title = $row_domain['title'];
} else {
$title = "No Title";
}
echo "$ref - $title";
}
This is a logical problem. It happens that way, because you are same variable names outside and inside the loop.
Explanation:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
// Now $results hold the result of the first query
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
//Using same $query does not affect that much
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
//But here you are overriding the previous result set of first query with a new result set
$result = mysql_query($query) or die(mysql_error());
//^ Due to this, next time the loop continues, the $result on whose basis it would loop will already be modified
//..............
Solution 1:
Avoid using same variable names for inner result set
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$sub_result = mysql_query($query) or die(mysql_error());
// ^ Change this variable so that it does not overrides previous result set
Solution 2:
Avoid the double query situation. Use joins to get the data in one query call. (Note: You should always try to optimize your query so that you will minimize the number of your queries on the server.)
SELECT
ref,user,id
FROM
table t
INNER JOIN
anothertable t2 on t.ref t2.domain
LIMIT 0, 5
Learn about SQL joins:
SELECT table.ref, table.user, table.id, anothertable.title
FROM table LEFT JOIN anothertable ON anothertable.domain = table.ref
LIMIT 5
You're changing the value of $result in your loop. Change your second query to use a different variable.
it is not give proper result because you have used same name twice, use different name like this edit.
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query1 = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result1 = mysql_query($query1) or die(mysql_error());
if (mysql_num_rows($result1) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}