Jump to content

I get nothing


Recommended Posts

I populated the database made sure I was connecting and get no display. Can someone please check this.

Thanks

 

<?php

$max_items = 5;

$db = mysql_connect('xxxx','xxxx','xxxx!');
mysql_select_db ('xxxx',$db);

function displayPartners($all = 0){
   
   global $db, $max_items;
   
   if ($all == 0){
      
      $query = "SELECT id,title" . "DATE_FORMAT(postdate, '%Y-%m-%d) as date" .
"FROM partners ORDER BY postdate DESC LIMIT $max_items";
}else{
   $query = "SELECT id,title" . "DATE_FORMAT(postdate, '%Y-%m-%d') as date " . "FROM partners ORDER BY postdate DESC";
   }

$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
   echo "<TABLE border = \"1\" width=\"300\>\n";
   $date = $row['date'];
   $title = htmlentities ($row['title']);
   
   echo "<TR><TD><b>$title</b>posted on $date</TD></TR>\n";
   echo "<TR><TD>$partners</TD></TR>\n";
   
   $comment_query = "SELECT count(*) FROM comment WHERE partners_id ={$row['id']}";
   $comment_result = mysql_query ($comment_query);
   $comment_row = mysql_fetch_row($comment_result);
   
   echo "<TR><TD><ahref=\"{$_SERVER['PHP_SELF']}" . "?action = show&id={$row['id']}\<Comments</a>($comment_row[0]}</TD></TR>\n)";

   echo "</TABLE>\n";
   echo "<BR>\n";   
}
if ($all == 0){
   
   echo "<ahref =\{SERVER_['PHP_SELF']}" . "?action=all\">View all Partners</a>\n;
   }
}
function displayOneItem($id){
   global $db;

   $query = 'SELECT * FROM partners WHERE id = $id';
   $result = mysql_query ($query);
   
   if (mysql_num_rows ($result) == 0){
      echo 'No partners found ';
      return;
   }
   $row = mysql_fetch_assoc($result);
   echo '<TABLE border =\"1\" width=\"300\>\n";
   $title = htmlentities ($row['title']);
   
   echo "<TR><TD><b>$title</b></TD></TR>\n";
   echo "<TR><TD>$partners<b>/TD></TR>\n";
   
   echo "</TABLE>\n";
   echo "<BR>\n";
   
   displayComments($id);
      
}
function displayComments($id){
   
   global $db;
   
   $query = "SELECT * FROM comment WHERE partners_id=$id";
   $result = mysql_query ($query);
   
   echo "Comments:<BR><HRwidth=\"300\">\n";
   
   while ($row = mysql_fetch_assoc ($result)){
      
      echo "<TABLE border=\"1\"width=\"300\">\n";
      
      $name = htmlentities ($row['name']);
      echo "<TR><TD><b>by:$name</b></TD></TR>\n";
      
      $comment = strip_tags ($row['comment'],'<a><b><i><u>');
      $comment = nl2br ($comment);
      
      echo "<TR><TD>$comment</TD></TR>\n";
      
      echo "</TABLE>\n";
      echo "<BR>\n";
   }
   echo "<HR width=\"300\">";
   echo "<FORM action=\"{$_SERVER['PHP_SELF']}" . "?action=addcomment&id=$id\"method=POST>\n";
   echo "Name: <input type=\"text\" " . "width=\"30\"name=\"name\"><BR>\n";
   
   echo "<TEXTAREA cols=\"40\" rows=\"5\" " . "name=\"comment\"></TEXTAREA><BR>\n";
   echo "<input type=\"submit\"name=\"submit\" " ."value=\"Add Comment\"\n";
   echo "</FORM>\n";
}
function addComment($id) {
   global $db;
   $query = "INSERT INTO comment " . "VALUES('',$id,'{$_POST['name']}'," . "'{$_POST['comment']}')";
   mysql_query ($query);
   
   echo "Comment submitted<BR>\n";
   echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id=$id\">Back</a>\n";
   }
   
   echo "<CENTER>\n";
   switch($_GET['action']) {
      
      case 'show':
      displayOneItem($_GET['id']);
      break;
   case 'all':
      displayPartners(1);
      break;
   case 'addcomment':
      addComment($GET['id']);
      break;
   default:
      displayPartners();
   }
}
?>

 

