• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

php block poduces no output - incuding error!

new to PHP, trying to test a php page with db connection to mySQL database, there is zero outptu produced by the code, see below, not even an error statement. if i include a simple print "inside php block"; on the first line it is out put but nothing afterwards produces any output

here is the code block

<?PHP

include ('connect.php');

$sql_cats = "select id,subject from cats order by subject ASC";
$get_cats = @mysql_query($sql_cats) or die("Could not get a list of categories from the database. <br>Error #". mysql_errno() . ": " . mysql_error());
$num_cats = mysql_num_rows($get_cats);

print "<h1>Articles</h1>";
if ($num_cats < 1) {
      print "<p>There are currently no articles and no categories.";
      if ($admin == "loggedIN") {
            print " Please start by adding a <a href=\"categories.phtml\">category</a>.";
      }
      print "</p>";
}


while ($row = mysql_fetch_array($get_cats)) {
$cat_id=$row['id'];
$subject=$row['subject'];
print "<h2>$subject</h2>";

if ($admin == "loggedIN") {
      print " <h2 class=\"subHead\">[ <a href=\"categories.phtml?cat_id=$cat_id\">edit</a> ]</h2>";
}

$sql_article = "select id,subject,created from articles where cats_id = $cat_id order by created ASC";
$get_article = @mysql_query($sql_article) or die("Could not get a list of articles from the database. <br>Error #". mysql_errno() . ": " . mysql_error());
$num_articles = mysql_num_rows($get_article);

if ($num_articles < 1) {
      print "<p>There are currently no articles for " . $subject . "</p>";
}

print "<ul>";
while ($article = mysql_fetch_array($get_article)) {
$article_id = $article['id'];
$created = $article["created"];

// Find the date of the last comment added.
$sql_comments = "select created, name from comments where article_id = $article_id order by created DESC";
$get_comments = mysql_query($sql_comments) or die("Could not get a list of comments for this article from the database. <br>Error #". mysql_errno() . ": " . mysql_error());
$comments = mysql_fetch_array($get_comments);
$num_comments = mysql_num_rows($get_comments);

// MySQL time stamp format: YYYY-MM-DD HH:MM:SS
// Format the ARTICLE date into a human readable format
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $created, $bits)) {
      $date = date("M j, Y, g:i a", mktime($bits[4],$bits[5],$bits[6],$bits[2],$bits[3],$bits[1]));
}

// Format the COMMENT date into a human readable format
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $comments["created"], $bytes)) {
      $modified = date("M j, Y \a\\t g:i a", mktime($bytes[4],$bytes[5],$bytes[6],$bytes[2],$bytes[3],$bytes[1]));
}

print "<li><a href=\"comments.phtml?id=$article_id\"><strong>";
      print $article["subject"] . "</strong></a>";
      if ($admin == "loggedIN") {
            print " [ <a href=\"articles.phtml?article_id=$article_id\">edit</a> ]";
      }
      print "<br>Date added: $date";
      if ($num_comments > 1) {
            print "<br>Last comment added by <strong>".$comments["name"]."</strong> on $modified";
      }
print "</li>";

}
print "</ul>";

}


 ?>

this is the connect.php
<?PHP
      $db = mysql_connect("server" , "log" , "pass!");
      mysql_select_db("dbname",$db) or die("connect failed");
?>
0
James Rodgers
Asked:
James Rodgers
  • 9
  • 8
1 Solution
 
nplibCommented:
first Change the first line
$sql_cats = "select id,subject from cats order by subject ASC";
to
$sql_cats = "select id,subject from cats order by subject ASC;";

Add echo $num_cats; after $num_cats = mysql_num_rows($get_cats);

and see if it is actually getting any results.
0
 
nplibCommented:
also use
require("connect.php");
instead of include
0
 
