Ok, have a mysql database which has some information about software reviews (which users have inputed via a form).

I've got two scripts which show a list of links (links.php) and then from the links a variable is passed called id - the id holds the contents of the links. This id var is passed to showlinks.php which is meant to call up all the fields in the clicked row (making sense, or lost yet?)

The main part of links.php looks like this:
$result = mysql_query("SELECT date FROM review");

$num_rows = mysql_num_rows ($result);
print "<table border=0>\n";
while ($a_row = mysql_fetch_row($result))
{
print "<tr>\n";

	foreach ($a_row as $field)


					print "\t<td>" ?>
					<? print"<a href=\"showlinks.php?id=$a_row[0]\">$field</a>";?>
					<? print "</td>\n"; ?> 
					<?print "</tr>\n";
		}
print "</table>\n"; 

which works fine, then showlinks.php is mainly this:

<? $result = mysql_query ("SELECT * WHERE date=id");




	print "<table border=1>\n";
	print $result;
	while ($a_row = mysql_fetch_row($result))
				{
				print "<tr>\n";
				foreach ($a_row as $field )

								print "\t<td>$field</td>\n";
								print "</tr>\n";

								}

				print "</table>\n";
				mysql_close($link);



	?> 

The error

(Warning: Supplied argument is not a valid MySQL result resource in c:\apache\htdocs\ltphp\showlinks.php on line 32)

seems to come from the mysql_fetch_row, which is driven by $result, which in turn is created from the mysql_query - can anyone see any problems with this, which would produce these errors?

The only one that I can think of is the fact that id has a space in it, but that shouldn't stop it?

Any ideas?

Thanks in advance

    Your select statement doen't say from which table.
    But you still put better error checking in.

    Ace21

      Thanks guys - although I'm still not 100% on this.

      I have a variable passed to the showlinks.php page, which has one of the fields in it (it's a date).

      What I want to do, is search the DB for that field, and then display all the other entries in that row.

      Am I close, or miles away from this? I'm a relative newbie, but am trying to work things out.

      Thanks for any help

      Adam

        • [deleted]

        You're on the right track, but when 'they' say add errorchecking, that's what they mean: add errorchecking.

        Have a good long hard look at this query, find the two mistakes.

        SELECT * WHERE date=id

        The first is what Ace mentioned, there's no FROM clause

        And the second... dont you want to compare date with $id?

        Again, if you implemented error checking, PHP would have told you that there was first that there was an unknown column called 'id', and then it would have told you that your query was inclomplete.

          Okay - I get the point - be nice to me, as I'm only a newbie - but I am very grateful for your help.

          The latest is that I've bought a Mysql book (not cheap) to show that I'm serious about doing things properly. However, it only has a small chapter in blending mysql with php.

          I've managed to get the script to write the whole table to my browser.

          This is my query so far - which is so close now to being right - but I'm a little lost on the right syntax for the query -

          $result = ("SELECT * FROM review WHERE date ="$id");

          Getting warmer?

            • [deleted]

            Getting warmer, just lacking a little simple logic.

            First, why parenthesis around the string?
            Second, You define a string by putting quotes around it. That means you can't just put quotes inside the string too, PHP will get confused. Luckily, SQL prefers to have single-quotes inside the queries.
            Third, SQL doed not require nor desire quotes around numeric data, only around strings.

            $sQuery = "SELECT * FROM table WHERE date = $id";

            Tip: when using variables in strings, it helps to use concatenation:

            $sQuery = "SELECT * FROM table WHERE date = ".$id;

              I think part of the problem was that I hadn't supplied the query as a string, I was just trying to mysql_query ("SELECT... - which I can see now isn't the best way to do it.

              Still having problems - but I'll try and work it out before posting again - thanks for the support.

              Adam

                • [deleted]

                "I think part of the problem was that I hadn't supplied the query as a string,"

                that was not the problem. You PHP syntax was just wrong.

                However, it is a good idea to always put your query into a varialbe before you execute it, so you can print it in case of an error.

                I strongly (read: very strongly, expect a whipping if you don't) advise you to add error checking like in the link I posted earlier.

                  Thanks Vincent,

                  here's the code as it stands at the moment. I'm still getting a strange response (although not an error) where the $result is returned as null, even though id was created from an entry in the database (as a clickable link).

                  I've printed out $query to see what it is (another advantage of having it as a variable and it appears thus: SELECT * FROM review WHERE date = Aug2003 - and Aug2003 is an entry in the field date in my db.

                  Any ideas?

                  $query = "SELECT * FROM review WHERE date = ".$id;
                  print $query."<BR>";
                  if( $result = mysql_query ("$query"))
                  {
                  print $result;
                  while ($row = mysql_fetch_array($result))
                  {
                  print "$row[title]";
                  print "$row[author]";
                  print "$row[review]\n";
                  };

                  else {print "The query returned null";}
                  mysql_close($link);

                    • [deleted]

                    you should change the 'print "The query returned null"'
                    to
                    echo mysql_error();

                    that will give you some more information.

                    In this case if will probable tell you that you forgot to put quotes around the $id content.
                    Aug2003 is a string, not an integer, and as I said before, in SQL you must qoute strings, an not quote integers.

                    So you will have to put single quotes around the $id:

                    $sQuery = "SELECT * FROM table WHERE field='".$id."'";

                      That has done it - thanks Vincent!

                      As for one more little problem, I'll raise that later today (forward slashes appearing where apostrophes are in D😎.

                      Thanks again.

                      Adam

                        Write a Reply...