MOD EDIT:

 . . . 

BBCode tags added.

Link to comment
Share on other sites

When posting code, please enclose it within the forum's

 . . . 

BBCode tags.

 

How did you make sure you're connected to the database? There's no logic in that code that would report any errors that are encountered by mysql.

 

Do you have php's error_reporting set to an appropriate level, and display_errors = On? Seeing the errors is an important part of debugging code wile developing.

Link to comment
Share on other sites

i believe this

$query = "SELECT id,title" . "DATE_FORMAT(postdate, '%Y-%m-%d) as date" .
"FROM partners ORDER BY postdate DESC LIMIT $max_items";
}else{
   $query = "SELECT id,title" . "DATE_FORMAT(postdate, '%Y-%m-%d') as date " . "FROM partners ORDER BY postdate DESC";
   }

should be

$query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d) as date FROM partners ORDER BY postdate DESC LIMIT $max_items";
}else{
   $query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC";
   }

Link to comment
Share on other sites

When posting code, please enclose it within the forum's

 . . . 

BBCode tags.

 

How did you make sure you're connected to the database? There's no logic in that code that would report any errors that are encountered by mysql.

 

Do you have php's error_reporting set to an appropriate level, and display_errors = On? Seeing the errors is an important part of debugging code wile developing.

 

 

I have a connect.php file that gives me an error message if it doesn't connect. I ran that and everything seemed to be ok. No error message that is.

Link to comment
Share on other sites

But it appears you aren't using it in this script, and there's no error handling in there either, so you really don't know if there's a db connection or not. There are typos in the first query string, and where is the main function ever actually called?

Link to comment
Share on other sites

I wrote in a Mysql error and fixed first query.

 

<?php

$max_items = 5;

$db = mysql_connect('cccccc','-----','-------');
if (!$db)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db ('-----',$db);

function displayPartners($all = 0){

global $db, $max_items;

if ($all == 0){

$query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d) as date FROM partners ORDER BY postdate DESC LIMIT $max_items";
	}else{
$query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC";
}

$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
echo "<TABLE border = \"1\" width=\"300\>\n";
$date = $row['date'];
$title = htmlentities ($row['title']);

echo "<TR><TD><b>$title</b>posted on $date</TD></TR>\n";
echo "<TR><TD>$partners</TD></TR>\n";

$comment_query = "SELECT count(*) FROM comment WHERE partners_id ={$row['id']}";
$comment_result = mysql_query ($comment_query);
$comment_row = mysql_fetch_row($comment_result);

echo "<TR><TD><ahref=\"{$_SERVER['PHP_SELF']}" . "?action = show&id={$row['id']}\<Comments</a>($comment_row[0]}</TD></TR>\n)";

echo "</TABLE>\n";
echo "<BR>\n";	
}
if ($all == 0){

echo "<ahref =\{SERVER_['PHP_SELF']}" . "?action=all\">View all Partners</a>\n;
}
}
function displayOneItem($id){
global $db;

$query = 'SELECT * FROM partners WHERE id = $id';
$result = mysql_query ($query);

if (mysql_num_rows ($result) == 0){
	echo 'No partners found ';
	return;
}
$row = mysql_fetch_assoc($result);
echo '<TABLE border =\"1\" width=\"300\>\n";
$title = htmlentities ($row['title']);

echo "<TR><TD><b>$title</b></TD></TR>\n";
echo "<TR><TD>$partners<b>/TD></TR>\n";

echo "</TABLE>\n";
echo "<BR>\n";

displayComments($id);

}
function displayComments($id){

global $db;

$query = "SELECT * FROM comment WHERE partners_id=$id";
$result = mysql_query ($query);

echo "Comments:<BR><HRwidth=\"300\">\n";

while ($row = mysql_fetch_assoc ($result)){

	echo "<TABLE border=\"1\"width=\"300\">\n";

	$name = htmlentities ($row['name']);
	echo "<TR><TD><b>by:$name</b></TD></TR>\n";

	$comment = strip_tags ($row['comment'],'<a><b><i><u>');
	$comment = nl2br ($comment);

	echo "<TR><TD>$comment</TD></TR>\n";

	echo "</TABLE>\n";
	echo "<BR>\n";
}
echo "<HR width=\"300\">";
echo "<FORM action=\"{$_SERVER['PHP_SELF']}" . "?action=addcomment&id=$id\"method=POST>\n";
echo "Name: <input type=\"text\" " . "width=\"30\"name=\"name\"><BR>\n";

echo "<TEXTAREA cols=\"40\" rows=\"5\" " . "name=\"comment\"></TEXTAREA><BR>\n";
echo "<input type=\"submit\"name=\"submit\" " ."value=\"Add Comment\"\n";
echo "</FORM>\n";
}
function addComment($id) {
global $db;
$query = "INSERT INTO comment " . "VALUES('',$id,'{$_POST['name']}'," . "'{$_POST['comment']}')";
mysql_query ($query);

echo "Comment submitted<BR>\n";
echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id=$id\">Back</a>\n";
}

