Franco Maldonado
Franco Maldonado

Reputation: 147

how to write a php variable on a mysql query

I need to write my php variables correctly.

$query2 = mysqli_query($con,"SELECT * FROM palettes LIMIT '$starting_number, $palettes_per_page'");

Upvotes: 0

Views: 52

Answers (3)

user9418917
user9418917

Reputation:

You could use parameterized queries which also prevent any need to use mysqli_real_escape_string

$stmt = $conn->prepare("SELECT * FROM palettes LIMIT ?, ?'"); $stmt->bind_param("ii", $starting_number, $palettes_per_page); $stmt->execute();

Upvotes: 0

alter-native
alter-native

Reputation: 13

Just remove the single quote, because double quote can read variable's value

$query2 = mysqli_query($con,"SELECT * FROM palettes LIMIT $starting_number, $palettes_per_page");

Hope this works for you

Upvotes: 0

Dave
Dave

Reputation: 320

I don't think you want single quotes on your LIMIT parameters. One way is to use . to concatenate strings.

Since $starting_number and $palettes_per_page are integers, you do not need to escape them. If they were strings, wrap them in mysqli_real_escape_string or mysqli_escape_string to escape special characters.

$query2 = mysqli_query( $con,
             "SELECT * FROM palettes LIMIT " .
             $starting_number .
             "," .
             $palettes_per_page );

Upvotes: 1

Related Questions