11 Replies - 383 Views - Last Post: 06 May 2013 - 12:44 PM Rate Topic: -----

#1 Nana Nyarko Abronomah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 03-November 10

Can't print when I use "or die"

Posted 05 May 2013 - 02:32 PM

I have a php script and with mysql. When I add the 'or die' part to my while loop, the code outside the while loop can't print. but if i take away the 'or die' part, everything prints fine. In both situations, no error message pops up anyway.

But If I eliminate the 'or die' part, i get an error when i upload it to my web server. Below is the code i'm using. Thnx in advance;


$Total=0.00;
while($row=mysql_fetch_assoc($res) or die()) {
	$Total1 = $row["TotalCost"];
	$Total = $Total + $Total1;
	print " 
		<tr>
			<td>$row[Product]</td>
			<td align=right>$row[Price]</td>
			<td align=right>$row[Quantity]</td>
			<td align=right>$row[UnitQty]</td>
			<td align=right>$row[Discount]</td>
			<td align=right>$row[TotalCost]</td>
		</tr>";
	}
		mysql_close(mycon);
		Print"
		<tr>
							<td><b>Total</b></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td align=right><b>$Total</b></td>
					
				
				</table>
					</center>
					</div>";
					
			}
			print $Total;
			print "fsdf";


This post has been edited by macosxnerd101: 05 May 2013 - 02:43 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Can't print when I use "or die"

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Can't print when I use "or die"

Posted 05 May 2013 - 02:45 PM

Generally, you use the or die() addendum with the mysql_query() function, and you pass the die() function the result of the mysql_error() function.

Just an FYI, though- The mysql_*() family of functions is deprecated. You really should be using prepared statements like PDO or MySQLi.
Was This Post Helpful? 0
  • +
  • -

#3 Nana Nyarko Abronomah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 03-November 10

Re: Can't print when I use "or die"

Posted 05 May 2013 - 02:59 PM

am sorry but I cant really get what you are saying
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,232
  • Joined: 08-June 10

Re: Can't print when I use "or die"

Posted 05 May 2013 - 03:24 PM

thatís quite simple. mysql_fetch_assoc() returns false if there are no more rows to fetch, mysql_query() returns false if there is an error in the SQL statement. hence, for one of them it makes sense to die(), for the other not.
Was This Post Helpful? 0
  • +
  • -

#5 Nana Nyarko Abronomah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 03-November 10

Re: Can't print when I use "or die"

Posted 05 May 2013 - 03:36 PM

So how do I get around this
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,232
  • Joined: 08-June 10

Re: Can't print when I use "or die"

Posted 05 May 2013 - 03:42 PM

donít use or die() in the wrong place.
Was This Post Helpful? 0
  • +
  • -

#7 Nana Nyarko Abronomah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 03-November 10

Re: Can't print when I use "or die"

Posted 05 May 2013 - 03:56 PM

thanks sir but where is the right place to put it
Was This Post Helpful? -1
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Can't print when I use "or die"

Posted 05 May 2013 - 04:03 PM

Are you even reading our posts? You've been told twice now where to use it.
Was This Post Helpful? 0
  • +
  • -

#9 Nana Nyarko Abronomah  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 03-November 10

Re: Can't print when I use "or die"

Posted 05 May 2013 - 04:09 PM

sorry I just wanted to be sure it was at the mysql_query()
Was This Post Helpful? 0
  • +
  • -

#10 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Can't print when I use "or die"

Posted 06 May 2013 - 05:59 AM

When the thing before "or die" is false, the script will halt completely and stop. So when. You die before anything gets outputted, youll get a blank screen.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,788
  • Joined: 12-December 12

Re: Can't print when I use "or die"

Posted 06 May 2013 - 06:35 AM

The very first example in THE DOCS uses die() with mysql_query.
Was This Post Helpful? 0
  • +
  • -

#12 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Can't print when I use "or die"

Posted 06 May 2013 - 12:44 PM

It's also worth mentioning that or die(mysql_error()) is a very poor way to handle errors. It simply dumps the error onto the screen, in full view of whatever user happens to be requesting the page. Not only does this look extremely bad for you, but it potentially gives out information about the database structure that random users should in no way be given.

A better way is to set up proper error logging (which may include showing errors on-screen on dev servers) and then use functions like trigger_error() or error_log() to log/display the error.
// It could simply be:
$result = mysql_query($sql) or trigger_error("Query failed: " . mysql_error(), E_USER_ERROR);

// Or you could print out a user message first, and then trigger the error:
$result = mysql_query($sql);
if (!$result) {
    echo "<h1>Error!</h1><p>An error has occurred. Please try again!</p>";
    trigger_error("Query failed: " . mysql_error(), E_USER_ERROR());
}


In both cases the script execution will be halted and the error logged. The great thing there is that on development servers this will actually print the error to the screen, but on servers configured for production use, the errors won't show up on screen, only in the logs.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1