• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

PHP Variable Issues

I recently spent days trying to get an ASP page to work, only to find out that the Customer site uses PHP on Linux and Linux doesn't do ASP.

So I used ASP2PHP, but now I keep getting these funky Undefined Variable Errors

Here's the Code
****************************
49    echo "Microsoft.Jet.OLEDB.4.0";
50    $a2p_connstr=$DOCUMENT_ROOT."AirQuality.mdb";
51    $a2p_uid=strstr($a2p_connstr,'uid');
52    $a2p_uid=substr($d,strpos($d,'=')+1,strpos($d,';')-strpos($d,'=')-1);
53    $a2p_pwd=strstr($a2p_connstr,'pwd');
54    $a2p_pwd=substr($d,strpos($d,'=')+1,strpos($d,';')-strpos($d,'=')-1);
55    $a2p_database=strstr($a2p_connstr,'dsn');
56    $a2p_database=substr($d,strpos($d,'=')+1,strpos($d,';')-strpos($d,'=')-1);
57    $conn=mysql_connect("localhost",$a2p_uid,$a2p_pwd);
58    mysql_select_db($a2p_database,$conn);
59
60    // $rs is of type "ADODB.recordset"
61
62    $sql="SELECT DISTINCT UnitSeer FROM Webside ORDER BY UnitSeer";
63    $rs=mysql_query($sql);
64    $UnitSeer=$_POST["UnitSeer"];
***************************

Here's the Errors

Undefined variable:  DOCUMENT_ROOT in Y:/AirQuality/AirQualityQuery2.php on line 50
Undefined variable:  d in Y:/AirQuality/AirQualityQuery2.php on line 52
Undefined variable:  d in Y:/AirQuality/AirQualityQuery2.php on line 52
Undefined variable:  d in Y:/AirQuality/AirQualityQuery2.php on line 52
Undefined variable:  d in Y:/AirQuality/AirQualityQuery2.php on line 52
Undefined variable:  d in Y:/AirQuality/AirQualityQuery2.php on line 54
Undefined variable:  d in Y:/AirQuality/AirQualityQuery2.php on line 54
Undefined variable:  d in Y:/AirQuality/AirQualityQuery2.php on line 54
Undefined variable:  d in Y:/AirQuality/AirQualityQuery2.php on line 54
Undefined variable:  d in Y:/AirQuality/AirQualityQuery2.php on line 56
Undefined variable:  d in Y:/AirQuality/AirQualityQuery2.php on line 56
Undefined variable:  d in Y:/AirQuality/AirQualityQuery2.php on line 56
Undefined variable:  d in Y:/AirQuality/AirQualityQuery2.php on line 56
Warning:  mysql_connect(): Access denied for user: 'ODBC@localhost' (Using password: NO) in Y:/AirQuality/AirQualityQuery2.php on line 57
Warning:  mysql_select_db(): supplied argument is not a valid MySQL-Link resource in Y:/AirQuality/AirQualityQuery2.php on line 58
Warning:  mysql_query(): Access denied for user: 'ODBC@localhost' (Using password: NO) in Y:/AirQuality/AirQualityQuery2.php on line 63
Warning:  mysql_query(): A link to the server could not be established in Y:/AirQuality/AirQualityQuery2.php on line 63
Undefined index:  UnitSeer in Y:/AirQuality/AirQualityQuery2.php on line 64

Thank You very much!
0
resolvco
Asked:
resolvco
  • 11
  • 10
  • 6
  • +1
1 Solution
 
BogoJokerCommented:
Hi resolvco,

For DOCUMENT_ROOT you might want:
$_SERVER['DOCUMENT_ROOT']
OR
$_SERVER['PHP_SELF'];

You have not defined $d anywhere.  I can't tell what it is that you want it to be either.
Once you tell us what you want that $d to be (is it the document root?) then the rest of your code should fall into place.

Joe P
0
 
resolvcoAuthor Commented:
52 User
54 Password
56 Database

and Thanks Joe
0
 
Muhammad WasifCommented:
On top of your php page, put this

error_reporting(E_ALL ^ E_NOTICE);

