webweever Posted December 3, 2007 Share Posted December 3, 2007 I have a DB with 1 table and 3 fields in that table (name, url, cat). I'm trying to pull everything from the table only if the value of the cat field is php. The code I have is below, it works but the output is just a bunch of phphphphphphp. // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // This could be supplied by a user, for example $cat = 'php'; // Formulate Query // This is the best way to perform a SQL query // For more examples, see mysql_real_escape_string() $query = sprintf("SELECT cat FROM bookmarks WHERE cat='php'", mysql_real_escape_string($cat)); // Perform Query $result = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } // Use result // Attempting to print $result won't allow access to information in the resource // One of the mysql result functions must be used // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. while ($row = mysql_fetch_assoc($result)) { echo $row['name']; echo $row['url']; echo $row['cat']; } // Free the resources associated with the result set // This is done automatically at the end of the script mysql_free_result($result); Any ideas how I can fix this? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 3, 2007 Share Posted December 3, 2007 You didn't select anything from the database but the "cat", so change your query to this SELECT cat, name, url FROM bookmarks WHERE cat='php' Quote Link to comment Share on other sites More sharing options...
revraz Posted December 3, 2007 Share Posted December 3, 2007 Put <br /> after each echo if you want a carriage return after each. But you have other issues too in your code. May want to change where cat='php' to where cat = '$cat' You're setting a variable, might as well use it. Quote Link to comment Share on other sites More sharing options...
webweever Posted December 3, 2007 Author Share Posted December 3, 2007 Thanks for the help, that worked like a charm. Quote Link to comment 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.