PHP代码:尝试链接到MYSQL表[重复]

PHP代码:尝试链接到MYSQL表[重复]

问题描述:

This question already has an answer here:

I have a PHP file which is designed to link to a test SQL table which I've set up. The problem is that entering data into my search box isn't getting the results I'm after.

First, the PHP code:

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

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT ID, FirstName, LastName FROM 'create'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result)>0) {
    // output data of each row
    while($row=mysqli_fetch_assoc($result)) {
        echo "ID: " . $row["ID"]. " - Name: " . $row["FirstName"]. " " . $row["LastName"]. "<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?> 

And the SQL file, copied and pasted from the CSV version, in order of ID, FirstName, LastName...

1,"Ryan","Butler"
2,"Ryan","Butler"
3,"Brent","Callahan"
4,"Harry","Callahan","makemyday@dirtyharry.net"
5,"Luke","Cage","luke@mywifeisdead.com",
6,"Jessica","Jones","jessica@getkilgrave.org",

The result of entering any of the above words in my search box should give me a result of 6. Jessica. Jones. (I'm not bothering to search for the emails, I don't know why I included them.) Instead, I get "0 results" no matter what I type.

There's probably just one thing that I have wrong, but I'll be damned if I know what it is. Then again, PHP is not my forté.

</div>

此问题已经存在 这里有一个答案: p>

  • 何时在MySQL中使用单引号,双引号和反引号 12 answers span> li> ul> div>

    我有一个PHP文件 它旨在链接到我设置的测试SQL表。 问题是将数据输入我的搜索框并没有得到我想要的结果。 p>

    首先,PHP代码: p>

     &lt;?php 
     $ servername =“localhost”; 
     $ username =“username”; 
     $ password =“password”; 
     $ dbname =“database”; 
     
     //创建连接 
     $ conn = mysqli_connect($ servername,$ username,$ password,$ dbname); 
     //检查连接
    if(!$ conn){
     die(“连接失败:”。mysqli_connect_error()); \  n} 
     
     $ sql =“SELECT ID,FirstName,LastName FROM'create'”; 
     $ result = mysqli_query($ conn,$ sql); 
     
    if(mysqli_num_rows($ result)&gt; 0)  {
     //输出每行的数据
     while($ row = mysqli_fetch_assoc($ result)){
     echo“ID:”。  $行[ “ID”]。  “ - 名称: ” 。  $行[“名字”。  “”。  $行[“姓氏”。  “&lt; br&gt;”; 
    } 
    }其他{
     echo“0 results”; 
    } 
     
    mysqli_close($ conn); 
    ?&gt;  
      code>  pre> 
     
     

    从CSV版本复制并粘贴的SQL文件,按ID,FirstName,LastName ... p> 1, “瑞恩”, “管家” \ N 2, “瑞恩”, “管家” \ N3, “布伦特”, “卡拉汉” \ N4, “哈里”, “卡拉汉”,“makemyday @ dirtyharry。 净 “\ N5,” 卢克”, “笼”, “luke@mywifeisdead.com”,\ N6, “杰西卡”, “琼斯”, “jessica@getkilgrave.org” 代码> PRE>

    在我的搜索框中输入上述任何单词的结果应该会给我6. Jessica的结果。 琼斯。 (我不打算搜索电子邮件,我不知道为什么要包含它们。)相反,无论我输入什么,我都会得到“0结果”。 p>

    可能 只有一件事我错了,但如果我知道它是什么我会被诅咒。 再说一次,PHP不是我的强项。 p> div>

Your table name should be in backticks, not quotes.

$sql = "SELECT ID, FirstName, LastName FROM `create`";