RE: [PHP-WIN] mysql_fetch_array problem
| From: | Dash McElroy | Date: | Sat, 09 Nov 2002 00:52:35 +0000 |
| Subject: | RE: [PHP-WIN] mysql_fetch_array problem | ||
| Groups: | php.windows | ||
| Request: | Send a blank email to php-windows+get-16823@lists.php.net to get a copy of this message | ||
Zeus,
2 notes:
1. Data you insert into a database should be addslashes($varname) first (or
another encoding) to protect against MySQL injection vulnerabilities. A nice
stripslashes($varname) on the way out gets rid of the slashes. You could
also use urlencode($varname) and urldecode($varname). Helpful for storing
URL's.
2. mysql_fetch_array
(http://www.php.net/manual/en/function.mysql-fetch-array.php) is nowhere to
be found in your code. You're using mysql_fetch_field...
Either way, this is what I do:
$result = mysql_query("SELECT * FROM shoutbox BY ID desc LIMIT 5");
$count = mysql_num_rows($result);
if ($count > 0) {
for ($i=0;$i<$count;$i++) {
$row = mysql_fetch_array($result);
echo $row['time'] ."<br />".$row['name']."<br
/>".$row['message']."<br />\n";
}
}
elseif ($count == 0) {
echo "No results returned";
}
else {
echo "Invalid Query. MySQL error: ".mysql_error();
}
Please note - this code is off the top of my head, I have not actually tried
it :)
-Dash
-----Original Message-----
From: Zeus [mailto:zeus_dreamer@myrealbox.com]
Sent: Saturday, November 09, 2002 4:29 AM
To: php-windows@lists.php.net
Subject: [PHP-WIN] mysql_fetch_array problem
I try to display mysql database using fetch_array but there seem to problem,
i m new with PHP and mysql.
I didn't quite sure, what's worng with my codes.i havent include the form
yet.
the error messages:
Warning: mysql_fetch_field(): supplied argument is not a valid MySQL result
resource in c:\apache\htdocs\zeus\shoutbox.php on line 17
code in shoutbox.php:
<?PHP
mysql_connect('localhost', 'zeus', 'pass') or die
("deadconnect");
mysql_select_db('zeussama_db') or die ("no db");
if($submit)
{
$time=date("h:ia d/j/y");
$result=mysql_query("INSERT INTO shoutbox (id,name,message,time)","values
('NULL','$name','$message','$time')");
}
?>
<?php
$result = mysql_query("SELECT * FROM shoutbox BY ID desc LIMIT 5");
while($r=mysql_fetch_field($result))
{
$time=$r["time"];
$id=$r["id"];
$message=$r["message"];
$name=$r["name"];
echo "$time <br>";
echo "$name <br>";
echo "$message <br>";
}
?>
================================
:.zeus:.
http://www.redrival.com/zeussama
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php