tags:

views:

71

answers:

3

I am a college student taking a course in php and mysql progamming and my first question is about the "$variable" variables in the following code:

<?php ob_start(); ?>

<?php

session_start();

if ($_SESSION['auth'] != "true")
{ header("Location: login.php");
  exit;
}

$uid = $_SESSION['user'];

$connection = mysql_connect("localhost", "username", "password");

mysql_select_db("username", $connection);

$result = mysql_query ( "SELECT * FROM users where user_id = '$uid'", 
$connection);

$num = mysql_numrows($result);

$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"firstname");
$f2=mysql_result($result,$i,"lastname");
?>

<html><body>
<p>
<td><center><font size = "18" face="Arial"><?php echo "Name: $f1 "; echo $f2; ?> </font></center></td>
</p>
</body></html>

<?php
$i++;
}
?>


<?php

$result1 = mysql_query ( "SELECT * FROM phone where user_id = '$uid'", $connection);

$num1 = mysql_numrows($result1);

$j=0;
while ($j < $num1) {
$f3=mysql_result($result1,$j,"type");
$f4=mysql_result($result1,$j,"number");
?>

<html><body>
<p>
<br>
<td><center><font size = "12" face="Arial"><?php echo "$f5: "; echo "($f3) "; echo "$f4 <br />"; ?> </font></center></td>
</p>
</body></html>

<?php
$j++;
}
?>

<?php

$result2 = mysql_query ( "SELECT * FROM address where user_id = '$uid'", $connection);

$num2 = mysql_numrows($result2);

$h=0;
while ($h < $num2) {
$f6=mysql_result($result2,$h,"type");
$f7=mysql_result($result2,$h,"address");
$f8=mysql_result($result2,$h,"city");
$f9=mysql_result($result2,$h,"state");
$f10=mysql_result($result2,$h,"zip");
?>

<html><body>
<p>
<br>
<td><center><font size = "12" face="Arial"><?php echo "$f10 Address: $f6, $f7, $f8 $f9"; ?></font></center></td>
</p>
</body></html>

<?php
$h++;
}
?>

<?php

include 'navbar.php';

ob_end_flush();

?>

I just don't really understand the $variables at all. Are they user-generated or are they entities in the database? And how does the code know which $result is which?

My second question is that, if this was someone else in my class's code and I wanted to modify it to make it my own and substitute my own variables, how would I go about doing that? Do the $variables need to be changed if they are not user-defined and if so, how? I apologize if these are dumb questions, but I am a beginner at this programming language. Thanks in advance for your help.

-Jeff

A: 

In PHP you don't need to define the variables and neither the var type. That's mean that the variable declaration is implicit in any new assignment. The $ symbol it's used by PHP to understand that it's a variable.

An example:

$result = mysql_query ( "SELECT * FROM users where user_id = '$uid'", $connection);

With this line you do basically 3 things:

  1. Define a new variable called $result
  2. Set the variable type based on the result type of your function a resource in this case
  3. Assign the function's result to the variable.

you can learn more about PHP variables and types on php.net, could be interesting for you also read more about === operator

next time, could you please use the code tag in your post ? :)

Cesar
OK your answer was very helpful. Now one more question on this:<pre><code>$myusername=$_POST["myusername"];$mypassword=$_POST["mypassword"];</code></pre>this creates variables called myusername and mypassword obviously. does this mean that the values in the brackets are grabbed via the post function as the database values?(i'm not sure how to use the code tag other than how i tried to haha. sorry.)
Jeff
`$_POST` are superglobal variables taken from a HTML `form` that was submitted. Please read the manual.
Anthony Forloney
@Jeff `$myusername=$_POST["myusername"]` assigns the content of the variable `$_POST["myusername"]` (`$_POST` is an array, `"myusername"` the array index) to the variable `$myusername`. That's all. The contents of `$_POST` will be whatever the user sent to the server via POST (i.e. from a form, usually).
deceze
A: 

basically, I just need to know a.) which $variables are part of the code and which $variables are entities of the database with dollar signs in front of them

The answer is that all variables are part of the code. There is no such thing as a "database entity with a dollar sign in front".

The value which a variable holds may well have different meanings and different origins. To know what each variable is used for, you will have to read and understand the code. Sometimes a variable is used for more than one purpose. There a some conventions around variables that may help you identify what a variable is used for.

$num = mysql_numrows($result);   // obviously holds the total number of rows
$i = 0;   // may be obscure, but "i" is often used as a counter in loops

while ($i < $num) {

    // very bad names for variables,
    // but it's pretty obvious what kind of value they hold here.
    $f1 = mysql_result($result, $i, "firstname");  // notice your friend $i again
    $f2 = mysql_result($result, $i, "lastname");

    // $firstname and $lastname would have been better names and
    // and would make the rest of the code easier to understand.

    ...

    $i++;  // yup, it's a loop counter after all
}

Good variable naming goes a long way in making code readable and avoid questions like the ones you have. You won't be able to just "replace" variables easily, they're all part of the overall algorithm, each has its place in the program. While some are more "integral" to the structure of the program, like loop counters, and others "just hold data", both are needed. The variables that hold the data will be needed at some point in the app, like when they're echoed. Just the same, loop counters are also necessary.

Only proper naming can help you distiguish between different variables uses. Get used to a clear naming convention early on, don't be afraid to be verbose. $customerFirstName is a good variable name, $f2 in this case isn't. $i is alright for loops, $num may be better off as $totalRows.

deceze
A: 

I suggest you to study some simple php tutorial, f.ex. this at w3schools.com. That helped me very much when I started to learn php. In half an hour you can learn a lot and you will be able to understand the code of your mate.

bancer