0

I am trying to query mysql and get the result to be returned, but I am running into the following error when trying to check that the number of returned results is greater than zero. Here is my code:

    <body>

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

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

                $sql = "SELECT * FROM cdlist WHERE cdlist.title =" . $_POST["cd_name"]." OR cdlist.Artist=" .$_POST["artist_name"]. " OR cdlist.Price=" . $_POST["cd_price"] . " OR cdlist.Year=" . $_POST["year"];
                */
                $sql = "SELECT * FROM cdlist WHERE cdlist.Title=" . $_POST["cd_name"];

                //$sql = "SELECT * FROM cdlist";
                $result = mysqli_query($conn, $sql);
                $json_array = array();
                if (mysqli_num_rows($result)>0){
                    while($row = mysqli_fetch_assoc($result)){
                        $json_array[] = $row;
                    }

                    echo json_encode($json_array);
                }
                $conn->close();
            ?>

    </body>

Is there an issue with either of the queries that are in the php? I can only get it to work with SELECT * FROM cdlist, but that's not the query that I'm trying to use. I know for a fact that cd_name is the correct name for the variable as well. Thanks for any help you can provide me

hippoman
  • 187
  • 2
  • 10
  • 1
    Fixing your SQL injection issue will also fix the query. Use error reporting and the driver will tell you your exact issue. Hint, what would `WHERE cdlist.Title= nevermind` do? – user3783243 Apr 16 '19 at 02:44
  • and/or https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments – user3783243 Apr 16 '19 at 02:46
  • You need to enclose `$_POST["cd_name"]` in single quotes in your query i.e. `$sql = "SELECT * FROM cdlist WHERE cdlist.Title='" . $_POST["cd_name"] . "'";` – Nick Apr 16 '19 at 03:56

0 Answers0