Jump to content

Add press hit as archive.php?id=$id


careym1989

Recommended Posts

Hello, I'm here to bother you guys again!

 

When I use my homegrown CMS and add a press hit, it enters it into the MySQL database "press"

 

Is there anyway I could have all of the information stored under that $id in the database be displayed on a page.

 

Perhaps, archive.php?id=$id of record in database?

 

A tutorial or a code snippet would be great. I assume I'm using the $_GET variable but I'm still learning.

 

Regards,

Carey

Link to comment
https://forums.phpfreaks.com/topic/125030-add-press-hit-as-archivephpidid/
Share on other sites

<?php
// includes
include("../template/conf.php");
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$Id = mysql_escape_string($_GET['Id']);
$query = "SELECT * FROM blog WHERE Id = '$Id'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// get resultset as object
$row = mysql_fetch_object($result);
// print details
if ($row)
{
?>

Echo out the content here
<?php
}
else
{
?>
<p>
<font size="-1"> could not be located in our database.</font>
<?php
}

// close database connection
mysql_close($connection);
?>

Hey man,

 

Thanks for the speedy reply, but:

 

I've changed all the information (connect, database, table, etc) and I've fiddled with the code a little bit, but all it's echoing back to me is "could not be located in our database."

 

Regards,

Carey

you need conf.php where that include is it includes the connection details

replace your include with this

<?php
// conf.php - configuration parameters
// database configuration
$host = "localhost";
$user = "Username";
$pass = "Password";
$db = "Database";
?> 


Okay. I've fixed the record doesn't exist problem.

 

Now, it displays nothing. I did alternate the code a little bit:

 

<?php
// includes
include("includes/conf.php");
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$id = mysql_escape_string($_GET['id']);
$query = "SELECT * FROM press WHERE id = '$id'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// get resultset as object
$row = mysql_fetch_object($result);
// print details
if ($row)
{

echo "$row[id]";

} else {

echo "This record does not exist in our database. Go back and try again!";

}
mysql_close($connection);
?>

 

EDIT: I added in:

 

error_reporting(E_ALL);

 

And there are no errors.

For some reason  I can't modify my previous post. My current issue is that when I visit the page that I used the script in, nothing is displayed. Error reporting is on ALL and no errors come up.

 

Help would be appreciated.

$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// get resultset as object

echo mysql_num_rows($result); // add this to see if you have any data returned from the query.

$row = mysql_fetch_object($result);
// print details

 

Note the mysql_num_rows line.... this will tell you if you are getting results from the database. Post back with the result. It will be a number 0 or greater.

Here it is:

 

<?php
error_reporting(E_ALL);
// includes
include("includes/conf.php");
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$id = mysql_escape_string($_GET['id']);
$query = "SELECT * FROM press WHERE id = '$id'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

echo mysql_num_rows($result); // add this to see if you have any data returned from the query.
// get resultset as object
$row = mysql_fetch_object($result);
// print details
if ($row)
{
echo "$row[id]"; // obviously I want more to show up, this was just a test to see if it was working. Doesn't echo record id back.
} else {
echo "This record does not exist in our database. Go back and try again!";
}
mysql_close($connection);
?>

$row = mysql_fetch_object($result);
// print details
if ($row)
{
echo "$row[id]"; // obviously I want more to show up, this was just a test to see if it was working. Doesn't echo record id back.
} else {
echo "This record does not exist in our database. Go back and try again!";
}

 

Here is your problem.... your using mysql_fetch_object and then trying to display the result as an array. I suggest doing this..

 

while($row = mysql_fetch_object($result))
{
     echo $row->id.'<br>';
}

 

This basically is saying while there are rows to fetch as objects, echo them.

 

take a look at the manual at the mysql_fetch_* functions. There are several of them. Your trying to echo the data as if you used mysql_fetch_assoc();

 

 

Nate

 

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.