Jump to content

MySQL Query problem


almystersv

Recommended Posts

Hi Guys,

 

I have a page in my website that displays all orders in the database that are "Awaiting Approval". This is a query that selects all from 'productorder', 'orders' and 'product' tables where status = 'Awaiting Approval'.

 

What I would like to do is display on the homepage a notification counting how many orders there are that are "Awaiting Approval"

but this would be running a count on a query rather than on a table.

 

Is this possible??

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/90220-mysql-query-problem/
Share on other sites

The Query I posted above, Works in phpMyAdmin when entering it manually, but i am gettin this error message when running the code in php...

 

MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) FROM orders o, productorder p WHERE p.orderID = o.orderID AND p.status = 'Awa' at line 1

 

Any ideas why?!

 

PLEASE  :-\

Link to comment
https://forums.phpfreaks.com/topic/90220-mysql-query-problem/#findComment-462654
Share on other sites

Hi,

 

this is the homepage (welcome.php) that will be displaying the output, but it is calling the 'NotificationManagerMenu.php' page that has the query on it.

 

welcome.php

<?php
session_start();
if (isset($_SESSION['username']) == false){
	header("Location: login.php");
	exit();
}

require "connect.php";
?>
<!DOCTYPE HTML PUBLIC "//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>BIS Portal</title>
<link rel="stylesheet" type="text/css"
href="mystylelogin.css" />

</head>

<body>

<?php
switch ($_SESSION['type']) 
{
	case 'user':
    	include ("header.php");
	include ("NotificationMenu.php");
    	break;

	case 'manager':
    	include ("header.php");
	include ("NotificationManagerMenu.php");
    	break;
  
	case 'admin':
	include ("headerAdminMain.php");
	include ("NotificationMenu.php");
	break;
}
?>

<div id="header">Messages</div>


<div id="OrdersBasketTable">
<table border="0" width="802">
<hr />
<tr>
  		<th align="left" > </th>
  		<th align="left" > </th>
  		<th align="left" > </th>
  		<th align="left" > </th>
  		<th align="left" > </th>
</tr>
<tr>
  		<th width="3%" align="left" > </th>
	<th width="20%" align="left" ><h3><a href="welcome.php?var=msgTitle">Title</a></h3></th>
        <th width="52%" align="left" ><h3><a href="welcome.php?var=msgBody">Content</a></h3></th>
        <th width="14%" align="left" ><h3><a href="welcome.php?var=msgDate">Date & Time</a></h3></th>
        <th width="11%" align="left" ><h3><a href="welcome.php?var=username">User</a></h3></th>
</tr>
<?php
$orderby = $_GET['var'];
$query = "SELECT * FROM message, employee WHERE message.empID = employee.empID ORDER BY $orderby desc LIMIT 0, 10";
$result = mysql_query($query, $connection)
or die ("MySQL Error: ".mysql_error());
while($row = mysql_fetch_array($result))
	{ ?>
<tr>
  		<td> </td>
	<td><?php echo $row['msgTitle']?></td>
	<td><?php echo $row['msgBody']?></td>
	<td><?php echo $row['msgDate']?></td>
	<td><?php echo $row['fName']?> <?php echo $row['sName']?></td>
</tr>
<tr>
  		<td align="center"> </td>
  		<td align="center"> </td>
  		<td> </td>
  		<td> </td>
  		<td> </td>
  		<td width="0%"> </td>
</tr>
<?php } ?>
</table>
</div>

</body>
</html>

 

 

NotificationManagerMenu.php

<?php
require "connect.php";

$query2 = "SELECT COUNT (*) FROM orders o, productorder p WHERE p.orderID = o.orderID AND p.status = 'Awaiting Approval'";
$result2 = mysql_query($query2, $connection)
or die ("MySQL Error: ".mysql_error());
?>

<link rel="stylesheet" type="text/css"
href="mystylelogin.css" />

<div id="NotificationMenu"><img src="images/NotificationMenu.gif" /></div>

<div id="NotificationMenuTable">
<table width="99%" border="0">
  <tr>
    <td><div align="center"><h2><u>Notifications</u></h2></div></td>
  </tr>
  <tr>
    <td> </td>
  </tr>
  <tr>
    <td>You have... </td>
  </tr>
  <tr>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
  </tr>
  <tr>
    <td><div align="center"><a href="pendingOrders.php">View Pending Orders</a></div></td>
  </tr>
</table>
</div>

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/90220-mysql-query-problem/#findComment-462659
Share on other sites

First...change your require()s to require_once(). This allows you to keep including connect.php, but if it's already been included, it won't keep including (and reconnecting).

 

on NotificationManagerMenu.php:

<?php
require_once("connect.php");
$query2 = "SELECT COUNT (*) FROM orders o, productorder p WHERE p.orderID = o.orderID AND p.status = 'Awaiting Approval'";
$result2 = mysql_query($query2, $connection)
or die ("MySQL Error: ".mysql_error());
        $row= mysql_fetch_row($result);
        $count = $row[0];
....
?>

Link to comment
https://forums.phpfreaks.com/topic/90220-mysql-query-problem/#findComment-462855
Share on other sites

Hi,

 

Thanks.

 

I changed the code and am now gettin this error?!

 

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Build\NotificationManagerMenu.php on line 5

 

then you probably misspelled or there is an incorrect row in your query. Try adding TICKs to the Column names and Single quotes to the values. This way, if the MYSQL name is reserved, the ticks will skip that and read it as a mysql row.

Link to comment
https://forums.phpfreaks.com/topic/90220-mysql-query-problem/#findComment-462888
Share on other sites

Superb,

 

Thanks alot for your help.

 

I've got just one more question, sorry  :-\

 

Is there anyway of editing the query to count just the DISTINCT orderID's as it is duplicating many because one order has many products and it is counting them each time?!

 

Thanks, this is the last question  :)

Link to comment
https://forums.phpfreaks.com/topic/90220-mysql-query-problem/#findComment-462957
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.