Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php从mySQL表中获取一行数据?_Php_Mysql_Scripting - Fatal编程技术网

如何使用php从mySQL表中获取一行数据?

如何使用php从mySQL表中获取一行数据?,php,mysql,scripting,Php,Mysql,Scripting,下面是我所拥有的当前PHP代码的一个示例。我只想从表中获取一行,但它返回以下错误: Call to a member function fetch_assoc() on a non-object 如有任何见解,将不胜感激 $pathname = "C:\Users\BL\Documents\GitHub\Moozik\music"; $files = scandir($pathname); $server = "localhost"; $user = "root"; $pass = "";

下面是我所拥有的当前PHP代码的一个示例。我只想从表中获取一行,但它返回以下错误:

Call to a member function fetch_assoc() on a non-object
如有任何见解,将不胜感激

$pathname = "C:\Users\BL\Documents\GitHub\Moozik\music";
$files = scandir($pathname);
$server = "localhost";
$user = "root";
$pass = "";

while ($files[0] == "." || $files[0] == "..") {
    array_shift($files);
}

print_r($files);
$song = $pathname . '\\' . $files[0];

$conn = new mysqli($server, $user, $pass);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT song_path FROM song_data";
$result = mysqli_query($conn, $sql);

while($row = $result->fetch_assoc()) {
    $its = $row["song_path"];
    printf($its);
}

mysqli_close($conn);
你可以用

$row=mysqli_fetch_array($result)
您甚至不必使用while,因为您只想获取一行。如果要获取多行,则可以使用while。

您可以使用此选项

$row=mysqli_fetch_row($result)
它将从结果集中获取一行


希望这会有帮助

第1点:

You have mixed mysqli Object-oriented and Procedural methods...Use any one
$conn = new mysqli($server, $user, $pass); 
// Here You missed database to be selected
$result = mysqli_query($conn, $sql); // Used procedural method But Connection is by Object Oriented method
第2点:

You have mixed mysqli Object-oriented and Procedural methods...Use any one
$conn = new mysqli($server, $user, $pass); 
// Here You missed database to be selected
$result = mysqli_query($conn, $sql); // Used procedural method But Connection is by Object Oriented method
第3点:

You have mixed mysqli Object-oriented and Procedural methods...Use any one
$conn = new mysqli($server, $user, $pass); 
// Here You missed database to be selected
$result = mysqli_query($conn, $sql); // Used procedural method But Connection is by Object Oriented method
这是一个完整的面向对象方法

$conn = new mysqli($server, $user, $pass, $database);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT song_path FROM song_data";
$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {
           $its = $row["song_path"];
            echo $its;
    }


$conn->close();

我想这是一个开始!我现在收到以下消息
mysqli_fetch_row()期望参数1为mysqli_result
@vigneshAre您确定列和表的名称正确吗?真的@bayblade567?我相信所有这些都是正确的。我怎样才能重新检查代码?此表中当前只有一行数据。错误
mysqli_fetch_row()要求参数1为mysqli_result
表示结果无效。这意味着查询出了问题。请转到phpmyadmin,然后键入查询并检查它是否给出了结果……转到phpmyadmin,然后转到SQL,然后键入查询,然后执行……您能试试我的答案吗