echo "<CENTER>\n";
switch($_GET['action']) {

	case 'show':
	displayOneItem($_GET['id']);
	break;
case 'all':
	displayPartners(1);
	break;
case 'addcomment':
	addComment($GET['id']);
	break;
default:
	displayPartners();
}
}
?>

Link to comment
Share on other sites

There's a closing single quote missing around the format string for DATE_FORMAT() in the first query. You also seem to have some string quoting issues as well.

 

if ($all == 0){

echo "<ahref =\{SERVER_['PHP_SELF']}" . "?action=all\">View all Partners</a>\n; // <--- MISSING CLOSING QUOTE
}
}
function displayOneItem($id){

//    [sNIP]    //

$row = mysql_fetch_assoc($result);
echo '<TABLE border =\"1\" width=\"300\>\n"; // <--- opening/closing quote mismatch (should be double quote at beginning)
$title = htmlentities($row['title']);

 

Then once those are corrected, you'll find that you have an extra closing curly brace at the end. After that, there are no more syntax errors, so make those changes and see if it has any affect.

Link to comment
Share on other sites

I can't win. Changed all typos then:

<CENTER>

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/20/7812420/html/test.php on line 25

<ahref =\{SERVER_['PHP_SELF']}?action=all">View all Partners</a>  :shrug:

 

<?php

$max_items = 5;

$db = mysql_connect('cccccc','-----','-------');
if (!$db)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db ('-----',$db);

function displayPartners($all = 0){

global $db, $max_items;

if ($all == 0){

$query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d) as date FROM partners ORDER BY postdate DESC LIMIT $max_items";
	}else{
$query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC";
}

$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
echo "<TABLE border = \"1\" width=\"300\>\n";
$date = $row['date'];
$title = htmlentities ($row['title']);

echo "<TR><TD><b>$title</b>posted on $date</TD></TR>\n";
echo "<TR><TD>$partners</TD></TR>\n";

$comment_query = "SELECT count(*) FROM comment WHERE partners_id ={$row['id']}";
$comment_result = mysql_query ($comment_query);
$comment_row = mysql_fetch_row($comment_result);

echo "<TR><TD><ahref=\"{$_SERVER['PHP_SELF']}" . "?action = show&id={$row['id']}\<Comments</a>($comment_row[0]}</TD></TR>\n)";

echo "</TABLE>\n";
echo "<BR>\n";	
}
if ($all == 0){

echo "<ahref =\{SERVER_['PHP_SELF']}" . "?action=all\">View all Partners</a>\n;
}
}
function displayOneItem($id){
global $db;

$query = 'SELECT * FROM partners WHERE id = $id';
$result = mysql_query ($query);

if (mysql_num_rows ($result) == 0){
	echo 'No partners found ';
	return;
}
$row = mysql_fetch_assoc($result);
echo '<TABLE border =\"1\" width=\"300\>\n";
$title = htmlentities ($row['title']);

echo "<TR><TD><b>$title</b></TD></TR>\n";
echo "<TR><TD>$partners<b>/TD></TR>\n";

echo "</TABLE>\n";
echo "<BR>\n";

displayComments($id);

}
function displayComments($id){

global $db;

$query = "SELECT * FROM comment WHERE partners_id=$id";
$result = mysql_query ($query);

echo "Comments:<BR><HRwidth=\"300\">\n";

while ($row = mysql_fetch_assoc ($result)){

	echo "<TABLE border=\"1\"width=\"300\">\n";

	$name = htmlentities ($row['name']);
	echo "<TR><TD><b>by:$name</b></TD></TR>\n";

	$comment = strip_tags ($row['comment'],'<a><b><i><u>');
	$comment = nl2br ($comment);

	echo "<TR><TD>$comment</TD></TR>\n";

	echo "</TABLE>\n";
	echo "<BR>\n";
}
echo "<HR width=\"300\">";
echo "<FORM action=\"{$_SERVER['PHP_SELF']}" . "?action=addcomment&id=$id\"method=POST>\n";
echo "Name: <input type=\"text\" " . "width=\"30\"name=\"name\"><BR>\n";

echo "<TEXTAREA cols=\"40\" rows=\"5\" " . "name=\"comment\"></TEXTAREA><BR>\n";
echo "<input type=\"submit\"name=\"submit\" " ."value=\"Add Comment\"\n";
echo "</FORM>\n";
}
function addComment($id) {
global $db;
$query = "INSERT INTO comment " . "VALUES('',$id,'{$_POST['name']}'," . "'{$_POST['comment']}')";
mysql_query ($query);

echo "Comment submitted<BR>\n";
echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id=$id\">Back</a>\n";
}