On line 50, you are specifying path to MS Access DB. But connecting the DB with mysql_connect(). $conn=mysql_connect("localhost",$a2p_uid,$a2p_pwd);

What database engine you are using? Access or MySQL?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BogoJokerCommented:
Yes, read what wasifg said.  Access won't take the mysql_connect() method and such, its a few other but similiar methods.

Also is there two strings your looking at?
One that contains all the information and then your looking for User/Password/Database inside that larger string?

0
 
resolvcoAuthor Commented:
Converting over to MySQL
0
 
Muhammad WasifCommented:
Simply use this code to connect to mysql

link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
   die("Not connected : " . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
   die ("Can't use foo : " . mysql_error());
}


more info on this canbe found on http://www.php.net/mysql_select_db
0
 
BogoJokerCommented:
wasifg showed you the nice straightforward code, here is the more complex but better looking way it is done:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die("Not connected : " . mysql_error());
$db = mysql_select_db('foo', $link) or die("Can't use foo : " . mysql_error());

The or die() will only run if the mysql_function() returns false, otherwise because it is or php will already see a true and just move on.
0
 
BogoJokerCommented:
That code runs exactly the same as wasifg's.
I know he gave you the straightforward code because your just learning php and its connection with mysql.

Your current code looks very much like both of ours except ours has error checking and if an error happens withen the database connection ours will print out the error.
Joe P
0
 
resolvcoAuthor Commented:
Thanks so much....this is where I'm at thanks to you both........now the only error I get at all is "UnitSeer Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in \\192.168.1.107\AirQuality\Query.php on line 74". These are those Lines between the Asterisks. Below that is all the cose and the changes made

**********************************
  }
  print ">";
  print ("UnitSeer");
  $rs=mysql_fetch_array($rs_query);
}

$rs=null;
**********************************

Modified Code Below


<?
error_reporting(E_ALL ^ E_NOTICE);

$conn=mysql_connect("192.168.1.107","root","password!");
mysql_select_db("airquality",$conn);

// $rs is of type "ADODB.recordset"

$sql="SELECT DISTINCT UnitSeer FROM Webside ORDER BY UnitSeer";
$rs=mysql_query($sql);
$UnitSeer=$_POST["UnitSeer"];

?>

<form method="post">
<font size="4" face="Arial, Verdana, Verdana Ref">Choose Unit&nbsp; </font> <select name="UnitSeer" size="1">
<? while(!(($rs==0)))
{
  print "<option";
  if (("UnitSeer")==$country)
  {
    print " selected";
  }
  print ">";
  print ("UnitSeer");
  $rs=mysql_fetch_array($rs_query);
}

$rs=null;
?>
</select>
<input type="submit" value="Show Unit Costs">
</form>

 
<?
if ($UnitSeer!="")
{

  $sql="SELECT UnitSeer,OpCost,SEER6OpCost,YearsToPay FROM Webside WHERE UnitSeer='".$UnitSeer."'";
// $rs is of type "ADODB.Recordset"

  $rs=mysql_query($sql);?>
   <font face="Arial, Verdana, Verdana Ref"><div align="center"><table width="700px" cellspacing="0" cellpadding="2" border="0">
   <tr>
     <th>Unit Size <br />and SEER</th>
     <th>Annual<br />Operating Cost</th>
     <th>Annual Current<br />Operating Cost *</th>
     <th>Years to <br />payoff Installation<br /> with Savings</th>
   </tr></font>
<?
  while(!(($rs==0)))
  {

    print "<tr>";
    print "<td>".("UnitSeer")."</td>";
    print "<td>".("OpCost")."</td>";
    print "<td>".("SEER6OpCost")."</td>";
    print "<td>".("YearsToPay")."</td>";
    print "</tr>";
    $rs=mysql_fetch_array($rs_query);

  }
 
  mysql_close($conn);
  $rs=null;

  $conn=null;
?>

</table></div>
   <p>&nbsp;
</p>
   <p>
     <? } ?>
0
 
BogoJokerCommented:
Sure.

You have:
$rs=mysql_query($sql);

So you should make that:
$rs_query=mysql_query($sql);

Both times in your code I think.
Joe P

