0

I'll need your help with the following code.

If there is no tickets with the prio_id 5 it should be displayed "ok" and when there one or more tickets with an higher priority it should be a image displayed

<?php 
$alert = mysql_query("SELECT count(tn) AS Prio-Tickets FROM ticket where ticket_state_id = 1 AND ticket_priority_id = 5;"); 
 while ($row = mysql_fetch_object($alert))
 { 
 if ($row->Prio-Tickets = 0 ) 
 { 
 echo 'OK'; 
 } 
 else
 { 
 echo '<img src="alert.gif"/>' 
 } 
 } 
 ?>     
0

2 Answers 2

1

You're presently assigning with a single = sign instead of comparing with ==

do if ($row->Prio-Tickets == 0 )


Edit:

Refer to spencer7593's answer concerning the alias you are using.

Sign up to request clarification or add additional context in comments.

Comments

1

We'd expect the SQL statement you are running to throw Error 1064 You have an error in your SQL syntax, due to the dash character in the column alias.

But likely you aren't seeing that error, because you aren't checking whether the query was successful.

To use the dash character in an identifier, you will need to "escape" the identifier; the normal pattern in MySQL is to either to enclose the identifier in backtick characters, or to to use an identifier that doesn't require escaping.

SELECT foo AS `Prio-Tickets` ...

SELECT foo AS prio_tickets ...

It's good practice to check the return from the query, and to appropriately handle the error, rather than having your code put it's figurative pinky finger to the corner of it's mouth Dr. Evil style "I'm just gonna assume it all went to plan. What?"

Since the mysql interface is deprecated (and new development should be using either the mysqli or PDO interface), I'm not not going to provide an example using the mysql interface; there's plenty of examples already available elsewhere.

1 Comment

Ah yes, I saw that too and TBH, I wasn't entirely sure about it. You are right about using backticks around the alias or using an underscore instead of a hyphen. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.