Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Query Variable(s) To Mysql DB Using Php


    Hello friends,

    I am working on php to improve my web development skills. I have created a web based university system using php and mysql. However, I got stuck in coding multi variables to query mysql using php. The multi variables are placed in mysql_query() with AND operator as shown below.

    $query3= mysql_query("SELECT * FROM Student WHERE Student_Name='$xstudent' AND Intake='$xmenu'");

    I could be able to query single variable however, face problems with multiple variables. The codes below works but when I query $xboth the output displays three tables showing $query, $query2 and $query3.

    I WANT TO DISPLAY ONLY $QUERY3 when passing $xboth.



    <?php
    include 'Lecture/connection.php';

    $xstudent=$_POST['stuname'];
    $xmenu=$_POST['drop'];
    $xboth= $_POST['stuname'] AND $_POST['drop'];


    $query= mysql_query("SELECT * FROM Student WHERE Student_Name='$xstudent'");
    $query2= mysql_query("SELECT * FROM Student WHERE Intake='$xmenu'");
    $query3= mysql_query("SELECT * FROM Student WHERE Student_Name='$xstudent' AND Intake='$xmenu'");


    if($xstudent)
    {
    $fields_num = mysql_num_fields($query);
    // Display table
    echo "<table border='1'><tr>";
    for($i=0; $i<$fields_num; $i++)
    {
    $field = mysql_fetch_field($query);
    echo "<td>{$field->name}</td>";
    }
    echo "</tr>\n";

    while($row = mysql_fetch_row($query))
    {
    echo "<tr >";
    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
    echo "<td width= '30' >$cell</td>";
    echo "</tr>\n";
    }

    }


    if($xmenu)
    {
    $fields_num2 = mysql_num_fields($query2);
    // Display table
    echo "<table border='1'><tr>";
    for($i=0; $i<$fields_num2; $i++)
    {
    $field2 = mysql_fetch_field($query2);
    echo "<td>{$field2->name}</td>";
    }
    echo "</tr>\n";

    while($row2 = mysql_fetch_row($query2))
    {
    echo "<tr >";
    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row2 as $cell2)
    echo "<td width= '30' >$cell2</td>";
    echo "</tr>\n";
    }
    }

    if($xboth )
    {
    $fields_num3 = mysql_num_fields($query3);
    // Display table
    echo "<table border='1'><tr>";
    for($i=0; $i<$fields_num3; $i++)
    {
    $field3 = mysql_fetch_field($query3);
    echo "<td>{$field3->name}</td>";
    }
    echo "</tr>\n";

    while($row3 = mysql_fetch_row($query3))
    {
    echo "<tr >";
    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row3 as $cell3)
    echo "<td width= '30' >$cell3</td>";
    echo "</tr>\n";
    }
    }


    THANKS GUYS
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,907
    Rep Power
    6351
    So you want the first table to only display if $xstudent and not $xboth? Do that:
    if ( $xstudent && !$xboth )

    The new user guide has information on how to format your posts, and is a good read for new users.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,457
    Rep Power
    594
    1) If you are learning, STOP. Do not use the depracated mysql extensions. Switch to PDO.
    2) Please enclose your code in [ PHP ] tags (see the sticky at the top of this forum) and use proper indentation.
    3) Are $xstudent, $xmenu and $xboth mutually exclusive? You probably want to use elseif rather than just if on the 2nd and 3rd conditional blocks.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by ManiacDan
    So you want the first table to only display if $xstudent and not $xboth? Do that:
    if ( $xstudent && !$xboth )

    The new user guide has information on how to format your posts, and is a good read for new users.
    Noted will read the user guide on formatting the post.

    When I pass the variable $xboth, it runs if($xboth) block but it also runs if($xstudent) and if($xmenu) block. How can I execute only if($xboth) block when $xboth variable is passed.

    $xboth is basically a variable containing values from two attributes with in the same table (Student_Name and Intake).
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,907
    Rep Power
    6351
    ...I answered your question exactly. Did you not understand what I said? You want to only print the first table if $xstudent but not $xboth. That's the data condition you're looking for. Use that in your IF, just like I wrote it.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,054
    Rep Power
    9398
    Originally Posted by gw1500se
    depracated
    I'm trying to figure out why you bolded the 'a'...

    Comments on this post

    • gw1500se agrees : Beacuse Jaques1 insists that is what I should use.
    • ptr2void agrees : Yes, but the a should be an e. It's "deprecated". :)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by ManiacDan
    ...I answered your question exactly. Did you not understand what I said? You want to only print the first table if $xstudent but not $xboth. That's the data condition you're looking for. Use that in your IF, just like I wrote it.
    Thanks big bro for answering. I want to print only the third table if $xboth but not if $xstudent and if $xmenu. Its runs all three $xstudent, $xmenu and $xboth when passing $xboth

    I am still learning Cool down bro!!!
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,907
    Rep Power
    6351
    I'm not worked up or anything, I'm just wondering what the disconnect is.

    You solved the problem then I assume.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. I fail at spelling
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Sep 2003
    Location
    NDAuNjIxMTExLC03OS4xNTU=
    Posts
    3,232
    Rep Power
    1774
    PHP Code:
    <?php
    include 'Lecture/connection.php';

    $xstudent=$_POST['stuname'];
    $xmenu=$_POST['drop'];
    $xboth$_POST['stuname'] AND $_POST['drop'];


    $querymysql_query("SELECT * FROM Student WHERE Student_Name='$xstudent'");
    $query2mysql_query("SELECT * FROM Student WHERE Intake='$xmenu'");
    $query3mysql_query("SELECT * FROM Student WHERE Student_Name='$xstudent' AND Intake='$xmenu'");


    if(
    $xstudent)
    {
        
    $fields_num mysql_num_fields($query);
        
    // Display table
        
    echo "<table border='1'><tr>";
        for(
    $i=0$i<$fields_num$i++)
        {
            
    $field mysql_fetch_field($query);
            echo 
    "<td>{$field->name}</td>";
        }
        echo 
    "</tr>\n";

        while(
    $row mysql_fetch_row($query))
        {
            echo 
    "<tr >";
            
    // $row is array... foreach( .. ) puts every element
            // of $row to $cell variable
            
    foreach($row as $cell) {
                echo 
    "<td width= '30' >$cell</td>";
                echo 
    "</tr>\n";
            }
        }
    }


    if(
    $xmenu)
    {
        
    $fields_num2 mysql_num_fields($query2);
        
    // Display table
        
    echo "<table border='1'><tr>";
        for(
    $i=0$i<$fields_num2$i++)
        {
            
    $field2 mysql_fetch_field($query2);
            echo 
    "<td>{$field2->name}</td>";
        }
        echo 
    "</tr>\n";

        while(
    $row2 mysql_fetch_row($query2))
        {
            echo 
    "<tr >";
            
    // $row is array... foreach( .. ) puts every element
            // of $row to $cell variable
            
    foreach($row2 as $cell2) {
                echo 
    "<td width= '30' >$cell2</td>";
                echo 
    "</tr>\n";
            }
        }
    }

    if(
    $xboth )
    {
        
    $fields_num3 mysql_num_fields($query3);
        
    // Display table
        
    echo "<table border='1'><tr>";
        for(
    $i=0$i<$fields_num3$i++)
        {
            
    $field3 mysql_fetch_field($query3);
            echo 
    "<td>{$field3->name}</td>";
        }
        echo 
    "</tr>\n";

        while(
    $row3 mysql_fetch_row($query3))
        {
            echo 
    "<tr >";
            
    // $row is array... foreach( .. ) puts every element
            // of $row to $cell variable
            
    foreach($row3 as $cell3) {
                echo 
    "<td width= '30' >$cell3</td>";
                echo 
    "</tr>\n";
            }
        }
    }
    ?>
    Just helping, put braces in your foreach calls that were missing.
    I am working now with Symfony2, Twig, Doctrine, Composer, Assetic, and HTML5. Enjoying doing what I do everyday!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0
    Finally, after weeks of struggle got it working. Thanks ManiacDan, it was data condition and u guided me correctly how ever, instead of if ( $xstudent && !$xboth ) we should use if ( $xstudent && !$xmenu ) and if ( $xmenu && !$xstudent ) and last block will be if ($xboth) to make it work.

    Thanks again
    really appreciated
  20. #11
  21. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,907
    Rep Power
    6351
    Both would work fine, unless your variables are the wrong data type.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by ManiacDan
    Both would work fine, unless your variables are the wrong data type.
    all data type used is 'String'
  24. #13
  25. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,907
    Rep Power
    6351
    Why are they strings? You're using them like booleans.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by ManiacDan
    Why are they strings? You're using them like booleans.
    Not really... all the input values of the variables are string type. I am using data conditioning to query a particular record whose value(s) is derived from multiple flied.
  28. #15
  29. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,907
    Rep Power
    6351
    PHP Code:
    if($xboth 
    You are using them like booleans. The inside of an IF must be a true/false expression. PHP will fake it if you do it wrong, but you're still using them like booleans.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo