marjun1979 wrote:But, what made me puzzled was the Author of some php video tutorial used the code exactly how I specified above and it worked for him. But, I guess, he might have used earlier version.
Nope. That's always been true. Variables never just magically acquire values, you have to assign them. He must have assigned a value to $name earlier in his script, which he didn't show.
I have one more question. How do you debug php code if something goes wrong. I am using PHP Coder. But I get only blank page in browser if something wrong in the code. I know, HTML page will be blank if php can't be executed. But, How do you guys handle that?
This is always a problem with PHP, due to the way it is interpreted by the web server. You're depending on the web server to find errors, but it's not set up to issue much in the way of debugging info. If it can't interpret the PHP, it merely stops interpreting it, and then if there's no more plain HTML, there's nothing to send to the browser. You can turn on PHP's error reporting temporarily, but this won't always give you usable errors, either. See
http://us.php.net/error_reporting.
We adopt defensive programming techniques, such as echoing (printing on the screen) key variables, when and where we have problems. These debugging lines can be removed later, when everything is running correctly. It helps, also, to avoid writing a lengthy and complicated script all at once, before testing a greatly simplified version, with "stubs" for a lot of the content. If the simplified version runs, then as you add more logic, you test again, so that you realize when it doesn't run, that you have just added something that's wrong. I sometimes do the reverse, too: when a lengthy script fails, I start commenting out whole blocks of code, until it works again, then I can home in on which block of code is faulty.
It also helps to use "best practice" coding and formatting techniques, which make it easier to spot silly mistakes--we all make 'em! Here's a bad and a good example of what I mean. Bad:
Code: Select all
$result=mysql_query("select ordernum,orderdate,ordtotal,name,address,city,state,zip,phone from customers as c,orders as o where o.custid=c.custid and custid=$id order by state,city");
Better:
Code: Select all
$sql="SELECT ordernum, orderdate, ordtotal, name, address, city, state, zip, phone ";
$sql.="FROM customers AS c, orders AS o ";
$sql.="WHERE o.custid=c.custid AND custid=$id ";
$sql.="ORDER BY state, city";
echo "SQL = $sql"; // remove this line after debugging!
$result=mysql_query($sql);
Now you can see if your SQL query is being formed the way you expected it to, for example, does it have a value for the $id parameter?