That is because what is returned from mysql_query() is what you put into msql_fetch_array()

Example: (this has nothing to do with your code, its just an example)
$result = mysql_query($sql, $conn);
$rowInResultSet = mysql_fetch_array($result);
0
 
resolvcoAuthor Commented:
So the ASP Version of Getting the Variable to Pass to the Query is:

UnitSeer=request.form("UnitSeer")

%>

<form method="post">
<font size="4" face="Arial, Verdana, Verdana Ref">Choose Unit&nbsp; </font> <select name="UnitSeer" size="1">
<%  do until rs.EOF
    response.write("<option")
    if rs.fields("UnitSeer")=country Then
      response.write(" selected")
    end If
    response.write(">")
    response.write(rs.fields("UnitSeer"))
    rs.MoveNext
Loop
rs.Close

And this is where I got stuck in the first place, passing a User selected value and then have a SQL Query pickup this value and display the results.

If you go to http://www.resolvco.com/AirQuality/AirQualityQuery2.asp you'll see how it works in ASP.

And again, Thanks Joe very Much!
0
 
BogoJokerCommented:
The thing I love about PHP is that it was so easy to learn. =)

The ASP equivalent of:
UnitSeer = request.form("UnitSeer");

Is:
$unit = $_POST['UnitSeer'];
(This also does the same thing: $unit = $_REQUEST['UnitSeer']);

------------------------------------------------------
Here is the crash course because I suspect you know most of the information from ASP, if this is well known to you just ignore it =)

The form has two things that PHP finds relevent.
<form method="POST" acton="actionPage.php">
The action is obviously the page/script that will run when the form is submitted and the method could be GET or POST.  If it is POST then once the page is submitted all the form data will be in the superglobal $_POST['fieldname'].  Likewise if it was GET then the information would be in $_GET['fieldname'].  And finally just using $_REQUEST['fieldname'] would actually be the same as GET, POST, and COOKIE (clearly working with cookies is as simple as using $_COOKIE['cookieName']).

Now when you make an element, most likely an <input>, <select>, or <textarea> inside a form you give it a name.  In your case you made <select name="UserSeer"> but let me take a simple case:
<form method="POST" action="processor.php">
  Textbox: <input type="text" name="TEXT1">
  <input type="submit" value="Submit Button" name="SUBMIT">
</form>

When that form is submitted, $_POST will have two indexs.  If you were to print $_POST (and $_REQUEST just the same) it would contain TEXT1 and SUBMIT.  I capitalized them to make the visualization connection simple but they don't need to be capitalized.  So transferring data from a form is just as simple in ASP as it is in PHP.  Just the element's name, indexed inside $_POST.

A couple pointers if you found that to be elementary to your skill level.  method="POST" is default so you don't have to write it in the <form> tag but it is nice to put.  $_GET gets from the URL string www.index.php?name=value&name=value1+string+keeps+going.  Anything else you want to learn I can point you to sites or maybe help you out here.

Joe P
0
 
BogoJokerCommented:
Oh, if you wanted to print out the contents of $_POST or any array you want just use: print_r()

So for fun and learning:
print_r($_POST);
0
 
resolvcoAuthor Commented:
Everything seems like it's working! The only issue remaining is setting up a visible range of Choices that a user can select from and when they do select it displays the information from the Query.

Your note Joe have helped out a Huge amount.

And yes, any Books you'd recomend would be great!
0
 
BogoJokerCommented:
Here are a list of tutorial websites: (free)
http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21792936.html

I also don't understand the last thing you said.
Joe P
0
 
Richard QuadlingSenior Software DeverloperCommented:
If you turn off notices, you may as well turn off errors. If your code is producing notices NOW, it will be producing warnings and errors later on.
0
 
resolvcoAuthor Commented:
From a Dropdown List, either on the Webpage or a sepparate one, a drop down list.

It would contain values like:

<SELECT name="UnitSeer" class="textbox">
              <OPTION value="2.13">2 Ton SEER 13</OPTION>
              <OPTION value="2.14">2 Ton SEER 14</OPTION>
              <OPTION value="2.15">2 Ton SEER 15</OPTION>
              <OPTION value="2.16">2 Ton SEER 16</OPTION>
              <OPTION value="3.13">3 Ton SEER 13</OPTION>
