简体   繁体   中英

mysql unknow error with select option

I'm trying to get everything from an table whit php/mysql but it won't work. I tried to search but i don't know what is making the error. I hope you can help me.

this the code:

    $servername = "localhost";      //Location Of Database - usually   it's "localhost" 
        $username = "root";             //Database User Name 
        $password = "";                 //Database Password 
        $dbname = "hr";                 //Database Name 

        $naam = mysql_real_escape_string($_POST['filler']);     
        $naam2 = mysql_real_escape_string($_POST['name']);

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

        $sql="SELECT * FROM antwoorden WHERE 'filler'='$naam' AND
            'name'='$naam2'";

        if ($conn->query($sql) === TRUE) {
            $row = $result->fetch_row();
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }

        $date = $row[2];
        $C_KVW = $row[5];
        $TL_KVW = $row[6];
        $C_AW = $row[7];
        $TL_AW = $row[8];
        $C_FB = $row[9];
        $TL_FB = $row[10];
        $C_FO = $row[11];
        $TL_FO = $row[12];
        $C_SW = $row[13];
        $TL_SW = $row[14];
        $C_WC = $row[15];
        $TL_WC = $row[16];
        $C_ST = $row[17];
        $TL_ST = $row[18];
        $C_CF = $row[19];
        $TL_CF = $row[20];
        $C_OP = $row[21];
        $TL_OP = $row[22];
        $C_IN = $row[23];
        $TL_IN = $row[24];
        $C_NA = $row[25];
        $TL_NA = $row[26];
        $C_OB = $row[27];
        $TL_OB = $row[28];
        $gemcijf = $row[29];

        echo("De antwoorden zijn: <br/>".$date." & ".$C_KVW." & ".$TL_KVW." & ".$C_AW." & ".$TL_AW." & ".$C_FB." & ".$TL_FB." & ".$C_FO." & ".$TL_FO." & ".$C_SW." & ".$TL_SW." & ".$C_WC." & ".$TL_WC." & ".$C_ST." & ".$TL_ST." & ".$C_CF." & ".$TL_CF." & ".$C_OP." & ".$TL_OP." & ".$C_IN." & ".$TL_IN." & ".$C_NA." & ".$TL_NA." & ".$C_OB." & ".$TL_OB." & ".$gemcijf );

and the error i get:

Error: SELECT * FROM antwoorden WHERE 'filler'='bart' AND 'name'='willem'
De antwoorden zijn:
& & & & & & & & & & & & & & & & & & & & & & & & & 

You were mixing two APIs mysql and mysqli . Stop using deprecated mysql

So need to update your variables

$naam = mysqli_real_escape_string($conn,$_POST['filler']);     
$naam2 = mysqli_real_escape_string($conn,$_POST['name']);

No need of using quotes around the column names - filter or name . Instead you can use backticks

$sql="SELECT * FROM antwoorden WHERE `filler`='$naam' AND
        `name`='$naam2'";

No need of ' s around the column names - filter or name . You may use backticks instead -

$sql="SELECT * FROM antwoorden WHERE `filler`='$naam' AND
        `name`='$naam2'";

UPDATE

There is no more errors in the query. You are doing -

if ($conn->query($sql) === TRUE) {

Which checks for identical true with the return value of $conn->query($sql) which will be always false as it will return the resource object not true for successfull SELECT . You should do it like -

if ($conn->query($sql)) {

instead.

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

$conn->query($sql) will return false if there is any error regarding database or query.

in database field use this sign `

'filler'='bart' AND 'name'='willem'

replace this code

`filler`='bart' AND `name`='willem'

As b0s3 said: leave the ' s

And please start using some modern frameworks or at least PDO when connecting/querying your database etc.

PDO: http://php.net/manual/en/book.pdo.php

Medoo (lightweight php databsae framework): http://medoo.in/

GL =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM