otester Posted August 4, 2010 Share Posted August 4, 2010 I am currently trying create a sales system where it checks the user's username against the database to check whether they are in the list of buyers. The mysql query returns "Resource id #35", I need it to return the actual username (which I manually inserted into the database to test). PHP code that fetches from database: <?php $con = mysql_connect("x","x","x"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("x", $con); $check_buyer = "SELECT * FROM Buyers WHERE Buyer='x'"; $buyer = mysql_query($check_buyer); ?> Product page: <?php include("/home/x/public_html/scripts/buyer.php"); if ($user->data['user_id'] == ANONYMOUS) { echo 'To use ' . $product . 'you must be logged in!'; echo '<br /><a href="http://x/forum/ucp.php?mode=register">Register</a>'; echo ' or '; echo '<a href="http://x/forum/ucp.php?mode=login">Sign In</a>'; } elseif ($user->data['username_clean'] == $buyer) { echo "<h3>Welcome to x</h3>"; } else { echo "You need to buy this product to use it!"; echo $user->data['username_clean']; //test whether username is outputted correctly - which it did echo $buyer; //Fetched from mysql - returned "Resource id #35", not the desired username } ?> Any help would be great! Thanks, otester Link to comment https://forums.phpfreaks.com/topic/209837-resource-id-35-error/ Share on other sites More sharing options...
schilly Posted August 4, 2010 Share Posted August 4, 2010 $buyer = mysql_query($check_buyer); This is a result set of rows, not an array or a value. You need to use someone like mysql_fetch_array to get the data from it. Link to comment https://forums.phpfreaks.com/topic/209837-resource-id-35-error/#findComment-1095331 Share on other sites More sharing options...
dolrichfortich Posted August 4, 2010 Share Posted August 4, 2010 It gives that error because $buyer is still a mysql resource. You still need to fetch the values from the resource like this. <?php $con = mysql_connect("x","x","x"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("x", $con); $check_buyer = "SELECT * FROM Buyers WHERE Buyer='x'"; $buyer = mysql_query($check_buyer); if(mysql_num_rows($buyer)) { $row = mysql_fetch_array($buyer); $buyer = $row['username']; //Change username to the correct field name in your database } else { $buyer = ''; } ?> Link to comment https://forums.phpfreaks.com/topic/209837-resource-id-35-error/#findComment-1095333 Share on other sites More sharing options...
otester Posted August 5, 2010 Author Share Posted August 5, 2010 It gives that error because $buyer is still a mysql resource. You still need to fetch the values from the resource like this. <?php $con = mysql_connect("x","x","x"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("x", $con); $check_buyer = "SELECT * FROM Buyers WHERE Buyer='x'"; $buyer = mysql_query($check_buyer); if(mysql_num_rows($buyer)) { $row = mysql_fetch_array($buyer); $buyer = $row['username']; //Change username to the correct field name in your database } else { $buyer = ''; } ?> Worked perfectly, thanks for your help! Link to comment https://forums.phpfreaks.com/topic/209837-resource-id-35-error/#findComment-1095378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.