回声结果没有显示

时间:2016-07-22 21:39:15

标签: php mysql

我在一个数据库中有表,一个是外键到另一个。我的问题是我试图根据用户名调用存储在一个表中的信息,该用户名链接存储在另一个表中的2个表。这是我的PHP,请注意,我在数据库和php上非常新鲜,所以减少了一些空闲。这是我的代码:

<?php

$loaduser= $_SESSION['username'];
$loaduser_conn= @mysql_connect("DB_NAME","DB_USER","DB_PASS");

mysql_select_db("user_register") or die ("Couldn't find user database.");

$gal_result= mysql_query("SELECT shoot_name FROM images WHERE username='$loaduser'") or die (mysql_error());

while($row = mysql_fetch_assoc($gal_result,MYSQL_ASSOC))
{
    foreach($results['shoot_name'] as $result)
    {
        echo $result['shoot_name'], '<br>';

        if(mysql_num_rows($gal_result) !=1) {
            die("No galleries found for this user.");
        }

    }
}

3 个答案:

答案 0 :(得分:0)

  1. $ results必须在tryin获取其内容之前获取。

  2. 在尝试获取其内容时,您获取$ row而不是$ results

  3. 您正在使用mysql,不推荐使用mysql,请在将来尝试使用mysqli或PDO。

  4. 无论如何都要试试。

    <?php
    $loaduser= $_SESSION['username'];
    
    $loaduser_conn= @mysql_connect("DB_NAME","DB_USER","DB_PASS");
    mysql_select_db("user_register") or die ("Couldn't find user database.");
    
    $gal_result= mysql_query("SELECT shoot_name FROM images WHERE username='$loaduser'") or die (mysql_error());
    
    $results = mysql_fetch_assoc($gal_result,MYSQL_ASSOC)
    
    while($results = mysql_fetch_assoc($gal_result,MYSQL_ASSOC)){
      foreach($results['shoot_name'] as $result) {
    
      echo $result['shoot_name'], '<br>';
    
      if(mysql_num_rows($gal_result) !=1){
        die("No galleries found for this user.");
      }
    
      }
    }
    ?>
    

答案 1 :(得分:0)

尝试重写这个未经测试的代码段(如果您想继续使用已弃用的代码;)

$loaduser= $_SESSION['username'];
$loaduser_conn= @mysql_connect("DB_NAME","DB_USER","DB_PASS");
mysql_select_db("user_register") or die ("Couldn't find user database.");
$gal_result= mysql_query("SELECT shoot_name FROM images WHERE username='$loaduser'") or die (mysql_error());
if(mysql_num_rows($gal_result)==0){ // returns number of rows so if 0 there are no rows
    die("No galleries found for this user.");
}
while ($row = mysql_fetch_array($gal_result)) { // "while" is already your loop. No need for the foreach within.
    echo $row['shoot_name'], '<br>';
}

如果您想从多个表中选择一些参考,请尝试以下查询:

$gal_result= mysql_query("SELECT shoot_name FROM images AS a LEFT JOIN your_other_table AS b ON a.username = b.username WHERE a.username='$loaduser'") or die (mysql_error());

像任何人一样,我会建议您使用更新的代码。 有关详细信息,请参阅此处:http://php.net/manual/de/function.mysql-query.php

答案 2 :(得分:0)

您可以谷歌“PHP PDO教程”并找到许多资源。这是一篇非常清晰且写得很好的文章:例如:https://phpdelusions.net/pdo

这是一个更好的例子:

<?php

$loaduser = $_SESSION['username'];

// use PDO, not the deprecated mysql extension
$dsn = "mysql:host=localhost;dbname=user_register";
$conn = new PDO($dsn, "DB_USER", "DB_PASS");

// set exception errmode, so code dies automatically if there's an error
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// use a parameterized query instead of concatenating variables into SQL 
$sql = "SELECT shoot_name FROM images WHERE username = ?";
$stmt = $conn->prepare($sql);
$stmt->execute([$loaduser]);

// fetch all into an array of results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

// check the count of the results with < 1 instead of != 1
// in case it's 2 or more
if ($stmt->rowCount() < 1) {
    die("No galleries found for this user.");
}

// loop through results
foreach($results as $row) {
    // use dot for string concatenation instead of comma. 
    echo $row["shoot_name"] . "<br>";
}