Jump to content

Understanding variables used with MySql query statements


scotthammersley

Recommended Posts

Im relatively new to php and mysql, i have read several books on php but i cant seem to get a straight answer.

When passing form information to a php script i can get it to work with a simple script but not when i try to do more.  I have a couple of questions.

1) do i use $_GET[''] or @$_GET[''] or even $HTTP_GET_VARS[''] to assign variable info.
2) Have the following code to execute an Sql query, the query needs to use variables.  I get either a T_ECHO error or Unexpected '>'. Now this may be because of the first question, i dont know.

?php
//sort

include 'pattern_search.php';

$m = $_GET['order'];
$h = $_GET['how'];
$pattern = $_GET['pattern'];
$piece = $_GET['piece'];


$host = ;
$user = ;
$pass = ;
$database =;


    mysql_connect($host, $user, $pass);
    @mysql_select_db($database) or die( "Unable to select database");
//select from ds_carts
    $query = ("SELECT * FROM ds_carts WHERE pNum LIKE $pattern OR pName LIKE $piece ORDER BY $m OR $h");   
    $result = mysql_db_query(gpittman1, $query);


    if  ($result)
    {
    echo "<font><align=center><b>Current Collectors - Sorted By $m in $h order.</b><br><br>";
    echo "<table width=90% align=center border=1><tr>
        <td align=center bgcolor=#00FFFF>Product Name</td>
        <td align=center bgcolor=#00FFFF>Sku</td>
        <td align=center bgcolor=#00FFFF>Qty</td>
        <td align=center bgcolor=#00FFFF>Price</td>
        <td align=center bgcolor=#00FFFF>Total</td>
       
            </tr>";

 
            while ($r = mysql_fetch_array($result)) {    // Begin while
         
           
            $name = $r["pName"];
            $sku = $r["pNum"];
            $qty = $r["pQty"];
            $price = $r["pPrice"];
            $total = $r[$price * $qty];
           
echo "<tr>
                        <td>$name</td>
                        <td>$sku</td>
                        <td>$qty</td>
                        <td>$price</td>
                        <td>$total</td>
                       
</tr>";
        }  // end while
        echo "</table>";
    } else { echo "problems...."; }


?>

This may seem trivial but im stumped.

Thanks
Link to comment
Share on other sites

[ code] [/ code]

without spaces

[code]
<?php
//sort

include 'pattern_search.php';

$m = $_GET['order'];
$h = $_GET['how'];
$pattern = $_GET['pattern'];
$piece = $_GET['piece'];


$host = ;
$user = ;
$pass = ;
$database =;


    mysql_connect($host, $user, $pass);
    @mysql_select_db($database) or die( "Unable to select database");
//select from ds_carts
    $query = ("SELECT * FROM ds_carts WHERE pNum LIKE $pattern OR pName LIKE $piece ORDER BY $m OR $h"); 
    $result = mysql_db_query(gpittman1, $query);


    if  ($result)
    {
    echo "<font><align=center>Current Collectors - Sorted By $m in $h order.

";
    echo "<table width=90% align=center border=1><tr>
        <td align=center bgcolor=#00FFFF>Product Name</td>
        <td align=center bgcolor=#00FFFF>Sku</td>
        <td align=center bgcolor=#00FFFF>Qty</td>
        <td align=center bgcolor=#00FFFF>Price</td>
        <td align=center bgcolor=#00FFFF>Total</td>
       
            </tr>";

 
            while ($r = mysql_fetch_array($result)) {    // Begin while
         
         
            $name = $r["pName"];
            $sku = $r["pNum"];
            $qty = $r["pQty"];
            $price = $r["pPrice"];
            $total = $r[$price * $qty];
         
echo "<tr>
                        <td>$name</td>
                        <td>$sku</td>
                        <td>$qty</td>
                        <td>$price</td>
                        <td>$total</td>
                     
</tr>";
        }  // end while
        echo "</table>";
    } else { echo "problems...."; }

 
?>
[/code]
Link to comment
Share on other sites

