Jump to content

Recommended Posts

Hi,

 

Wonder if someone could help with the problem below. Got a main page that includes a header, footer and a link to a database. I can get the two Includes to work but can't seem to display the database fields in the body of the page. What has me baffled is that it appears to be connecting to the database, (Opening the connection to the database server The connection worked. The link is: Resource id #3) but no text (see below for database text) is appearing on the screen. Yet I'm not getting a "page_not_found" message. I can go to the database in MYSQL console and query it successfully. Read and (hopefully) applied the section Debugging: A Beginner's Guide, but still can't see the problem. Any help greatly appreciated.

 

Bren

 

--------------------CODE FOR MAIN PAGE START-------------------

 

 

<?php

 

  $page_name = $_REQUEST['name'];

  /*http://localhost/exemples/page.php?name=about_us*/

 

// Get the body of the page

  mysql_connect("localhost","root","") or die(mysql_error());

  mysql_select_db("test") or die(mysql_error());

  $sql = "SELECT * from pages WHERE name='$page_name'";

 

print "Opening the connection to the database server<br>";

  $link = mysql_connect("localhost","root", "");

  print "The connection worked. The link is: $link <br>";

 

/*

--------------What is being selected from the database--------------

+----------+------------------------------------------------------------------------+

| name | body

|

+----------+------------------------------------------------------------------------+

| about_us | <h1>About Us</h1> ACME Toys has been established in 1850 to provide toys

| to children all over the world

+----------+------------------------------------------------------------------------+

1 row in set (0.00 sec)

----------------------------------------------------------------------

*/

  $result = mysql_query($sql) or die(mysql_error() );

  // If the page is not found, redirect to a static page

  if(mysql_num_rows($result) == 0 ) {

    header("Location: page_not_found.html");

  }

  $row = mysql_fetch_assoc( $result );

  $body = stripslashes( $row["body"] );

 

 

  // Include the header

include("c:/wamp/www/exemples/header.php");

  // Print the body of the page

echo $body;

  // Include the footer

  include("c:/wamp/www/exemples/footer.php");

 

?>

 

 

--------------------CODE FOR MAIN PAGE END-------------------

 

Link to comment
https://forums.phpfreaks.com/topic/211214-cant-display-text-from-mysql-on-page/
Share on other sites

I would first check to see what your error reporting is set to in your PHP.ini and set it to ALL if its not already... as for what I think may be causing a slight conflict is.. your throwing headers out after loading a page...

 

include("c:/wamp/www/exemples/header.php");

 

if something loads before that html wise the headers might break the script. But if the error reporting isn't set right you wont see the errors displaying to know what the issue is.

Monkeytooth,

 

Thanks for replying. I switched  Error Reporting to = E_ALL & E_NOTICE.

I got a message:  error: Call to undefined function mysql_connect() in C:\wamp\www\exemples\page.php on line 7.

 

I presumed (perhaps wrongly) that the code below would check to see if the database had in fact been connected to. Once I got an id number - which I was getting.

 

print "Opening the connection to the database server<br>";

  $link = mysql_connect("localhost","root", "");

  print "The connection worked. The link is: $link <br>";

 

Took your advice and tried echoing the main body of the page before opening  the  header and footer, then after, but to no avail. Any thoughts on this greatly appreciated.

 

 

Bren

 

You need to verify that your installation has MySQL support.

 

1. make a test page with the following.

testpage.php

<?php
phpinfo();
exit();
?>

 

Open this page and see if there is a section named MySQL.

 

2. IF IT DOESN"T, re-compile the installation with MySQL support.

 

3. IF IT DOES.

 

Open up the file php.ini and make sure the line to enable MySQL support is un-commented.

In Linux that line is:

extension=mysql.so

In Windows that line is:

extension=php_mysql.dll

 

Post back and let us know how it goes.

JC,

 

Following you advice:

 

1. The testpage.php worked.

 

2. extension=php_mysql.dl is uncommented.

 

3. You need to verify that your installation has MySQL support. I'm not sure about that last bit, or what to do if it hasn't. Here's some of the  printout from the PHP.INI page below.

 

Any help greatly appreciated.

 

Bren

---------------------------------------------------------------

mysqlMySQL Support enabled

Active Persistent Links 0

Active Links 0

Client API version mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $

Persistent cache enabled

put_hits 0

