Jump to content

[SOLVED] Query ouput problem


webweever

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/80035-solved-query-ouput-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.