</SELECT>

and Once selected run the Query with that value......

<?php

error_reporting(E_ALL ^ E_NOTICE);

$conn=mysql_connect("192.168.1.107","root","Beatr1ce!");
      mysql_select_db("airquality",$conn);
$sql="SELECT DISTINCT UnitSeer FROM Webside ORDER BY UnitSeer";
$rs_query=mysql_query($sql);
$UnitSeer=$_POST["UnitSeer"];

?>

<form method="post">
<font size="4" face="Arial, Verdana, Verdana Ref">Choose Unit&nbsp; </font>
<select name="UnitSeer" size="1">

<?php
       while(!(($rs==0)))
{
      print "<option";
      if (("UnitSeer")==$UnitSeer)
  {
    print " selected";
  }
      print ">";
      print ("UnitSeer");
$rs=mysql_fetch_array($rs_query);
}

$rs=null;
?>

</select>
<input type="submit" value="Show Unit Costs">
</form>
 
<?php
      if ($UnitSeer!="")
{

$sql="SELECT UnitSeer,OpCost,SEER6OpCost,YearsToPay FROM Webside WHERE UnitSeer='".$UnitSeer."'";

$rs_query=mysql_query($sql);?>
<font face="Arial, Verdana, Verdana Ref"><div align="center"><table width="700px" cellspacing="0" cellpadding="2" border="0">
      <tr>
          <th>Unit Size <br />and SEER</th>
           <th>Annual<br />Operating Cost</th>
          <th>Annual Current<br />Operating Cost *</th>
          <th>Years to <br />payoff Installation<br /> with Savings</th>
   </tr>
</font>
<?php
      while(!(($rs==0)))
  {

    print "<tr>";
    print "<td>".("UnitSeer")."</td>";
    print "<td>".("OpCost")."</td>";
    print "<td>".("SEER6OpCost")."</td>";
    print "<td>".("YearsToPay")."</td>";
    print "</tr>";
$rs=mysql_fetch_array($rs_query);

  }
 
      mysql_close($conn);
$rs=null;

$conn=null;
?>

</table></div>
<p>&nbsp;</p>
<p>
<?php } ?>
0
 
BogoJokerCommented:
You said:
>and Once selected run the Query with that value......

Does that mean you don't want them to push a submit button, just when they select a value go.  That is possible like this:
<SELECT name="UnitSeer" class="textbox" onChange="this.form.submit();">
And make sure that your first value is something like:
<OPTION value="0">Select A Value to Submit</OPTION>

Or do you want to do something else?
Joe P
0
 
resolvcoAuthor Commented:
Best Support I've gotten from Experts Exchange!
0
 
BogoJokerCommented:
I'm still pretty new at this though, thanks for the comment.
People like you make this place fun.  You took the suggestions and made them work.  Most askers just take the code, don't alter a thing, run it, if it fails they come back and say it didnt work.  You worked with us to get it working so I Thank you as well.  Keep it up!

Joe P
0
 
Richard QuadlingSenior Software DeverloperCommented:
BogoJoker.

Welcome to EE. Your comments about some of the monkeys coming here is exactly right.

They just want the code. That's it. Nothing else. We should find out where they work and tell their boss's that they are too stupid to do the job themselves.

That's one of the reason I like to provide pseudo code. English explanations to the code to be written. Normally it is the principle of how to achieve something rather than the exact code people need. Sure they WANT the code, but they NEED education!

It is a shame that a LOT of experts here only want the points and only provide code without educating the questioner.

0
 
BogoJokerCommented:
I like how you put that, monkeys =)
I agree with you!  I normally do shell out code though because it seems like many of the people that come here are not 100% comfortable with english, so I shell out the code isntead of pseudocode because hopefully we both can read that.  I especially try my best to explain all teh code I give.  I have only come across a few "homework" looking problems.
0
 
resolvcoAuthor Commented:
Cleaned up the code a bit........now not getting any results at all



