1

I am trying to make a price slider with PHP and SQL , but i have a problem when i have some problem in this code

$query = mysql_query("SELECT * FROM price WHERE phone_price BETWEEN" .$from. "AND" .$to. );
while($row = mysql_fetch_array($query)){
print $row['phone_name'];
print $row['phone_price'];
print '';
}

I want to run the SQL query like SELECT * FROM price WHERE phone_price BETWEEN 300 AND 500

I am making a beta version therefore i am accepting the $from and $to values from <input> , i think i am making the error in inserting the variable in mysql_query .

THE ERROR -Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login\slide\slide.php on line 28

0

2 Answers 2

4

You have a mistake in your query. Spaces needed after BETWEEN and AND. Otherwise php reads your query like ...BETWEEN123AND1234.... And you should better use quotes to place vars:

$query = mysql_query("SELECT * FROM `price` WHERE `phone_price` BETWEEN '".$from."' AND '".$to."'");
Sign up to request clarification or add additional context in comments.

7 Comments

Don't use quotes around number.
but why do you put things inside quotes 'price' ??
$query = mysql_query("SELECT * FROM price WHERE phone_price BETWEEN " .$from. " AND " .$to ); i just added spaces and everything worked
Well quotes around number are not necessary, but it make code more structured. But spaces between BETWEEN and AND are necessary.
can you tell how can i get the value of $from and $to , through a slider
|
1

Use PDO build-In object. mysql_ functions were deprecated.

Initialize connection.

$dsn = "mysql:host=localhost;dbname=my_database";
$pdo = new PDO($dsn, $login, $password);

Use prepare statement.

$sh = $pdo->prepare("SELECT * FROM price WHERE phone_price BETWEEN :from AND :to");

Bind values and value types.

$sh->bindParam(':from',$from,PDO::PARAM_INT);
$sh->bindParam(':to',$to,PDO::PARAM_INT);

Fetch results into assoc array.

$res = $sh->fetch_all(PDO::FETCH_ASSOC);

Good Luck

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.