MySQL query not working while using php variable in where clause

后端 未结 3 1123
忘了有多久
忘了有多久 2021-01-23 16:02

I\'m new to PHP and MySQL. I am trying to make a simple search form using which I would want to show the results from the database based on the input text entered in the form. M

相关标签:
3条回答
  • 2021-01-23 16:21

    This should work:

    $query = "SELECT * FROM cats WHERE name = '$name'";

    Also, when you call "exit;" in the following block it will cancel the execution of the rest of your script:

        if (!empty($results)){
         echo "query successful" ;
         exit;
        }
    
    0 讨论(0)
  • 2021-01-23 16:45

    You're escaping the $ in the variable by doing \$. Try:

    $query = "SELECT * FROM `cats` WHERE name='$name'";
    

    EDIT

    From the discussion below.

    The problem with the undefined index is the fact that you are using $row['age'] when really, the column name in the database is Age. Therefore you must use $row['Age'] when referring to the item. The same goes for name.

    0 讨论(0)
  • 2021-01-23 16:48
    $query = "SELECT * FROM `cats` WHERE name='" . $name . "'";
    

    or without concatenation

    $query = "SELECT * FROM `cats` WHERE name='$name'";
    
    0 讨论(0)
提交回复
热议问题