Why doesn't it like this...
Hi guys, I am writing a simple (or so I thought) script in PHP with a MySQL back-end. There are two parts to the script, one adding information to the database and another that shows:
1) All of the information in the database,
2) Selected information within the database.
The error that I am recieving is a invalid MySQL argument on line 33. This is the while command. On my MySQL query when I added || die (mysql_error()) it told me that 'No database selected'.
I would appreciate it if you could help me correct this and get this script online and working.
Here is the script:
<?php
$db = @mysql_connect("localhost","a_gajic","mypassword");
mysql_select_db("articles");
include("header.inc");
if ($id) {
// Display each individual article
$q = mysql_query("SELECT * FROM articles WHERE id=$id") || die (mysql_error());
if ($h = mysql_fetch_array($q)) {
printf ("<p>%s written by <a href=\"mailto:%s\">%s</a></p>\n", $h[title], $h[email], $h[author]);
printf ("%s\n", $h["article"]);
}
}
else {
// Show a list of all contacts with hyperlinks to their full stats page
echo "<p>Please select an article that you wish to view.</p>";
// Start of the list
$q = mysql_query("SELECT * FROM articles ORDER BY article DESC") || die (mysql_error());
while($h = mysql_fetch_array($q)) {
printf ("<a href=\"%s?id=%s\">%s</a>\n", $PHP_SELF, $h[id], $h[article]);
echo "<br>";
}
}
include("footer.inc");
?>
Anyhelp will be appreciated, very much so.
Thanks,
Adam.
Mark Hensler posted this at 23:36 — 23rd October 2001.
He has: 4,048 posts
Joined: Aug 2000
Add more debug code to that..
<?php
$db = @mysql_connect(\"localhost\",\"a_gajic\",\"mypassword\");
if (!$db || !mysql_select_db(\"articles\")) {
echo \"\nError connecting to database.\n\";
}
include(\"header.inc\");
function Query_Error($file,$line,$query,$silent=true) {
/**
* takes the line and file where the query was built, and the query itself
* prints the debug string.
*
* if $silent, it will print in <!--comment tags-->
*/
echo \"\n\n\";
if ($silent) echo \"<!--\n\";
echo \"FILE: $file\n\";
echo \"LINE: $line\n\";
echo \"\n\";
echo \"QUERY ERROR:\n\";
echo mysql_error() . \"\n\";
echo \"\n\";
echo \"QUERY:\n\";
echo $query . \"\n\";
if ($silent) echo \"-->\n\";
echo \"\n\";
}
if ($id) {
// Display each individual article
$query = \"SELECT * FROM articles WHERE id=$id\";
$result = mysql_query($query);
if (!$result) {
Query_Error(__FILE__, __LINE__, $query, true);
}
if ($result && $h = mysql_fetch_array($result)) {
printf (\"<p>%s written by <a href=\\"mailto:%s\\">%s</a></p>\n\", $h[title], $h[email], $h[author]);
printf (\"%s\n\", $h[\"article\"]);
}
}
else {
// Show a list of all contacts with hyperlinks to their full stats page
echo \"<p>Please select an article that you wish to view.</p>\";
// Start of the list
$query = \"SELECT * FROM articles ORDER BY article DESC\";
$result = mysql_query($query);
if (!$result) {
Query_Error(__FILE__, __LINE__, $query, true);
}
while($result && $h = mysql_fetch_array($result)) {
printf (\"<a href=\\"%s?id=%s\\">%s</a>\n\", $PHP_SELF, $h[id], $h[article]);
echo \"<br>\";
}
}
include(\"footer.inc\");
?>
As far as DB connection, double check that your host/user/pass are correct and your db name. Case sensitve!
Mark Hensler
If there is no answer on Google, then there is no question.
a_gajic posted this at 12:16 — 24th October 2001.
They have: 71 posts
Joined: Aug 2001
THANKS MARK!!!!!!!!!!!!!!!!!!!
its working
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.