Previous Thread
Next Thread
Print Thread
Rate Thread
#242828 04/15/2003 6:02 PM
Joined: Apr 2003
Posts: 6
Lurker
Lurker
Offline
Joined: Apr 2003
Posts: 6
I'm using PHP 4.3.1 and I am learning PHP. I have a MySQL server running, a database called 'nistarr' that has a table 'jokes' and that table has one entry 'joketext' that I am simply trying to display to a webpage: Here is my php:

<?php
$mydatabase = nistarr;
$link = mysql_connect("localhost", "root", "MYPASSWORD")
or die("Could not connect: " . mysql_error());
$db = mysql_select_db($mydatabase)
or die("Count not connect: " . mysql_error());
mysql_close($link);
?>
<P> Here are all the jokes in our database: </P>
<BLOCKQUOTE>

<?php

// Request the text of all the jokes
$result = mysql_query("SELECT joketext FROM jokes")
or die("Invalid query: " . mysql_error());

}

while ($row = mysql_fetch_array($result)) {
echo("$row["joketext"]");
}

?>

Also, there is no error on the webpage, its just the data in the SQL server for 'joketext' isnt displayed. What have I done wrong?

michael

Sponsored Links
Joined: Apr 2002
Posts: 1,768
Addict
Addict
Offline
Joined: Apr 2002
Posts: 1,768
Try:

echo($row['joketext']);

1) There's no reason to quote that entire expression.
2) It's better to use single quotes unless you need to have variables interpolated.

If you needed to intepolate that variable, you would probably need braces {}, e.g.:

echo("joketext={$row['joketext']}<br />\n");

Joined: Apr 2003
Posts: 6
Lurker
Lurker
Offline
Joined: Apr 2003
Posts: 6
Both:

echo($row['joketext']);
and
echo("joketext={$row['joketext']}<br />\n");

arent working. I'm running 4.3.1 with register globals off.

do I need echo($_POST['joketext']);
or something similiar?

Joined: Apr 2002
Posts: 1,768
Addict
Addict
Offline
Joined: Apr 2002
Posts: 1,768
1) String not quoted:
$mydatabase = 'nistarr';

2) You're closing the connection before doing the query. That shouldn't be done until you're through with the database.
mysql_close($link);

3) Extra closing brace before the "while":
}

By the way, I was getting error messages shown when I ran this. You might have error message display turned off in your php.ini file. In that case, the errors should have shown up in your server error log.

It's also helpful to raise the error reporting level when debugging:
error_reporting(E_ALL);

Joined: Apr 2003
Posts: 6
Lurker
Lurker
Offline
Joined: Apr 2003
Posts: 6
You can be quite sure others, as well as myself had to laugh when you pointed out that I was closing the DB BEFORE trying to pull info out :/

Thanks Dave, works now.

Michael

Sponsored Links

Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
• Shock Hosting
• Stable Host
• bluehost
• InterServer
Visit us on Facebook
Member Spotlight
isaac
isaac
California
Posts: 1,156
Joined: July 2001
Forum Statistics
Forums63
Topics37,583
Posts293,957
Members13,826
Most Online151,614
Nov 14th, 2025
Today's Statistics
Currently Online 2063
Topics Created 0
Posts Made 0
Users Online 0
Birthdays 1
Top Posters
AllenAyres 21,080
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,834
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2026 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.1.0
(Snapshot build 20261026)