mysql_fetch_array and num_rows

Started on the old forum in January 2010 · view the original in the archive · old links redirect here

11 replies · 1,422 views · started 16 years ago · 1 min read

7 participants
wap_king Developer · 126 posts #1

when i have those in my codings i get errors like this

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/sitename/file.php on line 54

y is that??/

subzero Super moderator Fellow · 3,954 posts #2

Cant find sql bro your missing a sql or wrong val for sql.

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

wap_king Developer · 126 posts #3

hey bro this is wats at that line.....

$check = mysql_fetch_array(mysql_query("SELECT count(*) FROM members WHERE name='".$nm."'"));

this code is to check if username is already registered

cobusbo Developer · 117 posts #4

my guess remove the count and the brackets around the * try

$query = sprintf("SELECT * FROM members where name ='".$nm."'"); 
$result = @mysql_query($query); 
$rowAccount = @mysql_fetch_array($result);
wap_king Developer · 126 posts #5

ok bro am gonna try that!

crazybrumi Super moderator Principal · 1,064 posts #6
$check = mysql_fetch_array(mysql_query("SELECT name FROM members WHERE name='".$nm."'"));

if($check>0){echo "sorry this username is already taken";}

make sure that $nm has soemthing assigned to it

Want something coded email me at sales@webnwaphost.com for a prices.

http://flagcounter.com/more/W80q

GumSlone Administrator Principal · 1,690 posts #7

hmm this code is ok.

$check = mysql_fetch_array(mysql_query("SELECT count(*) FROM members WHERE name='".$nm."'"));

if ($check[0]>0) echo 'username is registered';

And brumis code, check is array. you can do it this way if you select a string from db

$check = mysql_fetch_array(mysql_query("SELECT name FROM members WHERE name='".$nm."'"));

if(isset($check[0][1])){echo "sorry this username is already taken";} 

i would suggest simply to get a user id from db, without exact match of username.

$check = mysql_fetch_row(mysql_query("SELECT id FROM members WHERE name LIKE '".mysql_real_escape_string($nm)."' LIMIT 1"));

if($check[0]>0){echo "sorry this username is already taken";} 

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

wap_king Developer · 126 posts #8

do u thinl i should create sum kind of function for this mysql_fetch_array() thats the error poping up and that code looks like a function

djlee Developer · 224 posts #9

??? you dont need a function, any errors that stem from the fetch_assoc, array, num_rows functions are due to bad php structure, validation and sanitisation. Your specific error however i cant say why your original code wasnt working, even with no data in the variable it should still run the query but look for a empty field and return no records and fetch array would simply be bool false. If i had lavalair or whatever code your using and your problem script i could probably figure it out, you might wanna learn some basic debug stuff too, such as mysql_error() and printing out the query so you can see what its trying to run with your sample data.

as for your code it would be better in regards to optimisation if you ran the check like so (this suggests you are only checking for registered members under the given name and will not actually use any positive resultsets later on)

if((int)mysql_result(mysql_query("SELECT id FROM members WHERE name LIKE '".mysql_real_escape_string($nm)."' LIMIT 1"),0) > 0) {
 //username is registered
}
else {
//username is not regged
}
metulj Super moderator Principal · 1,726 posts #10

would this be any good? (i've posted this in wrong thread before lol)

$check = mysql_query("SELECT name FROM members WHERE name = '$nm'") or die(mysql_error());
$checkresult = mysql_num_rows($check);
if($checkresult != 0)
{
echo "Sorry $username is already in use ! <a href=\"link_to_registration.php\">Choose another username</a>";
}
else
{
echo "bla bla...";
}

It's better to keep your mouth shut and give the impression that you're stupid, than to open it and remove all doubt. ⓣⓗⓔ ⓠⓤⓘⓔⓣⓔⓡ ⓨⓞⓤ ⓑⓔ©ⓞⓜⓔ, ⓣⓗⓔ ⓜⓞⓡⓔ ⓨⓞⓤ ⓐⓡⓔ ⓐⓑⓛⓔ ⓣⓞ ⓗⓔⓐⓡ ! ιη тнєσяу, тнє ρяα¢тι¢є ιѕ α яєѕυℓт σƒ тнє тнєσяу, вυт ιη ρяα¢тι¢є ιѕ тнє σρρσѕιтє.

キノgんイノ刀g 4 ア乇ムc乇 ノ丂 レノズ乇 キucズノ刀g 4 √ノ尺gノ刀ノイリ!

wap_king Developer · 126 posts #11

i got it to work thanx any way

crazybrumi Super moderator Principal · 1,064 posts #12

djlee wrote: ??? you dont need a function, any errors that stem from the fetch_assoc, array, num_rows functions are due to bad php structure, validation and sanitisation. Your specific error however i cant say why your original code wasnt working, even with no data in the variable it should still run the query but look for a empty field and return no records and fetch array would simply be bool false. If i had lavalair or whatever code your using and your problem script i could probably figure it out, you might wanna learn some basic debug stuff too, such as mysql_error() and printing out the query so you can see what its trying to run with your sample data.

as for your code it would be better in regards to optimisation if you ran the check like so (this suggests you are only checking for registered members under the given name and will not actually use any positive resultsets later on)

if((int)mysql_result(mysql_query("SELECT id FROM members WHERE name LIKE '".mysql_real_escape_string($nm)."' LIMIT 1"),0) > 0) {
 //username is registered
}
else {
//username is not regged
}

do you have any links with this sorrt of optimisation and validation help. as i wna take validation to an extra level. what i learnt so foar of my own back is ok but wna take it that little further.

Want something coded email me at sales@webnwaphost.com for a prices.

http://flagcounter.com/more/W80q

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close