Showing and Hiding MySQL error in PHP

You might ask why we hide an error, 'cause it should be fixed right? Anyway, there are instances that we just need to hide it. Let's say for example, "undefined index" error/warning in this case:

I have this table with unique attribute names in every "input" elements:
ID Name
1 Eds
2 Matt

We don't know how many value that the user will input, so I assume that he/she will always input eight(8) values. I must fetch the value from that table by assigning them to a variable. If the user will  input less than eight(8), a warning or error will show "undefined index..". In this case, I just hide it.

Here's how:

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="syllabus"; // Database name 
$tbl_name="form"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


Just add "error_reporting(0);" after <?php as shown above.

And to show an error, of course you need to delete that line of code or just add // before it then add this in your query or die($sql2."<br/>".mysql_error());
Just like the sample below:

$sql2="SELECT  lct,tf,lo,po,tla,at,imr FROM $tbl_name WHERE id ='" . $row['id'] . "' AND lct !=' ' AND ins_id='".$_SESSION['SESS_ID']."'";
$result2=mysql_query($sql2) or die($sql2."<br/>".mysql_error());

That's it! Don't forget to change the variables. Sorry for bad English. :P

Popular posts from this blog

Set table width to 100% on PDF export in Datatables

Echo of Soul: Huntress Quickdraw Build

GeoDjango + GeoServer + Openlayers Combo!