...

View Full Version : mysql_fetch_array error



jacko201
06-02-2004, 01:47 AM
Hi,

I keep getting the following error and just can't figure out what the problem is. The query is definately fine, could it be anything to do with the username being a name and not a number?

The code is as follows:


if (isset($_SESSION['username']) AND ($_SESSION['privilege'] === 'a'))
{
require_once ('mysql_connect.php'); // Connect to the database.
$query = "SELECT * FROM users WHERE users.username = {$_GET['username']}";

$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_ASSOC);

Any help would be much appreciated.

Thanks,
Paul

firepages
06-02-2004, 02:34 AM
I keep getting the following error

.. and the error is ... ;)

duniyadnd
06-02-2004, 03:11 AM
from what i've seen so far (and yeah, i got to agree with firepages - what error message), that you're missing a closing curly bracket '}'

Otherwise, if you mean error by "no output", then what you need to do is use a while loop:



if (isset($_SESSION['username']) AND ($_SESSION['privilege'] === 'a'))
{
require_once ('mysql_connect.php'); // Connect to the database.
$query = "SELECT * FROM users WHERE users.username = {$_GET['username']}";

$result = mysql_query ($query);
while ( $row = mysql_fetch_array ($result, MYSQL_ASSOC)) {

// do your thang here..

}
}

MrShed
06-02-2004, 10:52 PM
The query is definately fine, could it be anything to do with the username being a name and not a number?




No reason why that should make any difference jacko(unless of course you've got your username field set as an INT). I can't c any missing curly brackets like sum1 said above, in fact I can't see any problem with it at all. You might want to make the privilege comparison just an equivalent rather than exact match (i.e. "==" not "==="). It's hard to guess without seeing the actual error message, but is your mysql_connect script definitely working ok? As I can't see any problem with that at all. Also, this won't/shouldn't make any difference, but the "users.username" part is pointless, just have "username" . Hope this helps, but post the error!

jacko201
06-03-2004, 12:43 AM
Oh the error...sorry about that :)

the error is...

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/cjpyec01/public_html/view_customer.php on line 256

I've included the whole php code this time too.


<?php
if (isset($_SESSION['username']) AND ($_SESSION['privilege'] === 'a'))
{
require_once ('mysql_connect.php'); // Connect to the database.
$query = "SELECT * FROM users WHERE users.username = {$_GET['username']}";

$result = mysql_query ($query);
($row = mysql_fetch_array ($result, MYSQL_ASSOC))
// Item Details
echo '<table border="0" width="80%" cellspacing="3" cellpadding="3" align="center">
<tr><td bgColor=#80E7B7 align="right" width="35%"><b>Username:</b></td>';
echo "<td class=\"results_table\">{$row['username']}</td></tr>";

echo '<tr><td bgColor=#80E7B7 align="right" width="35%"><b>Registration Date:</b></td>';
echo "<td class=\"results_table\">{$row['registration_date']}</td></tr>";

echo '<tr><td bgColor=#80E7B7 align="right" width="35%"><b>Name:</b></td>';
echo "<td class=\"results_table\">{$row['forename']} {$row['surname']}</td></tr>";

echo '<tr><td bgColor=#80E7B7 align="right" width="35%"><b>Email:</b></td>';
echo "<td class=\"results_table\">{$row['email']}</td></tr>";

echo '<tr><td bgColor=#80E7B7 align="right" width="35%"><b>Phone:</b></td>';
echo "<td class=\"results_table\">{$row['phone']}</td></tr>";

echo '<tr><td bgColor=#80E7B7 align="right" width="35%"><b>Address:</b></td>';
echo "<td class=\"results_table\">{$row['address1']}<br> {$row['address2']}<br> {$row['town']}<br> {$row['postcode']}</td></tr>";


echo '<tr><td bgColor=#80E7B7 align="right" width="35%"></td>';
echo "<td><a href=\"edit_user.php?username={$row['username']}\">EDIT DETAILS</a> <br> <a href=\"delete_customer.php?username={$row['username']}\">DELETE CUSTOMER</a><td>";

echo "<br/>";

}
else
{
echo '<P>You are not authorised to view this page</p>';
echo 'Please <a href = "login.php"> Login </a><br>';
}
mysql_close();// Closes the database connection.

?>

If you've any idea what the problem could be please help!

Many thanks
Paul

jacko201
06-03-2004, 12:45 AM
Oh & yes, the mysql_connect works fine, tested it with other pages!

Thanks for any help

jacko201
06-03-2004, 01:03 AM
another mistake...

the line below is like this...


$row = mysql_fetch_array ($result, MYSQL_ASSOC);

I changed it in post #5 to try dunigadnd's suggestion!

Regards

firepages
06-03-2004, 02:37 AM
ok , assuming users.username is a character field , you need to quote it in the query ...

$query = " SELECT * FROM users WHERE users.username = '{$_GET['username']}' ";

in fact in MySQL you can quote everything regardless of type apart from function calls , however if you want your SQL to be portable you should not quote integer types.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum