[SOLVED] Php And Mysql

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • underground
    New Member
    • Sep 2006
    • 41

    [SOLVED] Php And Mysql

    I have a issue with getting a single varible via url and using php to query mysql database and display results from the row the varible occupy's. I've been working on this for about 3 days and have found hundreds of toturials online but none seem to be affective

    Here is the link used to pass the varible via url to the sample.php

    <a href="sample.ph p?CustomerID=3" >click here</a>

    the sample.php is supppose to retrieve the variable and display the information associated with it like this..

    [PHP]
    <?php
    $CustomerID = isset($_REQUEST['CustomerID']) ? intval($Custome rID) : 0;
    if($CustomerID < 0) { $CustomerID = 0;}
    $usr = "atl";
    $pwd = "XXXXX";
    $db = "XXXXXX XXXXX";
    $host = "localhost" ;
    # connect to database
    $cid = mysql_connect($ host,$usr,$pwd) ;
    mysql_select_db ($db);
    if (mysql_error()) { print "Database ERROR: " . mysql_error(); }
    $sql = "SELECT * FROM Registration1 WHERE CustomerID = {$CustomerID}";
    mysql_query($sq l);
    ?>
    <? echo $variablename; ?>
    <?
    $i=0;
    while($row = @mysql_fetch_ar ray($result)) {
    $RetailPrice=my sql_result($res ult,$i,"RetailP rice");
    $AlbumName=mysq l_result($resul t,$i,"AlbumName ");
    $Month=mysql_re sult($result,$i ,"Month");
    $Day=mysql_resu lt($result,$i," Day");
    $Year=mysql_res ult($result,$i, "Year");
    ?>
    [/PHP]

    The result from the database would passed to a html document like so
    <html>
    <head>
    <title>Untitled </title>
    </head>

    <body>

    <? echo $RetailPrice; ?>
    <? echo $AlbumName; ?>
    <? echo $Month; ?>
    <? echo $Day; ?>
    <? echo $Year; ?>

    </body>
    </html>

    when the link is pressed the value can be seen in the address bar but the sample page only displays a blank page..any thoughts?
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    You only show half your code. We cannot see how you pass the data from your db script to your display script. So show the code (WITHIN [CODE] TAGS!!!). and the forum members can have a look at it and try to see what is wrong.

    Ronald :cool:

    Comment

    • underground
      New Member
      • Sep 2006
      • 41

      #3
      Originally posted by ronverdonk
      You only show half your code. We cannot see how you pass the data from your db script to your display script. So show the code (WITHIN [CODE] TAGS!!!). and the forum members can have a look at it and try to see what is wrong.

      Ronald :cool:
      The Code Within Code Tags? I not sure what that is as it could be the problem.
      Based on a toturial the varible is passed using the link via url...
      The PHP script has a get() at the top script that get the varible and passes it to the DB query {$CustomerID}
      The DB retreives the data based of the CustomerID passed. The information is
      then echoed to the corresponding html.. I use a similar set for taking customer
      info via web form the only difference is I manual assign the varible to the query like below
      [PHP]
      <?
      $usr = "xxxxxxxxxx x";
      $pwd = "XXXXXXX";
      $db = "XXXXXX";
      $host = "XXXXXXXX";

      # connect to database
      $cid = mysql_connect($ host,$usr,$pwd) ;
      mysql_select_db ($db);
      if (mysql_error()) { print "Database ERROR: " . mysql_error(); }

      $result = mysql_query("SE LECT * FROM Registration1
      WHERE CustomerID='3'" );
      [/PHP]

      I know it's premative but it works fine the data is echoed into a html similar to the above with no (WITHIN [CODE] TAGS!!!) just css and html like this
      <?
      $i++;
      }


      echo "</table>";
      mysql_close();
      ?>
      </BODY></HTML>



      could you elaborate more! I will google it and see what I come up with..

      Thanks

      Comment

      • bevort
        New Member
        • Jul 2006
        • 53

        #4
        Originally posted by underground
        I have a issue with getting a single varible via url and using php to query mysql database and display results from the row the varible occupy's. I've been working on this for about 3 days and have found hundreds of toturials online but none seem to be affective

        Here is the link used to pass the varible via url to the sample.php

        <a href="sample.ph p?CustomerID=3" >click here</a>

        the sample.php is supppose to retrieve the variable and display the information associated with it like this..

        [PHP]
        <?php
        $CustomerID = isset($_REQUEST['CustomerID']) ? intval($Custome rID) : 0;
        if($CustomerID < 0) { $CustomerID = 0;}
        $usr = "atl";
        $pwd = "XXXXX";
        $db = "XXXXXX XXXXX";
        $host = "localhost" ;
        # connect to database
        $cid = mysql_connect($ host,$usr,$pwd) ;
        mysql_select_db ($db);
        if (mysql_error()) { print "Database ERROR: " . mysql_error(); }
        $sql = "SELECT * FROM Registration1 WHERE CustomerID = {$CustomerID}";
        mysql_query($sq l);
        ?>
        <? echo $variablename; ?>
        <?
        $i=0;
        while($row = @mysql_fetch_ar ray($result)) {
        $RetailPrice=my sql_result($res ult,$i,"RetailP rice");
        $AlbumName=mysq l_result($resul t,$i,"AlbumName ");
        $Month=mysql_re sult($result,$i ,"Month");
        $Day=mysql_resu lt($result,$i," Day");
        $Year=mysql_res ult($result,$i, "Year");
        ?>
        [/PHP]

        The result from the database would passed to a html document like so
        <html>
        <head>
        <title>Untitled </title>
        </head>

        <body>

        <? echo $RetailPrice; ?>
        <? echo $AlbumName; ?>
        <? echo $Month; ?>
        <? echo $Day; ?>
        <? echo $Year; ?>

        </body>
        </html>

        when the link is pressed the value can be seen in the address bar but the sample page only displays a blank page..any thoughts?

        If this is all the code then I wonder where you store the results of your query
        Change the line "mysql_query($s ql);" to $result=mysql_q uery($sql);

        Comment

        • underground
          New Member
          • Sep 2006
          • 41

          #5
          Originally posted by bevort
          If this is all the code then I wonder where you store the results of your query
          Change the line "mysql_query($s ql);" to $result=mysql_q uery($sql);
          I'm not sure I follow you so let me simplify. What I'm trying to do is very simular to how this forum functions by passing a varibles to sql query to display a specific post such as this one...

          The html resides in the same page as the php so they are in the same page. the varible that is passed by the link is then passed to the sampe php.page and the query in the script and displays the result in the html by means of echo..

          I'm really out my rage and maybe I going this all wrong please help!

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Did you do what bevort in his previous reply told you to do i.e.

            Change the line "mysql_query($s ql);" to $result=mysql_q uery($sql);
            If you don't want to assign the result of your query to a varibale, there is no point to continue this discussion.

            Ronald :cool:

            Comment

            • underground
              New Member
              • Sep 2006
              • 41

              #7
              Originally posted by ronverdonk
              Did you do what bevort in his previous reply told you to do i.e.


              If you don't want to assign the result of your query to a varibale, there is no point to continue this discussion.

              Ronald :cool:
              yes, and it failed. Let me present the question like this without the code example. What 'm trying to do is pass a varible or value via url. this will be passed to say the d.php page that will get the varible and query DB and display the results..on the d.php page.

              the code you keep refering to I simply don't have and I'm trying to find out what it your asking for?

              But if that's a waste of your time, please don't bother to post.

              If this is a waste of your time please don't bother to answer.

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                Don't you start like that. This forum is run by volunteers who dedicate their time and expertise to help others with problems. But they may expect that the requestors have some basic skills, can explain their problem in plain english and do not treat them as 'coding cattle'. Realise that it very difficult to answer a question when the requestor does himself not know what he is actually (technically) asking for and has not even read the "Posting Guidelines" that apply to this forum.

                And from your request the members of this forum have rightly assumed that you knew what you were talking about. That is where the question about the code comes from. You should have posted the code of the calling html script, so we could see how it was passed.

                Taking that into account and the fact that code that you did post has so many errors, it would be contrary to continue the discussion about errors in that code.

                So here is a sample solution to your request. Run this code (after changing your db settings) and you'll see how it works. Look at the code and learn how to pass data from a script to another script.
                [php]
                <?php
                // check to see if the form was posted with the correct id's
                if (isset($_POST['_submit']) AND isset($_REQUEST['CustomerID']) ) {
                $CustomerID = intval($_REQUES T['CustomerID']);
                if ($CustomerID < 0) {
                $CustomerID = 0;
                }
                $usr = "xxxxxx";
                $pwd = "yyyyy";
                $db = "zzzzzz";
                $host = "localhost" ;
                // connect to database server
                $cid = mysql_connect($ host,$usr,$pwd)
                or die("Server connnect ERROR: " . mysql_error());
                // select the database
                mysql_select_db ($db)
                or die("Database select ERROR: " . mysql_error());
                $sql = "SELECT * FROM Registration1 WHERE CustomerID = $CustomerID";
                // issue the SELECT command
                $res = mysql_query($sq l);
                // if no result: issue message and re-display form
                if (mysql_num_rows ($res) < 1) {
                echo "No records found for CustomerID: $CustomerID";
                }
                // at least 1 row result, assign to vars and display
                else {
                while ($row = mysql_fetch_ass oc($res)) {
                $RetailPrice= $row['RetailPrice'];
                $AlbumName = $row['AlbumName'];
                $Month = $row['Month'];
                $Day = $row['Day'];
                $Year = $row['Year'];
                echo "CustomerID = $CustomerID <br>" .
                "RetailPric e = $RetailPrice <br>" .
                "AlbumName = $AlbumName <br>" .
                "Month = $Month <br>" .
                "Day = $Day <br>" .
                "Year = $Year <br>";
                }
                // do whatever processing you have to do
                /* exit; */
                }
                }
                // Display the request form
                ?>
                <form name="MyForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                <input type="text" name="CustomerI D" />
                <input type="hidden" name="_submit" value=1 />
                <input type="submit" value="Submit it!" />
                </form>
                [/php]

                Ronald :cool:

                Comment

                • underground
                  New Member
                  • Sep 2006
                  • 41

                  #9
                  Originally posted by ronverdonk
                  Don't you start like that. This forum is run by volunteers who dedicate their time and expertise to help others with problems. But they may expect that the requestors have some basic skills, can explain their problem in plain english and do not treat them as 'coding cattle'. Realise that it very difficult to answer a question when the requestor does himself not know what he is actually (technically) asking for and has not even read the "Posting Guidelines" that apply to this forum.

                  And from your request the members of this forum have rightly assumed that you knew what you were talking about. That is where the question about the code comes from. You should have posted the code of the calling html script, so we could see how it was passed.

                  Taking that into account and the fact that code that you did post has so many errors, it would be contrary to continue the discussion about errors in that code.

                  So here is a sample solution to your request. Run this code (after changing your db settings) and you'll see how it works. Look at the code and learn how to pass data from a script to another script.
                  [php]
                  <?php
                  // check to see if the form was posted with the correct id's
                  if (isset($_POST['_submit']) AND isset($_REQUEST['CustomerID']) ) {
                  $CustomerID = intval($_REQUES T['CustomerID']);
                  if ($CustomerID < 0) {
                  $CustomerID = 0;
                  }
                  $usr = "xxxxxx";
                  $pwd = "yyyyy";
                  $db = "zzzzzz";
                  $host = "localhost" ;
                  // connect to database server
                  $cid = mysql_connect($ host,$usr,$pwd)
                  or die("Server connnect ERROR: " . mysql_error());
                  // select the database
                  mysql_select_db ($db)
                  or die("Database select ERROR: " . mysql_error());
                  $sql = "SELECT * FROM Registration1 WHERE CustomerID = $CustomerID";
                  // issue the SELECT command
                  $res = mysql_query($sq l);
                  // if no result: issue message and re-display form
                  if (mysql_num_rows ($res) < 1) {
                  echo "No records found for CustomerID: $CustomerID";
                  }
                  // at least 1 row result, assign to vars and display
                  else {
                  while ($row = mysql_fetch_ass oc($res)) {
                  $RetailPrice= $row['RetailPrice'];
                  $AlbumName = $row['AlbumName'];
                  $Month = $row['Month'];
                  $Day = $row['Day'];
                  $Year = $row['Year'];
                  echo "CustomerID = $CustomerID <br>" .
                  "RetailPric e = $RetailPrice <br>" .
                  "AlbumName = $AlbumName <br>" .
                  "Month = $Month <br>" .
                  "Day = $Day <br>" .
                  "Year = $Year <br>";
                  }
                  // do whatever processing you have to do
                  /* exit; */
                  }
                  }
                  // Display the request form
                  ?>
                  <form name="MyForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                  <input type="text" name="CustomerI D" />
                  <input type="hidden" name="_submit" value=1 />
                  <input type="submit" value="Submit it!" />
                  </form>
                  [/php]

                  Ronald :cool:
                  I didn't I was coming off as a person that know what he talking about but I did get a little frustrated. Thank for display however I figured it out a couple days ago thanks for the push!!!!

                  Comment

                  Working...