Jump to content

[SOLVED] mysql query question


justAnoob

Recommended Posts

i have two tables in mysql,,,, one table has "id" and "username" and the other table has "user_id" "imgpath" and "item_name"

Do you see what I'm trying to accomplish below????

<?php
include "connection.php";
$findit = $_SESSION['id'];
$result = mysql_query("SELECT id FROM members WHERE username = '$findit'");
$foundit = $result
$sql = mysql_query("SELECT imgpath, item_name FROM member_trades WHERE user_id = $foundit");
echo "<table border='0' CELLPADDING=5 STYLE='font-size:16px'>";
echo "<table border=1><tr><td><H3>Image </h3></td> <td><H3>Item&nbspName</H3></td></tr>";
while ($row = mysql_fetch_array($sql))
{
   echo "<tr><td align='center'>";
   echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />';
   echo "</td><td align='center'>";
   echo $row['item_name'];
   echo "</td></tr>";
}
echo "</table>";
?>

I'm looking to dispaly all the pictures from whatever user is signed in. See anything that is wrong??? The error I get is "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result "

Everything seems right

Link to comment
Share on other sites

i changed it around a little and put in a user_id directly into script and it works fine.

<?php
include "connection.php";
$findme = $_SESSION['id'];
$query = "SELECT id FROM members WHERE username = '$findme'";
$result = mysql_query($query);
$sql = mysql_query("SELECT imgpath, item_name FROM member_trades WHERE user_id = '20'");
echo "<table border='0' CELLPADDING=5 STYLE='font-size:16px'>";
echo "<table border=1><tr><td><H3>Image </h3></td> <td><H3>Item&nbspName</H3></td></tr>";
while ($row = mysql_fetch_array($sql))
{
echo "<tr><td align='center'>";
echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />';
echo "</td><td align='center'>";
echo $row['item_name'];
echo "</td></tr>";
}
echo "</table>";
?>

 

But when I go back to the variable,,,, it doesn't show the info.

<?php
include "connection.php";
$findme = $_SESSION['id'];
$query = "SELECT id FROM members WHERE username = '$findme'";
$result = mysql_query($query);
$sql = mysql_query("SELECT imgpath, item_name FROM member_trades WHERE user_id = '$result'");
echo "<table border='0' CELLPADDING=5 STYLE='font-size:16px'>";
echo "<table border=1><tr><td><H3>Image </h3></td> <td><H3>Item&nbspName</H3></td></tr>";
while ($row = mysql_fetch_array($sql))
{
echo "<tr><td align='center'>";
echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />';
echo "</td><td align='center'>";
echo $row['item_name'];
echo "</td></tr>";
}
echo "</table>";
?>

Link to comment
Share on other sites

$query = "SELECT id FROM members WHERE username = '$findme'";
$result = mysql_query($query) or die("SQL WAS: {$query}<br />ERROR WAS: " . mysql_error());

 

See what the shows you. Causes there is an error in your sql.

Link to comment
Share on other sites

I changed the script around a little,, but still the same basic idea.... Still can't get it to work... Something so simple,, it's killing me.... Here is the revised script... The table appears but no images or info.

<?php
session_start();
include "connection.php";
$findit = $_SESSION['id'];
$query = mysql_query("SELECT id FROM members WHERE username = '$findit'");
$result = mysql_query($query);
$sql = mysql_query("SELECT imgpath, item_name FROM member_trades WHERE user_id = '$result'"); //this line
echo "<table border='0' CELLPADDING=5 STYLE='font-size:16px'>";
echo "<table border=1><tr><td><H3>Image </h3></td> <td><H3>Item&nbspName</H3></td></tr>";
while ($row = mysql_fetch_array($sql))
{
echo "<tr><td align='center'>";
echo '<img src="' . $row['imgpath'] . '" width="125" alt="" />';
echo "</td><td align='center'>";
echo $row['item_name'];
echo "</td></tr>";
}
echo "</table>";
?>

 

I echoed out $findit and the username appears,,, which is great,,, that works. session_start is there.

I changed the commented line to the line below(1 is just a member id)and it echoes that table with the pics.

<?php
$sql = mysql_query("SELECT imgpath, item_name FROM member_trades WHERE user_id = '1'");
?>

So something is not working right.....

My tables are set up the following....

table "members"    id    username

table "member_trades"  userid    imgpath  item_name

 

Can you spot the prob????

 

Link to comment
Share on other sites

Wow 2 Gurus missed this one?

 

$result = mysql_query($query);
$sql = mysql_query("SELECT imgpath, item_name FROM member_trades WHERE user_id = '$result'");

You see nothing wrong with that? $result is a mysql resource.

 

I think you meant:

$result = mysql_query($query);
$sql = mysql_query("SELECT imgpath, item_name FROM member_trades WHERE user_id = '" . $result['id'] . "'");

Link to comment
Share on other sites

I think you meant:

$result = mysql_query($query);
$sql = mysql_query("SELECT imgpath, item_name FROM member_trades WHERE user_id = '" . $result['id'] . "'");

 

And I think you meant

// remove and combine below $query = mysql_query("SELECT id FROM members WHERE username = '$findit'");
// remove and combine like below $result = mysql_query($query);
$result = mysql_query("SELECT id FROM members WHERE username = '$findit'");

 

So this is what the first part of the code should look like for it to work

<?php
session_start();
include "connection.php";
$findit = $_SESSION['id'];
$result = mysql_query("SELECT id FROM members WHERE username = '$findit'");
$result = mysql_result($result, 0, 0);
$sql = mysql_query("SELECT imgpath, item_name FROM member_trades WHERE user_id = '$result'");
echo "<table border='0' CELLPADDING=5 STYLE='font-size:16px'>";
echo "<table border=1><tr><td><H3>Image </h3></td> <td><H3>Item&nbspName</H3></td></tr>";
while ($row = mysql_fetch_array($sql))

 

It was late at night when I looked at it, easily missed ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.