0

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:
& & & & & & & & & & & & & & & & & & & & & & & & & 
6
  • 1
    removethe single quote from 'filler' and 'name' Commented Jun 5, 2015 at 11:55
  • 1
    Use prepared statement and avoid issues with quotes! Commented Jun 5, 2015 at 11:55
  • You are mixing mysqli with mysql maybe you should look at that.... Commented Jun 5, 2015 at 11:58
  • where are I mixing them? srry i'm new at mysql Commented Jun 5, 2015 at 12:01
  • @yolo-lol $naam = mysql_real_escape_string($_POST['filler']); Commented Jun 5, 2015 at 12:02

4 Answers 4

2

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'";
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Error: SELECT * FROM antwoorden WHERE filler='bart' AND name='willem'
0

in database field use this sign `

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

replace this code

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

Comments

0

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 =)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.