php - Echo results not showing

225

I have tables in one db, and one is foreign keyed to the other. My problem is that I'm trying to call up information stored in one table based on the user name which links the 2 tables stored in the other. Here is my php, mind you I'm pretty fresh on the databasing and php, so cut some slack. Here is my code:

<?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.");
        }

    }
}
898

Answer

Solution:

You can google "PHP PDO tutorial" and find many resources. Here's one that is very clear and well written: like: https://phpdelusions.net/pdo

Here's a better example:

<?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>";
}
31

Answer

Solution:

  1. $results has to be fetched before tryin to get its contents.

  2. While trying to get its contents you were fetching $row instead of $results

  3. You are using mysql, mysql has been deprecated try using mysqli or PDO in the future.

Try this anyway.

<?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.");
  }

  }
}
?>
523

Answer

Solution:

Try rewriting to this untested snippet (in case you want to keep using deprecated code ;))

$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>';
}

If you want to select from multiple tables with some reference try this query:

$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());

Like anyone i would advise you on using more up to date code. See here for more info: http://php.net/manual/de/function.mysql-query.php

People are also looking for solutions to the problem: php - Limit in foreach?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.