James RodgersWeb Applications DeveloperAuthor Commented:
made recommended changes...no luck...still no output, i think the error is in the connect.php, a print line before it produces output but a print line after is not executed
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nplibCommented:
Change your connect.php file to this, then browse to it with your web browser and see what the output is.
<?php
    error_reporting(E_ALL ^ E_NOTICE);
    echo "Hello World";
    $db = mysql_connect("server" , "log" , "pass!") or die("connect failed");
    echo "Succeed";
?>
0
 
James RodgersWeb Applications DeveloperAuthor Commented:
all it says is hello world
0
 
nplibCommented:
Ok, so as I understand it,
you have a DNS entry called "server" that points to the mysql server?
and the username for that mysql server is "log"
and the password for the mysql server is "pass!"?

mysql_connect('IP_ADDRESS OR DNS_HOST_NAME', 'USERNAME', 'PASSWORD');

From what I see it is failing here, obviously.

So one of two things, The MySQL API hasn't been enabled in PHP or there is something wrong with your mysql_connect statement.

Can you ping "server" from the command prompt of the server that this php file resides.

SHELL>ping server

Can you login with the credentials log, pass! from another sql query client such as MySQL Query Browser

And does the use log have access rights to logon from the webserver's IP Address

If the two servers are the same you could try

mysql_connect('localhost', 'log', 'pass!');
0
 
James RodgersWeb Applications DeveloperAuthor Commented:
the api is enabled, befoire i enabled it i was getting errors about mysql_connect not being understood, had to uncomment teh enable mysql line in the php ini

.....ah, just thought of something....
this database was migrated from aremote machine to the current development machine, i just assumed that teh log and pass would be kept in the migration, am i wrong? do i need to reset them in the migrated db?
0
 
nplibCommented:
The username and password not be migrated over if only the database was migrated over.

In MySQL  there is a MySQL database that stores all the usernames and passwords, unless that was also migrated over, log and pass do not exists.
0
 
James RodgersWeb Applications DeveloperAuthor Commented:
sorry...must be something wroing with teh server, i cannot connect to any account on the system, keeps saying unable to connect to mysql server on localhost(10061) when i run teh ping, it pings ok, so it must be the mysql installation...time to uninstall/reinstall...
0
 
nplibCommented:
make sure the service is running before going through that headache.
0
 
James RodgersWeb Applications DeveloperAuthor Commented:
ok, reinstalled, the service is running, i can see teh db in teh administrator,i can run queries in the query browser, but this
<?php
    error_reporting(E_ALL ^ E_NOTICE);
    echo "Hello World";
    $db = mysql_connect("server" , "log" , "pass!") or die("connect failed");
    echo "Succeed";
?>
still only says 'Hello World', no error and no 'Succeed'
i have tried it as
$db = mysql_connect("localhost , "pass!") or die("connect failed");
$db = mysql_connect("127.0.0.1", "pass!") or die("connect failed");
and
$db = mysql_connect(127.0.0.1, "pass!") or die("connect failed"); (this didnt even give 'Hello World')
0
 
nplibCommented:
None of those other ones tried have the password in it.

And the last one is totally incorrect and would break your script.
Did you try it like this?
$db = mysql_connect("localhost", "log", "pass!") or die("connect failed");
0
 
James RodgersWeb Applications DeveloperAuthor Commented:
yep, sorry all teh attemps had the log and pass in them. typo
since then i have set the userid up in the mySQL admin and still cannnot connect to teh db....
0
 
nplibCommented:
try something totally obscure like

$db = mysql_connect("192.192.192.192", "bob", "ha") or die("Connect failed");

see if you get anything.
0
 
James RodgersWeb Applications DeveloperAuthor Commented:
found the problem.....
duh...
i had the extension for mysql active in the php.ini, but the extension directory was not set
with the code provided above i got 'connection failed'
when i replaced itr with teh actual code i got 'succeed'
0
 
James RodgersWeb Applications DeveloperAuthor Commented:
thanks for all your help and patience
0
 
nplibCommented:
No problem.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now