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
[ 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]
<?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.
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]
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';

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.