473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SQL not returning value even though it's there...

Hello Everyone,

I have been having fits with a simple PHP script and I'm hoping it's
something that someone here can help me with. Basically, when I execute the
SQL statement directly on the MySQL CLI, it returns. But when I run the same
statement through the following PHP script, I *think* it returns an empty
set.

I say I think because the if(mysql_num_rows($recset) > 0){} block is
executed. But when I try to retreive values stored in my array, I get
blanks. Can someone offer me some help? Thanks in advance -- Anthony

=== SCRIPT ===
<?php
include("db.php");

$incoming_user = stripslashes($_POST['username']);

$sql = "SELECT * FROM tblSisters WHERE username != '$incoming_user' AND
isTaken != 'yes' ORDER BY RAND() LIMIT 1";

$recset = mysql_query($sql, $conn);

if(mysql_num_rows($recset) > 0){
$row = mysql_fetch_row($recset);
echo "<table border=\"1\" align=\"center\" cellspacing=\"7\">\n";
echo "<tr>\n";
echo "<th>USERNAME</th>\n";
echo "<th>WANTS MAIL</th>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>$row[username]</td><td>$row[wsm]</td>\n";
echo "</tr>\n";
echo "</table>\n";

// Update the database to reflect the new selection

$sql2 = "UPDATE tblSisters SET isTaken='yes', takeBy='$incoming_user' WHERE
username='row[username]'";
mysql_query($sql2, $conn);
}
else{
echo "<h2><u><b>RECORD RETREIVAL FAILURE</b></u></h2><br>\n";
echo "The secret sister database could not be queried. Please try again ";
echo "later or contact the system administrator.\n";
}
?>
=== END SCRIPT ===
Jul 17 '05 #1
5 2158
Anthony Papillion II wrote:
I say I think because the if(mysql_num_rows($recset) > 0){} block is
executed. But when I try to retreive values stored in my array, I get
blanks. Can someone offer me some help? Thanks in advance -- Anthony [snip] if(mysql_num_rows($recset) > 0){
$row = mysql_fetch_row($recset); [snip] echo "<td>$row[username]</td><td>$row[wsm]</td>\n";


If you were running with error_reporting set to E_ALL, you would see an
"undefined array index" notice here which would have helped you diagnose
the problem: mysql_fetch_row returns an enumerated array, not an
associative array.

If you want to access columns by name, use mysql_fetch_array (or
mysql_fetch_object to use object syntax).

See the online manual for additional information:
http://www.php.net/error_reporting
http://www.php.net/mysql_fetch_row

-- brion vibber (brion @ pobox.com)
Jul 17 '05 #2
.oO(Anthony Papillion II)
I have been having fits with a simple PHP script and I'm hoping it's
something that someone here can help me with. Basically, when I execute the
SQL statement directly on the MySQL CLI, it returns. But when I run the same
statement through the following PHP script, I *think* it returns an empty
set.

I say I think because the if(mysql_num_rows($recset) > 0){} block is
executed. But when I try to retreive values stored in my array, I get
blanks. Can someone offer me some help? Thanks in advance -- Anthony


Use mysql_fetch_assoc() instead of mysql_fetch_row(). The first returns
an associative array, the latter a numeric one.

Micha
Jul 17 '05 #3

Hi

mysql_fetch_row() creates an integer indexed array. You are using
associated indexes to output the row data. The index does not exist.
To use associative indexes simply change mysql_fetch_row() to
mysql_fetch_array() or mysql_fetch_assoc()

e.g => $row = mysql_fetch_array($recset);

Regards
Peter

Anthony Papillion II wrote:
Hello Everyone,

I have been having fits with a simple PHP script and I'm hoping it's
something that someone here can help me with. Basically, when I execute the
SQL statement directly on the MySQL CLI, it returns. But when I run the same
statement through the following PHP script, I *think* it returns an empty
set.

I say I think because the if(mysql_num_rows($recset) > 0){} block is
executed. But when I try to retreive values stored in my array, I get
blanks. Can someone offer me some help? Thanks in advance -- Anthony

=== SCRIPT ===
<?php
include("db.php");

$incoming_user = stripslashes($_POST['username']);

$sql = "SELECT * FROM tblSisters WHERE username != '$incoming_user' AND
isTaken != 'yes' ORDER BY RAND() LIMIT 1";

$recset = mysql_query($sql, $conn);

if(mysql_num_rows($recset) > 0){
$row = mysql_fetch_row($recset);
echo "<table border=\"1\" align=\"center\" cellspacing=\"7\">\n";
echo "<tr>\n";
echo "<th>USERNAME</th>\n";
echo "<th>WANTS MAIL</th>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td>$row[username]</td><td>$row[wsm]</td>\n";
echo "</tr>\n";
echo "</table>\n";

// Update the database to reflect the new selection

$sql2 = "UPDATE tblSisters SET isTaken='yes', takeBy='$incoming_user' WHERE
username='row[username]'";
mysql_query($sql2, $conn);
}
else{
echo "<h2><u><b>RECORD RETREIVAL FAILURE</b></u></h2><br>\n";
echo "The secret sister database could not be queried. Please try again ";
echo "later or contact the system administrator.\n";
}
?>
=== END SCRIPT ===

Jul 17 '05 #4
Brion Vibber <br***@pobox.com> wrote in message news:<31*************@uni-berlin.de>...
<snip>
If you were running with error_reporting set to E_ALL


or even better E_ALL | E_STRICT under PHP 5 in developement servers.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com
Jul 17 '05 #5
> If you want to access columns by name, use mysql_fetch_array (or
mysql_fetch_object to use object syntax).


LOL I can't believe it was something THAT simple and THAT obvious. I *knew*
that mysql_fetch_array() was the function to use (I've been using it
forever) but I totally spaced it out on this one. I didn't even know that
there was a mysql_fetch_row() function. Geez, now I feel stupid. Thanks for
your input.

Anthony
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Member | last post by:
Hi All. Maybe someone in here could help on this too.... Uusally I can return a value from a stored procedure without any problem. Today I ran into something I cannot figure out. ...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
11
by: Justin Naidl | last post by:
class Foo { protected: char foo_stuff; public: char* get_foo_stuff(); } Given the above example. What I want to know is the "proper/standard" way
19
by: Rick | last post by:
Hi, I was wondering, can it be safely assumed that any function that returns a *char will return a NULL terminated string? Or does the function explicitly mention this always (and the ones that...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
6
by: RThaden | last post by:
Hi together, I am a bit clueless about the following problem: In the following code excerpt std::string getStr() { std::string bla="asdfsa";
6
by: rowe_newsgroups | last post by:
I just noticed this about value types and return values, and it kind of bugs me that the compiler won't treat it as an error, even with Option Strict On. Try this out: Option Strict On ...
4
by: rbjorkquist | last post by:
This is my first attempt at writing/using web services, so any and all comments will be greatly appreciated. With that said, I am also by no means saying this is the correct either. I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.