<html>
<head>
<meta name="GENERATOR" content="SAPIEN Technologies PrimalScript 3.1">
<title>Document Title</title>
</head>
<body>
<?php
error_reporting(E_ALL ^ E_NOTICE);
 $UnitSeer = $_POST['UnitSeer'];
 $dbname = "aqcinc";
 $dbserver = "localhost";
 $dbuser = "aqcinc";
 $dbpassword = "aqcinc";
 $connect = mysql_connect($dbserver,$dbuser,$dbpassword);
 ?>
 <?php
 mysql_select_db($dbname);
 $sql = "SELECT * FROM Web2 WHERE UnitSeer = $UnitSeer;";
 $query = mysql_query($sql,$connect);
 $count_rows = mysql_num_rows($query);
 if($count_rows == 1) {
 }
 while(!(($result_array==0)))
  {
    print "<table>";
    print "<tr>";
    print "<td>".("UnitSeer")."</td>";
    print "<td>".("OpCost")."</td>";
    print "<td>".("SEER6OpCost")."</td>";
    print "<td>".("YearsToPay")."</td>";
    print "</tr>";
    print "</table>";
 $result_array = mysql_fetch_assoc($query);
}
 ?>
</body>
</html>



0
 
Richard QuadlingSenior Software DeverloperCommented:
<?php
set_time_limit(0); // Just in case your script takes more than the default 30 seconds.
error_reporting(E_ALL); // Report EVERYTHING that is wrong with the script - even a NOTICE is a bug!!!

// Define output.
$s_row_output = '';

// Has UnitSeer been supplied?
if (isset($_POST['UnitSeer']))
      {
      // Define the DB access.
      $UnitSeer = $_POST['UnitSeer'];
      $dbname = "aqcinc";
      $dbserver = "localhost";
      $dbuser = "aqcinc";
      $dbpassword = "aqcinc";
      $connect = mysql_connect($dbserver,$dbuser,$dbpassword);
      mysql_select_db($dbname); // Watch out for persistent DB connections - ideally the SQL script SHOULD have the table names prefixed with the DBname.
      $sql = "SELECT * FROM Web2 WHERE UnitSeer = $UnitSeer;"; // This suggests that UnitSeer is a numeric value? I would recommend casting it.
      if (False !== ($query = mysql_query($sql,$connect)))
            {
            while(False !== ($result_array = mysql_fetch_assoc($query)))
                  {
                  $s_row_output .=  <<< END_HTML
<table>
      <tr>
            <td>{$result_array['UnitSeer']}</td>
            <td>{$result_array['OpCost']}</td>
            <td>{$result_array['SEER60pCost']}</td>
            <td>{$result_array['YearsToPay']}</td>
      </tr>
</table>
END_HTML;
                  }
            }
      }

echo >>> END_HTML
<html>
<head>
<meta name="GENERATOR" content="SAPIEN Technologies PrimalScript 3.1">
<title>Document Title</title>
</head>
<body>
{$s_row_output}
</body>
</html>
END_HTML;
?>
0
 
Richard QuadlingSenior Software DeverloperCommented:
Your non-working code does not utilise the array containing the results from the mysql_fetch_assoc() function.

I've shown you how to use the array and the use of heredoc to build a string.
0
 
resolvcoAuthor Commented:
Thanks, I'll get that in there! I appreciate your comments
0
 
Richard QuadlingSenior Software DeverloperCommented:
NP.
0
 
resolvcoAuthor Commented:
RQuading,

Joe provided some good tutorial links, do you have any suggestions on Books or Sites?

Thanks
0
 
Richard QuadlingSenior Software DeverloperCommented:
The best documentation IS the free PHP Manual. This can be downloaded as a CHM from http://www.php.net/docs.php. The English version is the one that is actively maintained. All other translations are done on a volunteer basis and are considered to always be behind the English version.

This is THE start point.

I would also recommend php|Architects Guide to PHP Design Patterns. Very useful for creating solid, maintainable code.

I would learn about separating logic/processing from layout/design - Separation of content - very important.

But everything else depends upon your background and abilities. If you are familiar with the C style syntax of PHP, then you don't need stuff relating to basic syntax.



0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 11
  • 10
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now