Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Probs with example code 2

Status
Not open for further replies.

sazebac

Technical User
Joined
Apr 16, 2003
Messages
117
Location
US
I'm trying to figure out how some code works. This is from the book "Web
Database Applications with PHP and MySQL".
The books says "If the region name and description are both not empty, an
INSERT SQL statement is prepared to insert the new region,"
From the looks of "if (empty($regionName) || empty($description)) it seems
that it is the opposite. It looks like "if $regionname or $description are
empty, then....."
Next question. How does the PHP code know that regionName and description in
the Javascript code are $regionName and $description in the PHP code?
Also having problems following the order of the code. What it appears to be
saying is if $regionName and $description are empty then run the <form>,
else if no connection to the database can be established kill everything,
else if could not connect to the DB connection, run showerror() , otherwise
run the insertQuery statement, then not sure what
if ((@ mysql_query ($insertQuery,
$connection))
&& @ mysql_affected_rows() == 1)
echo &quot;<h3>Region successfully inserted</h3>&quot;;
else
showerror();

is doing.
What does the && represent. Could not find it in the php manual. Thanks in
advance for any help.




<html>
<head>
<title>Insert a Region</title>
</head>
<body>
<?php
include 'db.inc';
include 'error.inc';

// Test for user input
if (empty($regionName) || empty($description))
{
?>
<form method=&quot;GET&quot; action=&quot;example.6-1.php&quot;>
Region_name:
<br><input type=&quot;text&quot; name=&quot;regionName&quot; size=80>
<br>Description:
<br><textarea name=&quot;description&quot; rows=4 cols=80></textarea>
<br><input type=&quot;submit&quot;>
</form>
<?php
}
else
{
if (!($connection = @ mysql_connect($hostName,
$username,
$password)))
die(&quot;Could not connect to database&quot;);

if (!mysql_select_db($databaseName, $connection))
showerror();

$insertQuery = &quot;INSERT INTO region VALUES (NULL, &quot; .
&quot;\&quot;&quot; . $regionName . &quot;\&quot;, &quot; .
&quot;\&quot;&quot; . $description . &quot;\&quot;, &quot; .
&quot;NULL)&quot;;

if ((@ mysql_query ($insertQuery,
$connection))
&& @ mysql_affected_rows() == 1)
echo &quot;<h3>Region successfully inserted</h3>&quot;;
else
showerror();
} // if else empty()
?>
</body>
</html>



 
First things first...

&& is the logical AND, just like || was the logical OR.

The paragraphs are correctly describing the code...

not (A OR B) is the same thing as (not A) AND (not B)

So what they've said in english is
Code:
if (A AND B) then C.

What they've done in code is

if (A OR B) then
  D
else
  C
end if

As far as the variable thing... they're cheating in my opinion ;) Actually, what they're doing is basing it off an older version of PHP... with a new installation set to defaults that code probably won't work. (And those are HTML variables, not javascript by the way).

What used to happen (and still does it you turn on the setting in your INI file) is form variables were automatically inserted into their equivalent PHP variable names... by default they are now contained in super global arrays $_GET and $_POST...

so to make the code work you should either put something like...
Code:
$regionName = $_GET[&quot;regionName&quot;];
$description = $_GET[&quot;description&quot;];

at the top of your code... or replace all occurences of those form variables with
$_GET[&quot;....&quot;] when they appear in your PHP code.

-Rob
(please ask for clarification if you like)
 
No, it's saying, regionName empty.

The difference is that what they're telling you to do is in the ELSE statement, not immediately following the conditional...

as another example without the ORS...

in pseudo-english

IF it is raining THEN get umbrella ELSE do not get umbrella

is equally represented in the following code
Code:
IF (it is raining) {
  get umbrella
} else {
  do not get umbrella
}

and this code

IF (it is not raining) {
  do not get umbrella
} else {
  get umbrella
}

What they did was give you the english statement similar to what I did, but then translated it to the second code... not the clearest approach in my opinion.

-Rob
 
Thanks for all the help, skiflyer.
Doesn't the mysql_connect function have to be called first in order for an INSERT statement to work? I only see that function here:

if (!($connection = @ mysql_connect($hostName,
$username,
$password)))
die(&quot;Could not connect to database&quot;);

But there's a ! in front of it. I'm assuming this means in English: if the mysql_connect function fails, die. Else use the function?
 
One last thing,
in the following:
if ((@ mysql_query ($insertQuery,
$connection))
&& @ mysql_affected_rows() == 1)
echo &quot;<h3>Region successfully inserted</h3>&quot;;

How would the mysql_query function equal 1? I can see how mysql_affected_rows() could equal 1, as this function returns a number.
 
Assignment statements in PHP themselves return values. When the variable $connection is set to be equal to the return of mysql_connect(), that entire operation returns the value that was dumped into $connection. So it can be tested for in a condition. It's a useful ability of the language, but it can create readability problems.

A more understandable version of that same piece of code is:

Code:
$connection = @ mysql_connect($hostName,$username,$password);
if (!($connection == TRUE)
   die (...);

Or even more clearly:

Code:
$connection = @ mysql_connect($hostName,$username,$password);
if ($connection == FALSE)
   die (...);




The correct interpretation of the second condition you mentioned is:
Code:
if (
       (mysql_query(...))
     &&
       (mysql_affected_rows() == 1)
    )

Or in English,

&quot;If mysql_query() does not return FALSE and if mysql_affected_rows() returns 1, then do....&quot;

This is because operators have a hierarchy of precedence. The equivalence test operator &quot;==&quot; has higher precedence than the logical AND operator &quot;&&&quot;, so &quot;==&quot; will be tested first, then the &quot;&&&quot; operator.


Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks for the help. So if mysql_query can equal 1 (or true), does this mean that mysql_query returns a boolean value? This book says &quot;The function mysql_query() returns a result set handle resource; that is, a value that can retrieve the output-the result set-&quot; But the book does not say what data type the function returns.
I assume in the example

(if ((@ mysql_query ($insertQuery,
$connection))
&& @ mysql_affected_rows() == 1)

that mysql_query() is returning a boolean since it is being compared to a value of true.
 
This example code you are using is really of poor quality, particularly as examples for learning the language.

Since PHP is not a typed language, the engine will work very hard converting types for you automatically. The output of mysql_query() can be a resource handle, TRUE or FALSE, depending on the type of SQL query used with the function. PHP can then attempt to convert types -- non-zero values generally convert to TRUE, zero values convert to FALSE.

As an aside, PHP has two equivalence testing operators, &quot;==&quot; and &quot;===&quot;. The first tests for equivalence, the second tests for equivalence and the same type of value.

TRUE == 1 will evaluate to TRUE. &quot;1&quot; is not a boolean, but since it it non-zero, its value can be converted to a boolean TRUE.

TRUE === 1 will evaluate to FALSE. Although 1 is a non-zero value and can be converted to a boolean TRUE, the &quot;===&quot; operator does not allow the conversion to take place.

If a SELECT query is used and the query is successful, mysql_query() will return a resource handle or FALSE. A resource handle should evaluate to TRUE since it should always be non-zero.

Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top