mdmartiny Posted April 12, 2011 Share Posted April 12, 2011 I keep getting the mysql resource id #4 on a query that I am running and I have tried everything that I have read to fix it and nothing is working. I tried using the mysql_fetch_array, mysql_fetch_accoc, and the mysql_fetch_row functions I would appreciate any help that can be given to me. Here is my code as it stands now <?php include('includes/config.php'); $last = $_GET['l_name']; $first = $_GET['f_name']; $sql = 'SELECT * FROM `ttmautos` WHERE `l_name` LIKE \'$last\' AND `f_name` LIKE \'$first\''; $autographs = mysql_query($sql, $connection)or die(mysql_error()); $row = mysql_fetch_array($autographs); echo $sql; echo $autographs; $l_name = $row['l_name']; $f_name = $row['f_name']; $sent = $row['date_sent']; $return = $row['date_return']; $address = $row['address']; $isent = $row['item_sent']; $ireturn = $row['item_return']; $project = $row['project']; $team = $row['team']; $address = stripslashes($address); ?> Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/ Share on other sites More sharing options...
Pikachu2000 Posted April 12, 2011 Share Posted April 12, 2011 echo $autographs; <-- $autographs holds a query result resource. That is what is echoing the resource id. Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/#findComment-1200656 Share on other sites More sharing options...
mdmartiny Posted April 12, 2011 Author Share Posted April 12, 2011 It is not showing any of the fields that I am trying to get information from. I was echoing out the $autographs query to see what the results are. Every row should post information pertaining to that certain field onto a web page and I am not getting anything. Just the resource id Sorry if I was not clear enough in the first posting. Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/#findComment-1200662 Share on other sites More sharing options...
gizmola Posted April 12, 2011 Share Posted April 12, 2011 You're not reading the reply. A result set resource is not a variable you can do anything with. It is only used to support other functions that actually retrieve data from the result set (which is stored on the server). The variable in your script that actually has data is $row. Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/#findComment-1200667 Share on other sites More sharing options...
mdmartiny Posted April 12, 2011 Author Share Posted April 12, 2011 I modified the code to look like this now and I am still not getting any results.... What am I doing wrong? <?php include('includes/config.php'); $last = $_GET['l_name']; $first = $_GET['f_name']; $sql = 'SELECT * FROM `ttmautos` WHERE `l_name` LIKE \'$last\' AND `f_name` LIKE \'$first\''; $autographs = mysql_query($sql)or die(mysql_error()); while ($row = mysql_fetch_assoc($autographs)) { $l_name = $row['l_name']; $f_name = $row['f_name']; $sent = $row['date_sent']; $return = $row['date_return']; $address = $row['address']; $isent = $row['item_sent']; $ireturn = $row['item_return']; $project = $row['project']; $team = $row['team']; } $address = stripslashes($address); ?> Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/#findComment-1200673 Share on other sites More sharing options...
litebearer Posted April 12, 2011 Share Posted April 12, 2011 where are you echoing - $team, $project, $address etc etc? Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/#findComment-1200674 Share on other sites More sharing options...
mdmartiny Posted April 12, 2011 Author Share Posted April 12, 2011 where are you echoing - $team, $project, $address etc etc? I am doing it further down in the page. That was just the script that I wrote. Here is the full code of everything <?php include('includes/config.php'); $last = $_GET['l_name']; $first = $_GET['f_name']; $sql = 'SELECT * FROM `ttmautos` WHERE `l_name` LIKE \'$last\' AND `f_name` LIKE \'$first\''; $autographs = mysql_query($sql)or die(mysql_error()); while ($row = mysql_fetch_assoc($autographs)) { $l_name = $row['l_name']; $f_name = $row['f_name']; $sent = $row['date_sent']; $return = $row['date_return']; $address = $row['address']; $isent = $row['item_sent']; $ireturn = $row['item_return']; $project = $row['project']; $team = $row['team']; } $address = stripslashes($address); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Michael48060's Autographs - <?php echo "$f_name"; ?> <?php echo "$l_name"; ?> Autograph</title> </head> <body> <div id="wrapper"> <h6>Information</h6> <ul> <li><span class="header_two">Player Name</span> – <?php echo "$f_name"; ?> <?php echo "$l_name"; ?></li> <li><span class="header_two">date sent</span> – <?php echo "$sent"; ?></li> <li><span class="header_two">date return</span> – <?php echo "$return"; ?></li> <li><span class="header_two">address used</span> – <?php echo "$address"; ?></li> <li><span class="header_two">item sent</span> – <?php echo "$isent"; ?></li> <li><span class="header_two">item return</span> – <?php echo "$ireturn"; ?></li> <li><span class="header_two">project</span> – <?php echo "$project"; ?></li> <li><span class="header_two">team</span> – <?php echo "$team"; ?></li> </ul> </div> <!--End #content--> </div> <!-- End #wrapper --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/#findComment-1200676 Share on other sites More sharing options...
Pikachu2000 Posted April 12, 2011 Share Posted April 12, 2011 Spotted another issue. You aren't getting any results with that query string. Variables aren't interpolated within a single-quoted string. Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/#findComment-1200680 Share on other sites More sharing options...
gizmola Posted April 12, 2011 Share Posted April 12, 2011 Good catch by Pikachu2k. I have a link to this in my signature as well for similar reasons: http://www.flingbits.com/article/view/my-script-with-mysql-calls-doesn-t-work-why-1 Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/#findComment-1200688 Share on other sites More sharing options...
mdmartiny Posted April 12, 2011 Author Share Posted April 12, 2011 I changed the sql code and added the error message but I am still getting no results or errors <?php include('includes/config.php'); $last = $_GET['l_name']; $first = $_GET['f_name']; $sql = "SELECT * FROM ttmautos WHERE l_name LIKE '$last' AND f_name LIKE '$first' "; $autographs = mysql_query($sql); if (!$autographs) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } while ($row = mysql_fetch_assoc($autographs)) { $l_name = $row['l_name']; $f_name = $row['f_name']; $sent = $row['date_sent']; $return = $row['date_return']; $address = $row['address']; $isent = $row['item_sent']; $ireturn = $row['item_return']; $project = $row['project']; $team = $row['team']; } $address = stripslashes($address); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Michael48060's Autographs - <?php echo "$f_name"; ?> <?php echo "$l_name"; ?> Autograph</title> </head> <body> <div id="wrapper"> <h6>Information</h6> <ul> <li><span class="header_two">Player Name</span> – <?php echo "$f_name"; ?> <?php echo "$l_name"; ?></li> <li><span class="header_two">date sent</span> – <?php echo "$sent"; ?></li> <li><span class="header_two">date return</span> – <?php echo "$return"; ?></li> <li><span class="header_two">address used</span> – <?php echo "$address"; ?></li> <li><span class="header_two">item sent</span> – <?php echo "$isent"; ?></li> <li><span class="header_two">item return</span> – <?php echo "$ireturn"; ?></li> <li><span class="header_two">project</span> – <?php echo "$project"; ?></li> <li><span class="header_two">team</span> – <?php echo "$team"; ?></li> </ul> </div> <!--End #content--> </div> <!-- End #wrapper --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/#findComment-1200693 Share on other sites More sharing options...
gizmola Posted April 12, 2011 Share Posted April 12, 2011 This seems to indicate that your query is valid, but that the result set is empty. echo out your $sql and use a tool like phpMyAdmin to double check it. One other thing.. using the technique you're using, you will only ever see the last row, because you fetch all the rows in a loop, then only seem to go to display logic on the last row fetched. Just for quick testing, inside the fetch loop you could put in: var_dump($row); Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/#findComment-1200696 Share on other sites More sharing options...
mdmartiny Posted April 12, 2011 Author Share Posted April 12, 2011 Ok... I think I have figured out where the problem lies. It is in the $_Get function.. If I go in and type a persons name then the information shows up. Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/#findComment-1200702 Share on other sites More sharing options...
mdmartiny Posted April 12, 2011 Author Share Posted April 12, 2011 Thanks everyone for your help with this... The problem the whole time was the get function. Stupid Rookie mistake :-\ Quote Link to comment https://forums.phpfreaks.com/topic/233505-i-keep-getting-mysql-resource-id-4-not-sure-why/#findComment-1200707 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.