put_misses 0

get_hits 0

get_misses 0

size 2000

free_items 2000

references 2

 

Directive Local Value Master Value

mysql.allow_local_infile On On

mysql.allow_persistent On On

mysql.cache_size 2000 2000

mysql.connect_timeout 60 60

mysql.default_host no value no value

mysql.default_password no value no value

mysql.default_port no value no value

mysql.default_socket no value no value

mysql.default_user no value no value

mysql.max_links Unlimited Unlimited

mysql.max_persistent Unlimited Unlimited

mysql.trace_mode Off Off

 

 

mysqliMysqlI Support enabled

Client API library version mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $

Active Persistent Links 0

Inactive Persistent Links 0

Active Links 0

Persistent cache enabled

put_hits 0

put_misses 0

get_hits 0

get_misses 0

size 2000

free_items 2000

references 2

 

Directive Local Value Master Value

mysqli.allow_local_infile On On

mysqli.allow_persistent On On

mysqli.cache_size 2000 2000

mysqli.default_host no value no value

mysqli.default_port 3306 3306

mysqli.default_pw no value no value

mysqli.default_socket no value no value

mysqli.default_user no value no value

mysqli.max_links Unlimited Unlimited

mysqli.max_persistent Unlimited Unlimited

mysqli.reconnect Off Off

 

 

mysqlndmysqlnd enabled

Version mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $

Command buffer size 2048

Read buffer size 32768

Collecting statistics Yes

Collecting memory statistics Yes

-----------------------------------------------------------------------------------

 

 

 

 

 

Your symptom changed from the first post in the thread (where you were getting a valid link resource) to reply #2 (where you are getting a fatal runtime error.)

 

What is your current symptom/error and the corresponding code?

 

And you should always have error_reporting set to at least E_ALL and display_errors set to ON on your development system so that all the errors php detects will be reported and displayed.

jcbones - PFMaBiSmAd,

 

Thanks to both for your help. JC, The extension_dir is uncommented. So thanks for that.

 

PFMaBiSmAd,

 

E_ALL and display_errors is on. The error message I'm now getting is:

 

Notice: Undefined index: body in C\Wamp\www\exemples\page.php on line 33

 

That's what I don't get as the index seems to be referred to in the code correctly. That's why I think it's a connection problem, (but please correct me if I'm wrong). Thanks both for your help.

That error is probably referring to the line with - $row["body"]

 

That either means that the query matched zero rows or you don't have a column named body in your table. Since you are checking mysql_num_rows, it's likely that you don't have a column named body.

 

However, you do need an exit; statement after your header() redirect to prevent the rest of the code on the page from executing while the browser is requesting the new URL in the redirect.

 

You should also check in your logic if $page_name has a value before you blindly put it into a query and execute that query.

  • 2 weeks later...

PFMaBiSmAd,

 

 

As you can see from the output below there is a column called 'body' in the database. It's in a database called 'test'. Could the database be corrupt without showing a message to that effect? Or could it be a configuration problem?

 

 

/*

--------------What is being selected from the database--------------

+----------+------------------------------------------------------------------------+

| name | body

|

+----------+------------------------------------------------------------------------+

| about_us | <h1>About Us</h1> ACME Toys has been established in 1850 to provide toys

| to children all over the world

+----------+------------------------------------------------------------------------+

1 row in set (0.00 sec)

----------------------------------------------------------------------

*/

 

 

 

Regards,

 

Brendan

If the following is your current code immediately up to the point where the undefined index error is reported -

  $result = mysql_query($sql) or die(mysql_error() );
  // If the page is not found, redirect to a static page
  if(mysql_num_rows($result) == 0 ) {
     header("Location: page_not_found.html");
  }
  $row = mysql_fetch_assoc( $result );
  $body = stripslashes( $row["body"] );

 

And you do in fact have error_reporting set to E_ALL and there are no other errors being reported, then you must have a space or something as part of the 'xbodyx' column name, or you would not be getting that undefined index error.

PFMaBiSmAd,

 

Thank you, I Got It going! The row was titled 'Body'  (uppercase B), not  'body' - with a lowercase b. Even though in the  book I'm using  to learn PHP/Mysql it states that database and table names are not case sensitive in Microsoft Windows. Once again thanks to everyone who gave advice and had so much patience.

 

Kindest Regards,

 

Bren

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.