Justtechjobs.com Find a programming school near you






Online Campus Both


php3-list | 199903

Re: [PHP3] 1 is not a MySQL result index ? From: Richard Lynch (lynch <email protected>)
Date: 03/31/99

At 12:16 AM 3/31/99, Erik wrote:
>Hi. Help?
>
>I'm missing something ... The following connects to a mysql db, inserts
>strings set in a form page and should then echo back the current state of
>the page. Unfortunately it complains that "1 is not a MySQL result index"
>after I attempt to read the table. The writes to the table work fine and at
>this point there are a dozen or more lines there to be read. Could someone
>point out the error of my ignorant ways? Please?
>
>Erik
>stainsby <email protected>
>
>=================
>
><?
> echo $acctCode . "<BR>";
> echo $text . "<P>";
>
>$conn = MYSQL_CONNECT("localhost","nobody","") OR DIE("Unable to connect to
>database");
>if (! $conn) {
> echo "A server connection could not be established.\n";
> exit;
>}

You don't need both of these. In fact, if something goes wrong, the die()
guarantees that the if (!$conn) will never happen.

I like to use if (!$conn) so I can write a longer, more detailed message
than die(). Others like the simplicity of die(). Take your pick. But
using both just misleads you.

>$query = "INSERT INTO echo VALUES('$acctCode','$text')";
>$result = mysql_db_query("lets",$query,$conn);
>echo "INSERT affected: " . $result . "<BR>";

if (!$result){
  echo "insert failed.<BR>\n";
  echo mysql_errmsg();
  exit;
}
//or use the die() thing again.

>$second_query = "SELECT * FROM echo";
>$second_result = mysql_db_query("lets",$query,$conn);
>echo "SELECT result: " . $second_result . "<P>";

if (!$second_result){
  echo "select failed.<BR>\n";
  echo mysql_errmsg();
  exit;
}
//or use the die() thing again.

>$num = mysql_num_rows($second_result);
>echo "NumRows: " . $second_result . "<P>";

You should use $num here, not $second_result, to see the number of rows in
the table. $second_result is probably always 2 or 3. Each result set
increments the "result id" that you get back to use in later calls to
mysql_result.

>$i = 0;
>while ($i < $num) {
> echo "<TR><TD>";
> echo mysql_result($second_result, $i, "acctCode");
> echo "</TD><TD>";
> echo mysql_result($second_result, $i, "text");
> echo "</TD></TR>";
> $i++;
>}
>mysql_FreeResult($result);

//This is your actual error.
//An INSERT does not return a result set that should be freed.
//It returns true/false (1/0) to indicate success/failure.
//You should be freeing $second_result, which *IS* a result set
//with a bunch (10, you say?) records in it that are tying up memory.
//Of course, PHP is going to clean them up as soon as you close the connection
//or end the script, but hey, it's nice to clean
//up after yourself.

>mysql_Close($conn);
>
>?>

-- "TANSTAAFL" Rich lynch <email protected> webmaster@ and www. all of:
R&B/jazz/blues/rock - jademaze.com music industry org - chatmusic.com
acoustic/funk/world-beat - astrakelly.com sculptures - olivierledoux.com
my own nascent company - l-i-e.com cool coffeehouse - uncommonground.com

--
PHP 3 Mailing List   http://www.php.net/
To unsubscribe send an empty message to php3-unsubscribe <email protected>
To subscribe to the digest list:  php3-digest-subscribe <email protected>
For help: php3-help <email protected>  Archive:  http://www.php.net/mailsearch.php3
List administrator:  zeev-list-admin <email protected>