Welcome! Log In Create A New Profile

Advanced

error checking

Posted by Mac 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar
Mac
error checking
March 14, 2013 08:12AM
Probably the best tip I can give you, if you are not doing it already, is to allows echo out stuff as a means of error checking/finding.

E.g.
- passing variables: catch and echo it out immediately, or use a GET method (easier to check what is send in the URL) - you can always change back to POST.
- sql queries - use "or die()" after the query - that will provide more information on errors; also echo the query out

Language: PHP
$mail=mysql_query("SELECT * from course_student WHERE fmark is NULL"); echo $mail; //this is a test $nummail = mysql_num_rows($mail); echo $nummail; //this is a test - if you know there is a row and this shows no row, then you know your query is wrong

I can probably add many more - just always echo out
Don't try to understand the code here below (written for a recent app). Where echo is commented out, is where I did some error checking just to make sure I have everything I need to work with

Language: PHP
$sel=$_POST[';sel';]; $count = count($sel);   //echo $count;   for ($ar = 0; $ar < $count; $ar++) {   $getit= "$sel[$ar]"; //assign it a name $rest = substr("$getit", 8, -1); //substring it to remove stuff $pieces = explode("/", $getit); //split the remaining $fid=$pieces[0]; // piece1 the fixt id $ref_id=$pieces[1]; // piece2 the ref id   //echo "$sel $count $ref_id -> $fid<br>";
Re: error checking
March 14, 2013 10:20AM
thank you Mac.

What i normally do is echo my values before sending them to a database, just so i know i am sending correct values.
avatar Re: error checking
March 14, 2013 10:31AM
Have been using this for a while in programming, makes it so easy to check for errors where the code is working fine but the wrong values get send to the database. Just getting stuck on getting the data from the database to display nice in tables. sad smiley
avatar
Mac
Re: error checking
March 14, 2013 04:02PM
Language: PHP
// here is some sample code showing the trick on how to get the information into a table <?php include (';config.php';); //connection details to the database in a connect.php page echo "<table border=';1';><tr bgcolor=';#cccccc';><td>Book title</td><td>Authorname</td><td>Author surname</td><td>Price</td><td colspan=';2';>Action</td></tr>"; //put the table header column outside the loop ……//query statement here while($row = mysql_fetch_array($result)){ // the loop part of your query ... get info from db e.g, title, auth_name etc. echo "<tr><td>"; //here is the trick - > put the table rows inside the loop, and draw the information from the db into a table row, one at a time, which you can do because cause it is a while (loop) echo $row[';title';]; echo "</td><td>"; echo $row[';auth_name';]; echo "</td><td>"; echo $row[';auth_surname';]; echo "</td><td>"; echo $row[';price';]; echo "</td><td>"; echo "<a href=edit.php?book_id=$book_id&update=yes>Edit</a>"; echo "</td><td>"; echo "<a href=delete.php?book_id=$book_id&delete=yes>Delete</a>"; echo "</tr>"; } echo "</table>"; //end the table outside the loop
avatar Re: error checking
March 14, 2013 04:31PM
AWESOME!! Thanks Mac!!
Re: error checking
March 19, 2013 10:09PM
This seems to be dificult but i will try to master it becouse i will give it all ma attention tomorow
Re: error checking
March 21, 2013 01:18PM
I think it is best to build a separate functions file for error checking (call it: example: errorcheck.php) where you can randomly call functions in your code to validate the code for errors. With time you can build a pretty decent error-check-functions file that you can keep and use to test future code that you may write.

Also I have seen if you place almost most of your code in While () loops you can bypass most errors or problems, because it first needs to be validated by a While Loop or by a Do While Loop after the first statement before it can proceed.

Student Number: 78042879 (Part Time Courses)
avatar Re: error checking
March 22, 2013 10:56PM
Thank you for this. thumbs up
Re: error checking
March 23, 2013 04:53AM
thanks, it helps
Re: error checking
March 26, 2013 04:55AM
I love this way of doing things, I feel it also helps to document your code so if someone else works on the code they can see what you were trying to achieve.

Glen
avatar
Mac
Re: error checking
March 26, 2013 07:37AM
Yes, adding comments to your code is very useful, not only for your own benefit but in case someone needs to take over.
Re: error checking
March 26, 2013 08:43AM
Hi Mac

My understanding is there is no real difference between 'print' and 'echo'. Is this correct?

Why are there two ways of printing to the screen?
avatar Re: error checking
March 26, 2013 11:32AM
Print only takes one parameter, while echo can have multiple parameters, try out:
Language: PHP
echo(';one';, ';two';); print(';one';, ';two';);
Which one gives an error? Print is best for evaluating the outcome of an output statement.


Print returns a value (1), so can be used as a function.