<?php
//sort
include 'pattern_search.php';
$submit = $_GET['submit'];
$m = $_GET['order'];
$h = $_GET['how'];
$pattern = $_GET['pattern'];
$piece = $_GET['piece'];
$host = ;
$user = ;
$pass = ;
$database = 'gpittman1';
mysql_connect($host, $user, $pass);
    @mysql_select_db($database) or die( "Unable to select database");
//select from ds_customers
    $query = ("SELECT * FROM ds_carts WHERE pNum LIKE $pattern OR pName LIKE $piece ORDER BY $m OR $h");   
    $result = mysql_db_query(gpittman1, $query);
if  ($result)
    {
    echo "<font><align=center><b>Current Collectors - Sorted By $m in $h order.</b><br><br>";
    echo "<table width=90% align=center border=1><tr>
        <td align=center bgcolor=#00FFFF>Product Name</td>
        <td align=center bgcolor=#00FFFF>Sku</td>
        <td align=center bgcolor=#00FFFF>Qty</td>
        <td align=center bgcolor=#00FFFF>Price</td>
        <td align=center bgcolor=#00FFFF>Total</td>
        </tr>
"while ($r = mysql_fetch_array($result)) {    // Begin while
            $name = $r["pName"];
            $sku = $r["pNum"];
            $qty = $r["pQty"];
            $price = $r["pPrice"];
            $total = $r[$price * $qty];
            echo "<tr>
                        <td>$name</td>
                        <td>$sku</td>
                        <td>$qty</td>
                        <td>$price</td>
                        <td>$total</td>
                        </tr>";
        }  // end while
        echo "</table>";
    } else { echo "problems...."; } ?>


Is this what you mean, you have to excuse me, this is first time trying to get help using forums.
Link to comment
Share on other sites

try:

[code]
<?php
//sort

include("pattern_search.php");

$m = $_GET[order];
$h = $_GET[how];
$pattern = $_GET[pattern];
$piece = $_GET[piece];


$host = "";
$user = "";
$pass = "";
$database = "";

$connect = mysql_connect($host,$user,$pass);
$db = mysql_select_db($database,$connect) or die(mysql_error());
//select from ds_carts
    $query = "SELECT * FROM ds_carts WHERE pNum LIKE $pattern OR pName LIKE $piece ORDER BY $m"; 
    $result = mysql_query($query);


    if  ($result)
    {
    echo "<font><align=center>Current Collectors - Sorted By $m in $h order.";
    echo "
        <table width=90% align=center border=1><tr>
        <td align=center bgcolor=#00FFFF>Product Name</td>
        <td align=center bgcolor=#00FFFF>Sku</td>
        <td align=center bgcolor=#00FFFF>Qty</td>
        <td align=center bgcolor=#00FFFF>Price</td>
        <td align=center bgcolor=#00FFFF>Total</td>
        </tr>";

 
            while ($r = mysql_fetch_assoc($result)) {    // Begin while
         
         
            $name = $r["pName"];
            $sku = $r["pNum"];
            $qty = $r["pQty"];
            $price = $r["pPrice"];
            $total = $r[$price * $qty];
         
echo "<tr>
                        <td>$name</td>
                        <td>$sku</td>
                        <td>$qty</td>
                        <td>$price</td>
                        <td>$total</td>
                     
</tr>";
        }  // end while
  mysql_free_result($result);
        echo "</table>";
    } else {
die("ERROR");
};

?>
[/code]
Link to comment
Share on other sites

thats what i was saying earlier, i dont get an error from the script except on the 'if' statement, i dont think the form is passing the info over, or its that the script isnt processing the info because if i try to echo even the username, it doesnt display, it just displays $user.  Like this.

$connect = mysql_connect($host,$user,$pass);
$db = mysql_select_db($database,$connect) or die(mysql_error());
//select from ds_carts
    $query = "SELECT * FROM ds_carts WHERE pNum LIKE $pattern OR pName LIKE $piece ORDER BY $m"; 
    $result = mysql_query($query);
echo '$user';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.