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
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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