Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mysqli SELECT query won't execute

$dc=fopen("numeu.txt","rb");
$row1=fgets($dc,120);   
$row2=fgets($dc,120);
$row3=fgets($dc,120);

fclose($dc);

$servername="localhost";
$username="root";
$password='';
$database="Stildev";

$conn = new mysqli($servername, $username, $password, $database);

$sql="SELECT poza1,poza2,poza3,poza4,poza5,poza6 FROM postari WHERE utilizator='".$row2."' AND subiect='".$row1."' AND id2='".$row3."'";


$resultat=$conn->query($sql);

echo $resultat->num_rows;

This piece of code returns 0 rows

There is no error and sql query works just fine in MySQL console.

Also i discovered that withought WHERE clause the code works.How you explain that?

like image 526
Avram Alex Avatar asked Jul 18 '26 06:07

Avram Alex


1 Answers

Is the password empty on purpose?

Try adding

if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
        . $mysqli->connect_error);
}

Also, according to https://php.net/manual/ro/mysqli.query.php, this may help:

if ($result = $mysqli->query($sql)) {
    echo $result->num_rows;
    /* close result set */
    $result->close();
}

If you are using several result-returning queries, close previous results with ->close();

like image 80
Alex Tartan Avatar answered Jul 20 '26 19:07

Alex Tartan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!