How do I make an AND in PHP and run it in mysql

Asked

Viewed 51 times

0

I’m doing a program in PHP that will need to execute a certain query on mysql, I’m not getting through.

SELECT * 
FROM tb_dados 
WHERE cod_uf_mun = 1100205
  AND no_curso LIKE '%vetérinaria%';

It doesn’t work because the AND is a reserved php word. How do I make this same sql?

1 answer

1

Your query has to be a string, it won’t conflict with PHP. Take a look at the example below. I suggest searching on the subject.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM tb_dados WHERE cod_uf_mun = 1100205  AND no_curso LIKE '%vetérinaria%'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

mysqli_query($query);

Take a look at this site: https://www.w3schools.com/php/php_mysql_select.asp

  • it doesn’t work, like if I take what I have from AND it works more when I use AND or it doesn’t work and it doesn’t return any errors.

  • Take a look at this other topic:https://stackoverflow.com/questions/34228090/cannot-query-mysql-database-viaphp

  • Got Maicon?

  • I managed to use your example and it worked

  • Mark the answer as right :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.