简体   繁体   English

如何从php mySQL查询中回显一组值?

[英]How can I echo an array of values from a php mySQL query?

Basically I am attempting to make a login. 基本上我正在尝试登录。 I understand a very small amount of php, but everytime I try to log in it works. 我理解一个非常少量的PHP,但每次我尝试登录它都有效。 So it is not following my if statement below. 所以它不遵循下面的if语句。 So I would like to see if anyone can help me print the $results as not a string. 所以我想看看是否有人可以帮我打印$ results而不是字符串。 Everytime I echo it, it says error can not print as string. 每次我回应它,​​它说错误不能打印为字符串。 Which makes me think its an array, can someone help ? 这让我觉得它是一个数组,有人可以帮忙吗? =( =(

<?php
include('include/dbConnection.php');

if (isset($_REQUEST['attempt']))
{
    //variables
    $user = $_POST['user'];
    $password = sha1($_POST['password']);

    //SQL statement
    $query = "SELECT COUNT(user) 
              FROM users
              WHERE user = '$user'
              AND password = '$password'";

    //Execute prepared MySQL statement
    $results = mysqli_query($dbc,$query) or die('Error querying database');
        /* Here is where I want to print $results

    if ($results = 1)
    {
        session_start();
        $_SESSION['$user'];
        header('location: home.php');
    }
    else
    {
        echo $results + 'Incorrect Username or Password';
    }

*/
    //Close dbConnect
    mysqli_close($dbc);
}

?>

使用var_dump($output)print_r($output)显示数组的内容。

You have to use this: 你必须使用这个:

echo "<pre>";
print_r($results);
echo "</pre>";

It first echoes so that the print of the array is formatted properly. 它首先回应,以便正确格式化阵列的打印。 If you don't do this it will all be on one line. 如果你不这样做,它将全部在一条线上。

Hope this helped! 希望这有帮助! :D :d

mysql_query() returns a statement result handle, NOT the data you've requested in the query. mysql_query()返回一个语句结果句柄,而不是您在查询中请求的数据。 You first have to fetch a row of data to get access to the actual query data: 首先,您必须获取一行数据才能访问实际的查询数据:

$result = mysqli_query(...);

$row = mysqli_fetch_row($result);
$count = $row[0];

mysqli_query function returns false for unsuccessful queries. 对于不成功的查询,mysqli_query函数返回false。 it returns a MySQLi_Result object for select or show queries and true for insert and update queries. 它为select或show查询返回MySQLi_Result对象,对于插入和更新查询返回true。

if your query fails for some reason your script will die because of or die statement and never returns false. 如果您的查询由于某种原因失败,您的脚本将因为or die语句而or die并且永远不会返回false。

if ($results = 1) 

statement assigns 1 to your result variable. statement为结果变量赋值1。 when your script runs, your code enters this if block. 当您的脚本运行时,您的代码将在此块中输入。 because you control the assignment statement whether it is done or not. 因为您控制赋值语句是否已完成。

your query is a select, mysqli_query function returns MySQLi_Result object. 你的查询是一个select,mysqli_query函数返回MySQLi_Result对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM