mubeen
mubeen

Reputation: 839

How to use php variable in mysql for result

$min_range=$_GET['min_range'];
$max_range=$_GET['max_range'];
$brand_name=$_GET['brand'];

$result=mysql_query(
    "
        SELECT 
            brand,
            image,
            price
        FROM 
            mobile
        WHERE 
            brand = '"$brand_name " 'price =3000
    "
);

I want to use php variable in my sql query . What is wrong with this code. and how it can be corrected.

it is saying "parse error:syntax error,unexpect('$brand_name')"

Upvotes: 0

Views: 91

Answers (6)

Change your query to

$result=mysql_query("SELECT brand,image,price FROM mobile WHERE brand='$brand_name' AND price=3000");

You actually forgot to separate the condition. Added an AND keyword.

Upvotes: 0

vara
vara

Reputation: 836

$result = $mysql->prepare("SELECT brand, image, price FROM mobile WHERE brand =? AND price=3000");

$result->bind_param($brand_name);

$result->execute();

Upvotes: 0

Hansen
Hansen

Reputation: 690

you can try this :

    $result=mysql_query(
    "
        SELECT brand, image, price
        FROM mobile
        WHERE 
            brand = '" . $brand_name . "' AND price = 3000
     ");

Upvotes: 0

user200747
user200747

Reputation: 1

$result=mysql_query(
    "
        SELECT 
            brand,
            image,
            price
        FROM 
            mobile
        WHERE 
            brand = '.$brand_name.' AND price =3000
    "
);

Upvotes: 0

jihyun
jihyun

Reputation: 1

$result = mysql_query("SELECT brand,image,price FROM mobile WHERE brand = '$brand_name' AND price = 3000");

Upvotes: 0

KodeFor.Me
KodeFor.Me

Reputation: 13511

One way is the following:

$result=mysql_query(
    "
        SELECT 
            brand,
            image,
            price
        FROM 
            mobile
        WHERE 
            brand = '" . $brand_name . "'
        AND
            price =3000
    "
);

another way is this:

$result=mysql_query(
    "
        SELECT 
            brand,
            image,
            price
        FROM 
            mobile
        WHERE 
            brand = '$brand_name'
        AND
            price = 3000
    "
);

Another way is this:

$result=mysql_query(
    sprintf(
        "
            SELECT 
                brand,
                image,
                price
            FROM 
                mobile
            WHERE 
                brand = '%s'
            AND
                price = 3000
        ",
        $brand_name
    )
);

Also notice that your code, comes with syntax errors, replace the AND operator with one that meet your needs.

Upvotes: 1

Related Questions