Results 1 to 12 of 12

Thread: Get values from a DB.

  1. #1

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Get values from a DB.

    Thanks everyone for your help..

    Ok 2 questions.
    When a page loads up, i wanted it to search my DB, and give me how many (count) of an item there are.
    or better yet, just display all the records.
    But when i run this code

    PHP Code:
    <?php 
    require_once ('mysql_connect.php');
    $query "SELECT dss(MBlock_Part_Number, MBlock_Ref) FROM dss ORDER BY MBlock_Part_Number";
    $result = @mysql_query ($query);

    if (
    $result){ 
    while (
    $row mysql_fetch_array($resultMYSQL_ASSOC)){
    Echo 
    ' <tr><td align="left">' $row['MBlock_Part_Number'] . '</td><td align="left">' $row['MBlock_Ref'] . '</td></tr>';
    }else{
    Echo 
    ' No data retrived ';
    }
    }
    ?>
    I get an error when i try to load the page.
    I just wanted it to search the DB and display the results..

    Is there a way i can search also, make a text box, where it inserts it into the query string, and then displays the result?

    Thanks again everyone.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Get values from a DB.

    What error? Always specify what error.

    The query doesn't look right to me. What's dss supposed to do? It's a table name, yet you use it like a function. And if it's a function, then you will only get one result column, namely the result of the function.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Get values from a DB.

    The table name is DSS, the database name is home_app

    i want to search my DB for that criterial above, and display all the results with the while loop.

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Get values from a DB.

    Your query is written wrong. Your while() loop and if() statement also have the wrong syntax: if you open an IF statement before the loop, you can't put an ELSE statement before the loop ends. You have to end the loop, then do the ELSE statement.

    PHP Code:
    <?php 
      
    require_once ('mysql_connect.php');
      
    $query "SELECT MBlock_Part_Number, MBlock_Ref FROM dss ORDER BY MBlock_Part_Number";
      
    $result = @mysql_query ($query);
      
    $count mysql_num_rows($result); //number of results

      
    if ($count 0){ 
        echo 
    "found $count results.<br /><br />\n";
        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){
          echo 
    ' <tr><td align="left">' $row['MBlock_Part_Number'] . '</td><td align="left">' $row['MBlock_Ref'] . '</td></tr>';
        }
      }else{
        echo 
    "No results found";
      }
    ?>
    Like Archer? Check out some Sterling Archer quotes.

  5. #5

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Get values from a DB.

    worked perfect

    kows i love you

  6. #6

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Get values from a DB.

    ok weird, when i take the code out of the page you made me it works, but when i keep it in there it dosent.
    I wanted to run 2 different querys from the same page.

    1 is where a person can enter there info, then it enters it into the DB, and thenthe one above, i can run either or of the 2 codes when i have them seperate, but not together.

    PHP Code:
    <?php 

    if(isset($_POST['submitted'])){$errors = array();
    }

    if(empty(
    $_POST['partnumber'])){$errors[] = 'You forgot a part number. ';
    }else{
    $partnumber trim($_POST['partnumber']);
    }

    if(empty(
    $_POST['refnumber'])){$errors[] = 'You forgot a ref number. ';
    }else{
    $refnumber trim($_POST['refnumber']);
    }

    if(empty(
    $_POST['partcomments'])){$errors[] = 'You forgot part comments. ';
    }else{
    $partcomments trim($_POST['partcomments']);
    }

    IF (empty(
    $errors)){

    require_once (
    'mysql_connect.php');

    $query "INSERT INTO dss(MBlock_Part_Number,MBlock_Ref,MBlock_Defect,MBlock_Date) VALUES ('$partnumber','$refnumber','$partcomments',NOW())";
    $result = @mysql_query($query);

    if(
    $result){echo 'Information entered into DB';
    mysql_close();
    }else{echo 
    '<p>'mysql_error(). $query '</p>';
    include(
    "DSS_Footer.php");
    exit(); 
    }}
    ?>

    <?php  
      
    require_once ('mysql_connect.php'); 
      
    $query "SELECT MBlock_Part_Number, MBlock_Ref FROM dss ORDER BY MBlock_Part_Number"
      
    $result = @mysql_query ($query); 
      
    $count mysql_num_rows($result); //number of results 

      
    if ($count 0){  
        echo 
    "found $count results.<br /><br />\n"
        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){ 
          echo 
    ' <tr><td align="left">' $row['MBlock_Part_Number'] . '</td><td align="left">' $row['MBlock_Ref'] . '</td></tr>'
        } 
      }else{ 
        echo 
    "No results found"
      } 
    ?>

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Get values from a DB.

    what do you mean? does it just not run, or do you get errors? if so, what errors do you get?

    Remove the second require_once() call (no point in trying to include it twice for no reason). require_once() is only used to stop that page from being included/evaluated more than once, so your second require_once() does absolutely nothing. Also, don't forget to move mysql_close() down to below the code that displays the results, so that you don't close the connection before the second query is ran (this might be your actual problem).

    the only other thing I can think of, other than those listed above, is to change the second variable names of $query and $result, to something like $query2 and $result2, so that there can be no unintentional conflict between the two queries.
    Last edited by kows; Oct 4th, 2006 at 05:07 PM.
    Like Archer? Check out some Sterling Archer quotes.

  8. #8

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Get values from a DB.

    When i go to the page, its fine, the query you gave me above works perfectly.

    But also on the same page, i have code where i can enter data into fields, hit the submit button and it puts data into the DB, now that works, but i get this message now with it.

    Information entered into DB
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Inetpub\jf1\php\testing2.php on line 38
    No results found

  9. #9

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Get values from a DB.

    Ok when i first go to my page, it looks like this below. (i typed in the field box's)

    Then when i hit "Enter Button" it does this:


    Here is my code:
    PHP Code:
    <?php include("DSS_Header.php"); ?>
    <?php 
    require_once ('mysql_connect.php'); ?>
    <?php 

    if(isset($_POST['submitted'])){$errors = array();
    }

    if(empty(
    $_POST['partnumber'])){$errors[] = 'You forgot a part number. ';
    }else{
    $partnumber trim($_POST['partnumber']);
    }

    if(empty(
    $_POST['refnumber'])){$errors[] = 'You forgot a ref number. ';
    }else{
    $refnumber trim($_POST['refnumber']);
    }

    if(empty(
    $_POST['partcomments'])){$errors[] = 'You forgot part comments. ';
    }else{
    $partcomments trim($_POST['partcomments']);
    }

    IF (empty(
    $errors)){

    $query "INSERT INTO dss(MBlock_Part_Number,MBlock_Ref,MBlock_Defect,MBlock_Date) VALUES ('$partnumber','$refnumber','$partcomments',NOW())";
    $result = @mysql_query($query);

    if(
    $result){echo 'Information entered into DB';
    mysql_close();
    }else{echo 
    '<p>'mysql_error(). $query '</p>';
    include(
    "DSS_Footer.php");
    exit(); 
    }}
    ?>

    <?php  
      $query 
    "SELECT MBlock_Part_Number, MBlock_Ref FROM dss ORDER BY MBlock_Part_Number"
      
    $result = @mysql_query ($query); 
      
    $count mysql_num_rows($result); //number of results 

      
    if ($count 0){  
        echo 
    "found $count results.<br /><br />\n"
        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){ 
          echo 
    ' <tr><td align="left">' $row['MBlock_Part_Number'] . '</td><td align="left">' $row['MBlock_Ref'] . '</td></tr>'
        } 
      }else{ 
        echo 
    "No results found"
      } 
    ?>

    <table cellspacing="2" cellpadding="2" border="0">
    <tr>
        <td><form action="testing2.php" method="post">
    <fieldset>
    <span style="font-size:small;font:sans serif;">Part Number:</span><br><input type="text" name="partnumber" size="20" maxlenght="20" /><br>
    <span style="font-size:small;font:sans serif;">Ref #:</span><br><input type="text" name="refnumber" size="20" maxlenght="20" /></td>
        <td valign="top"><fieldset>Date: <?php echo date('M n, Y'); ?></td>
        <td rowspan="2" valign="top" width="510"><fieldset><span style="font-size:small;font:sans serif;">DSS Overview:</span>
    <li><span style="font-size:small;font:sans serif;">0 : Massage Blocks Pending</span>
    <li><span style="font-size:small;font:sans serif;">0 : Inverters Pending</span>
    <li><span style="font-size:small;font:sans serif;">0 : Total Items Pending</span>
    </fieldset>
    </td>
    </tr>
    <tr>
        <td colspan="2"><fieldset><span style="font-size:small;font:sans serif;">Part Comments:</span><br> <textarea name="partcomments" rows="6" cols="40">
    </textarea><br><input type="submit" name="submit" value="Enter" align="right"/>
    </fieldset>
    </form></td>
    </tr>
    </table>
    <?php include("DSS_Footer.php"); ?>

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Get values from a DB.

    That's because you call mysql_close() after processing the INSERT query. With this call, you close the connection, so the SELECT query later tries to work on a closed connection.

    At least that's my guess. Remove the mysql_close() and try again. PHP closes the connection anyway, so the call is not necessary.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Get values from a DB.

    that got it
    thanks.

  12. #12
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Get values from a DB.

    Quote Originally Posted by kows
    Also, don't forget to move mysql_close() down to below the code that displays the results, so that you don't close the connection before the second query is ran (this might be your actual problem).
    always helps if you read :)
    Like Archer? Check out some Sterling Archer quotes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width