Return values from mysql_query
Return values from mysql_query
am 23.03.2006 12:26:32 von David Killen
I'm just beginning with PHP and came across something which is
frustrating me no end. I have a really simple script which given a
username will query the database and if it doesn't find a match will
then insert into the database. The problem I find is that even when
there is no data in the database to match the return from mysql_query
evaluates to true even though all the documentation and tutorials that I
have read say that mysql_query should return false when using a SELECT
query if there is no match. Am I missing something really simple here or
is there another explaination as to why this might be the case? The
following code snippet exhibits this behaviour. I am running the
following: Apache 2.0.54, PHP 5.0.5-2, MySQL 4.1.12.
$username = "david";
$conn = mysql_connect('localhost','user','pass');
mysql_select_db('test',$conn);
$sql = "SELECT * FROM users WHERE username='$username'";
echo $sql."
";
$result = mysql_query($sql,$conn);
echo $result."
";
if (!$result)
echo "No Match!
";
else
{
$row = mysql_fetch_array($result);
while ($row)
{
echo $row['username'].' -- '.$row['email'].'
';
$row = mysql_fetch_array($result);
}
}
mysql_close();
?>
Cheers,
David
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Return values from mysql_query
am 23.03.2006 12:35:19 von joao
David Killen wrote:
> I'm just beginning with PHP and came across something which is
> frustrating me no end. I have a really simple script which given a
> username will query the database and if it doesn't find a match will
> then insert into the database. The problem I find is that even when
> there is no data in the database to match the return from mysql_query
> evaluates to true even though all the documentation and tutorials that I
> have read say that mysql_query should return false when using a SELECT
> query if there is no match. Am I missing something really simple here or
> is there another explaination as to why this might be the case? The
> following code snippet exhibits this behaviour. I am running the
> following: Apache 2.0.54, PHP 5.0.5-2, MySQL 4.1.12.
>
>
> $username = "david";
> $conn = mysql_connect('localhost','user','pass');
> mysql_select_db('test',$conn);
> $sql = "SELECT * FROM users WHERE username='$username'";
> echo $sql."
";
> $result = mysql_query($sql,$conn);
> echo $result."
";
> if (!$result)
> echo "No Match!
";
> else
> {
> $row = mysql_fetch_array($result);
> while ($row)
> {
> echo $row['username'].' -- '.$row['email'].'
';
> $row = mysql_fetch_array($result);
> }
> }
> mysql_close();
> ?>
>
> Cheers,
>
> David
The function mysql_query just returns false when it get an error in that
query. When do you wanto to know if is there some match, you ought to use
mysql_num_rows($result). It'll return the number of lines was found by your
query.
Hope helping.
--
---------------------------------------------------
João Cândido de Souza Neto
Web Developer
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
MySQL Query Help
am 23.03.2006 13:32:43 von Mark Dyer
Hello, I'm unsure how to write the follow query, Please can someone assist.
I am writing in short hand the basis of the two queries I would like to
combine. The object is to select all the products that have not meet the min
sale requirements so I can send myself a reminder email.
The first table products contains the product information and how ofter the
stores must purchase and at what period ie, Weekly or Monthly by product.
First Query gives me all the products that must check to see if they have
meet the min sales.
Select store_id,product_id, min_level, reoirder_period from products db
where monthly_order_required > 0
Second Query is then run to test to see if they have meet the min sales.
Php: If reorder period = weekly then backdate = 7 days elseif period =
monthly then backdate = 30
Select sum(sale_product_qty) as sale_period_total from sales db where store
= store_id and product = product_id and date <= now and date > backdate and
sale_period_total > min_level
Result: sum of sales in the last week or month period for that product from
that store. If no result then sale level ok. If result then the difference
from min_level is what is require for the store to make asap.
Php: if sale_period_total < min_level then email low order email.
Any assistance to combine to the queries instead of hundreds of individual
would be grateful.
Regards
Mark Dyer
NZ
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: MySQL Query Help
am 23.03.2006 13:51:50 von joao
Mark Dyer wrote:
>
> Hello, I'm unsure how to write the follow query, Please can someone
> assist.
>
> I am writing in short hand the basis of the two queries I would like to
> combine. The object is to select all the products that have not meet the
> min sale requirements so I can send myself a reminder email.
>
> The first table products contains the product information and how ofter
> the stores must purchase and at what period ie, Weekly or Monthly by
> product.
>
> First Query gives me all the products that must check to see if they have
> meet the min sales.
>
> Select store_id,product_id, min_level, reoirder_period from products db
> where monthly_order_required > 0
>
>
> Second Query is then run to test to see if they have meet the min sales.
>
> Php: If reorder period = weekly then backdate = 7 days elseif period =
> monthly then backdate = 30
>
> Select sum(sale_product_qty) as sale_period_total from sales db where
> store = store_id and product = product_id and date <= now and date >
> backdate and sale_period_total > min_level
>
>
> Result: sum of sales in the last week or month period for that product
> from that store. If no result then sale level ok. If result then the
> difference from min_level is what is require for the store to make asap.
>
> Php: if sale_period_total < min_level then email low order email.
>
>
> Any assistance to combine to the queries instead of hundreds of individual
> would be grateful.
>
> Regards
> Mark Dyer
> NZ
Please, could you putting your real code here to us?
--
---------------------------------------------------
João Cândido de Souza Neto
Web Developer
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php