|
|
|
|
Joined: Apr 2003
Posts: 6
Lurker
|
|
Lurker
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
|
|
|
|
|
Joined: Apr 2002
Posts: 1,768
Addict
|
|
Addict
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
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
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
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
|
|
|
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.
|
|
Posts: 1,156
Joined: July 2001
|
|
|
Forums63
Topics37,583
Posts293,957
Members13,826
| |
Most Online151,614 Nov 14th, 2025
|
|
Currently Online 2063
Topics Created 0
Posts Made 0
Users Online 0
Birthdays 1
|
|
|
|
|
|