drpqxogph15436713 2016-01-15 19:46
浏览 15
已采纳

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>
  • 写回答

1条回答 默认 最新

  • douqiang7976 2016-01-15 19:48
    关注

    Your table name should be in backticks, not quotes.

    $sql = "SELECT ID, FirstName, LastName FROM `create`";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错