Echo is slightly faster. There is a difference between the two speed-wise. echo is marginally faster since it doesn't set a return value.


77928490 Wrote:
-------------------------------------------------------
> Hi Mac
>
> My understanding is there is no real difference
> between 'print' and 'echo'. Is this correct?
>
> Why are there two ways of printing to the screen?

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: error checking
March 26, 2013 02:46PM
I use TextWrangler on macbook and it's fascinating how it helps to detect errors using certain color codes. Has anyone noticed that as yet?

I wonder if the same error detecting help applies to other text editors.

78026962 afojonny
Re: error checking
March 26, 2013 04:31PM
Just downloaded Notepad ++

Any comments?
Re: error checking
March 26, 2013 04:45PM
Yeah, I guess that's cool too.
Re: error checking
March 26, 2013 04:48PM
I use Notepad++ for PHP and (CSS allot), but recently changed to Sublime Text, because it also color codes everything for you and it is easier to spot mistakes or syntax errors.

Student Number: 78042879 (Part Time Courses)
Re: error checking
April 02, 2013 10:50AM
Mac or anybody out there, please help with the script below. Is it a valid PHP script or a mix of PHP and Javascript. I see it as PHP but considering that the '<script>' tags have been used I don't know . I am trying to use it for validating user input in my application. Thanks

Extract of the code:

$message="Beware of the ides of march";

print("<script> alert('$message'winking smiley </script>"winking smiley;
avatar
Mac
Re: error checking
April 02, 2013 10:59AM
Last line is Javascript, using PHP to echo it.
Re: error checking
April 02, 2013 11:02AM
Hi Mac, i have a problem with Edit page...i manage to retrieve data from the Database but when i display it on the form it shows the whole values on the column instead of one value as it intended to, is it because of my course_id doent catch the specific row or something else............the data that you select from the drop down list it displays right when i put on the Edit page but when i comes to text, it displays the whole values on the text box
Re: error checking
April 02, 2013 11:33AM
Possibly the column values are in an array and you need to reference the array position for your value, i.e. row[1], etc.
Re: error checking
April 02, 2013 11:53AM
Thanks, i will try that
Re: error checking
April 02, 2013 05:55PM
Thanks for the advice Mac.
Re: error checking
April 10, 2013 02:49PM
Thanks manage to sort it out
Re: error checking
May 01, 2013 12:14AM
What could be so wrong with this code, i get error:Notice: Undefined variable: movie_details

Language: PHP
<?php $link = mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("moviesite") or die (mysql_error());   $query = "SELECT movie_name, movie_director, movie_leadactor " . "FROM movie";   $result = mysql_query($query, $link) or die(mysql_error()); $num_movies = mysql_num_rows($result);   $movie_header=<<<EOD <h2><center>Movie Review Database</center></h2> <table width="70%" border="1" cellpadding="2" cellspacing="2" align="center"> <tr> <th>Movie Title</th> <th>Movie Director</th> <th>Movie Lead Actor</th> </tr>   EOD;   function get_director() { global $movie_director; global $director;   $query_d = "SELECT people_fullname " . "FROM people " . "WHERE people_id=';$movie_director';"; $results_d = mysql_query($query_d) or die(mysql_error()); $row_d = mysql_fetch_array($results_d); extract($row_d); $director = $people_fullname; }   function get_leadactor() { global $movie_leadactor; global $leadactor;   $query_a = "SELECT people_fullname " . "FROM people " . "WHERE people_id=';$movie_leadactor';"; $results_a = mysql_query($query_a) or die(mysql_error()); $row_a = mysql_fetch_array($results_a); extract($row_a); $leadactor = $people_fullname; }   while ($row = mysql_fetch_array($result)) { $movie_name = $row[';movie_name';]; $movie_director = $row[';movie_director';]; $movie_leadactor = $row[';movie_leadactor';];   //get director';s name from people table get_director();   //get lead actor';s name from people table get_leadactor();   $movie_details .=<<<EOD <tr> <td>$movie_name</td> <td>$director</td> <td>$leadactor</td> </tr> EOD; }   $movie_details .=<<<EOD <tr> <td>Total :$num_movies Movies</td> </tr> EOD;   $movie_footer ="</table>";   $movie =<<<MOVIE $movie_header $movie_details $movie_footer MOVIE;   echo "There are $num_movies movies in our database"; echo $movie; ?>
avatar Re: error checking
May 01, 2013 05:25PM
Your variable movie_details in undefined.

Does your error message tell you which line the error is on?

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: error checking
May 02, 2013 01:33AM
Thanks, i managed to sort it out. This was missing "$movie_details =<<<EOD" just before While statement
Sorry, only registered users may post in this forum.

Click here to login