echo "<CENTER>\n";
switch($_GET['action']) {

	case 'show':
	displayOneItem($_GET['id']);
	break;
case 'all':
	displayPartners(1);
	break;
case 'addcomment':
	addComment($GET['id']);
	break;
default:
	displayPartners();
}
}
?>

Link to comment
Share on other sites

At least there aren't any parse errors. A query is failing, though

 

For debugging purposes while developing, you should output any errors generated by the database, along with the query string that generated them. You can do that in this manner, where $query would be changed to whatever variable holds the query string being executed. You would not want to echo all that information on a live, production site, however . . .

$result = mysql_query( $query ) or die ( "<br>Query string: $query<br>Produced error: " . mysql_error() . '<br>On line ' . __LINE__ );

Link to comment
Share on other sites

Here's the newest. :shrug:

 

<CENTER>

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/20/7812420/html/test.php on line 25

<ahref =\{SERVER_['PHP_SELF']}?action=all">View all Partners</a>

 

if ($all == 0){

echo "<ahref =\{SERVER_['PHP_SELF']}" . "?action=all\">View all Partners</a>\n";
}

 

Link to comment
Share on other sites

ok last post I'm going to sleep. Ran this and verified I am connected for sure.

 

<html>
<head>
<title>Listing every database, table, and field</title>
</head>
<body>
<?php
$user = "xxx";
$pass = "xxx";
$db = "x";
$link =  mysql_connect( "x", $user, $pass );
if ( ! $link )
    die( "Couldn't connect to MySQL" );

$db_res = mysql_list_dbs( $link );

while ( $db_rows = mysql_fetch_row( $db_res ) ) {
    print "<b>$db_rows[0]</b>\n";
    if ( !@mysql_select_db( $db_rows[0], $link ) ) {
        print "<dl><dd>couldn't connect -- " . mysql_error() ." </dl>";
        continue;
    }
    $tab_res = mysql_list_tables( $db_rows[0], $link );
    print "\t<dl><dd>\n";
    while ( $tab_rows = mysql_fetch_row( $tab_res ) ){
        print "\t<b>$tab_rows[0]</b>\n";
        $query_res = mysql_query( "SELECT * from $tab_rows[0]" );
        $num_fields = mysql_num_fields( $query_res );
        print "\t\t<dl><dd>\n";
        for ( $x=0; $x<$num_fields; $x++ ){
            print "\t\t<i>";
            print mysql_field_type( $query_res, $x );
            print "</i> <i>";
            print mysql_field_len( $query_res, $x );
            print "</i> <b>";
            print mysql_field_name( $query_res, $x );
            print "</b> <i>";
            print mysql_field_flags( $query_res, $x );
            print "</i><br>\n";
        }
        print "\t\t</d1>\n";
    }
    print "\t</d1>\n";
}
mysql_close( $link );
?>
</body>
</html>

 

