justAnoob Posted May 5, 2009 Share Posted May 5, 2009 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 Name</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 Quote Link to comment https://forums.phpfreaks.com/topic/156877-solved-mysql-query-question/ Share on other sites More sharing options...
justAnoob Posted May 5, 2009 Author Share Posted May 5, 2009 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 Name</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 Name</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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/156877-solved-mysql-query-question/#findComment-826420 Share on other sites More sharing options...
premiso Posted May 5, 2009 Share Posted May 5, 2009 $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. Quote Link to comment https://forums.phpfreaks.com/topic/156877-solved-mysql-query-question/#findComment-826430 Share on other sites More sharing options...
Maq Posted May 5, 2009 Share Posted May 5, 2009 If you hardcoding a username works then it's probably the session. Have you echoed the session array to see what it contains? Do you have session_start at the top of all the scripts that use sessions? Quote Link to comment https://forums.phpfreaks.com/topic/156877-solved-mysql-query-question/#findComment-826447 Share on other sites More sharing options...
justAnoob Posted May 6, 2009 Author Share Posted May 6, 2009 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 Name</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???? Quote Link to comment https://forums.phpfreaks.com/topic/156877-solved-mysql-query-question/#findComment-827205 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 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'] . "'"); Quote Link to comment https://forums.phpfreaks.com/topic/156877-solved-mysql-query-question/#findComment-827237 Share on other sites More sharing options...
premiso Posted May 6, 2009 Share Posted May 6, 2009 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 Name</H3></td></tr>"; while ($row = mysql_fetch_array($sql)) It was late at night when I looked at it, easily missed Quote Link to comment https://forums.phpfreaks.com/topic/156877-solved-mysql-query-question/#findComment-827558 Share on other sites More sharing options...
justAnoob Posted May 6, 2009 Author Share Posted May 6, 2009 thanks premiso.... What exactly does the 0, 0 do??? Quote Link to comment https://forums.phpfreaks.com/topic/156877-solved-mysql-query-question/#findComment-827927 Share on other sites More sharing options...
premiso Posted May 6, 2009 Share Posted May 6, 2009 thanks premiso.... What exactly does the 0, 0 do??? mysql_result Fetches the first column from the first row. Quote Link to comment https://forums.phpfreaks.com/topic/156877-solved-mysql-query-question/#findComment-827936 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.