then ran this again and got: <CENTER> <ahref =\{SERVER_['PHP_SELF']} ?action=all">View all Partners</a>

could it be the character codes or am i just butchering the code?

<?php

$db = mysql_connect('x,x,x');
if (!$db)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db ('x',$db);

$max_items = 5;

function displayPartners($all = 0){

global $db, $max_items;

if ($all == 0){

$query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC LIMIT $max_items";
	}else{
$query = "SELECT id,title, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM partners ORDER BY postdate DESC";
}


if (!$db)
while ($row = mysql_fetch_assoc($result)){
echo "<TABLE border = \"1\" width=\"300\>\n";
$date = $row['date'];
$title = htmlentities ($row['title']);

echo "<TR><TD><b>$title</b>posted on $date</TD></TR>\n";
echo "<TR><TD>$partners</TD></TR>\n";

$comment_query = "SELECT count(*) FROM comment WHERE partners_id ={$row['id']}";
$comment_result = mysql_query ($comment_query);
$comment_row = mysql_fetch_row($comment_result);

echo "<TR><TD><ahref=\"{$_SERVER['PHP_SELF']}" . "?action = show&id={$row['id']}\<Comments</a>($comment_row[0]}</TD></TR>\n)";

echo "</TABLE>\n";
echo "<BR>\n";	
}
if ($all == 0){

echo "<ahref =\{SERVER_['PHP_SELF']} ?action=all\">View all Partners</a>\n";
}
}
function displayOneItem($id){
global $db;

$query = 'SELECT * FROM partners WHERE id = $id';
$result = mysql_query ($query);

if (mysql_num_rows ($result) == 0){
	echo 'No partners found ';
	return;
}
$row = mysql_fetch_assoc($result);
echo "<TABLE border =\"1\" width=\"300\>\n";
$title = htmlentities ($row['title']);

echo "<TR><TD><b>$title</b></TD></TR>\n";
echo "<TR><TD>$partners<b>/TD></TR>\n";

echo "</TABLE>\n";
echo "<BR>\n";

displayComments($id);

}
function displayComments($id){

global $db;

$query = "SELECT * FROM comment WHERE partners_id=$id";
$result = mysql_query ($query);

echo "Comments:<BR><HRwidth=\"300\">\n";

while ($row = mysql_fetch_assoc ($result)){

	echo "<TABLE border=\"1\"width=\"300\">\n";

	$name = htmlentities ($row['name']);
	echo "<TR><TD><b>by:$name</b></TD></TR>\n";

	$comment = strip_tags ($row['comment'],'<a><b><i><u>');
	$comment = nl2br ($comment);

	echo "<TR><TD>$comment</TD></TR>\n";

	echo "</TABLE>\n";
	echo "<BR>\n";
}
echo "<HR width=\"300\">";
echo "<FORM action=\"{$_SERVER['PHP_SELF']}" . "?action=addcomment&id=$id\"method=POST>\n";
echo "Name: <input type=\"text\" " . "width=\"30\"name=\"name\"><BR>\n";

echo "<TEXTAREA cols=\"40\" rows=\"5\" " . "name=\"comment\"></TEXTAREA><BR>\n";
echo "<input type=\"submit\"name=\"submit\" " ."value=\"Add Comment\"\n";
echo "</FORM>\n";
}
function addComment($id) {
global $db;
$query = "INSERT INTO comment " . "VALUES('',$id,'{$_POST['name']}'," . "'{$_POST['comment']}')";
mysql_query ($query);

echo "Comment submitted<BR>\n";
echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id=$id\">Back</a>\n";
}

echo "<CENTER>\n";
switch($_GET['action']) {

	case 'show':
	displayOneItem($_GET['id']);
	break;
case 'all':
	displayPartners(1);
	break;
case 'addcomment':
	addComment($GET['id']);
	break;
default:
	displayPartners();
}
?>

 

 

Link to comment
Share on other sites

It's not the character codes, but I'm not understanding why you are using them all over the place.

 

Why not just this (errors fixed either way):

 

echo "View all Partners\n";

 

One other thing --- it's best not to use $_SERVER['PHP_SELF'].  Instead use basename($_SERVER['SCRIPT_FILENAME']).  There are numerous blogposts you can find about why PHP_SELF should not be trusted.

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.