Jump to content


Photo

Resource id #6 error


  • Please log in to reply
7 replies to this topic

#1 willwill100

willwill100
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 21 March 2006 - 07:23 PM

from this code:

$namen = mysql_query("SELECT * FROM `$compl` WHERE `Name`='$name'");
echo ("<pre>" . $namen . "</pre>");

i get the <pre> displaying:

Resource id #6

what is the reason for this?

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 21 March 2006 - 07:52 PM

The mysql_query() function returns a pointer to the rows selected not the rows. To get individual rows, you need to use one of the mysql_fetch functions, usually in a loop.

Ken

#3 willwill100

willwill100
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 21 March 2006 - 08:20 PM

ok, ive ammended my code but i still get a negative result:

////if their name does not already exist in the REAL table, insert it////
$q = "SELECT COUNT(*) FROM `$compl` WHERE `Sail Number`='$snum'";
echo ("<pre>" . $q . "</pre>");
$rs = mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());

if (mysql_fetch_assoc($rs)==0){
    $b = "INSERT INTO `$compl` ( `Name` , `Sail Number` , `Class`) VALUES ( '$name', '$snum', '$boattype')";
   
}else{
echo ("<br>No user updated!");
}

I have inputted $q into phpmyadmin and a value of "0" is returned, why then is the "if statement" taken to be false and "No user updated!" outputted??

#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 08:24 PM

on this line:

if (mysql_fetch_assoc($rs)==0){

you're checking if an entire array equals 0. even if there are no rows, it still wont return the number 0.

what you need is
if (mysql_num_rows($rs) == 0) {

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#5 willwill100

willwill100
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 21 March 2006 - 08:31 PM

okay that makes sense but i still get the error.

any ideas, im really stuck on how to get my table to check if there is already a user in it and enter one if the user is not in the table.....



#6 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 21 March 2006 - 08:37 PM

[!--quoteo(post=357078:date=Mar 21 2006, 08:31 PM:name=WillWill)--][div class=\'quotetop\']QUOTE(WillWill @ Mar 21 2006, 08:31 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
okay that makes sense but i still get the error.

any ideas, im really stuck on how to get my table to check if there is already a user in it and enter one if the user is not in the table.....
[/quote]

<?php

$q = "SELECT COUNT(*) FROM `$compl` WHERE `Sail Number`='$snum'";

$result = mysql_query($q);

    if ( mysql_num_rows($query) == 0 ) {
        $q2 = "INSERT INTO `$compl` ( `Name` , `Sail Number` , `Class`) VALUES ( '$name', '$snum', '$boattype')";
        mysql_query($q2);
    } 

//more code here or whatever

?>

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#7 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 21 March 2006 - 08:39 PM

why not just do this

////if their name does not already exist in the REAL table, insert it////
$q = "SELECT * FROM `$compl` WHERE `Sail Number` = '$snum'";
$rs = mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());
  $num_rows = mysql_num_rows($rs);
if ($num_rows == 0){
    $b = "INSERT INTO `$compl` ( `Name` , `Sail Number` , `Class`) VALUES ( '$name', '$snum', '$boattype')";
   
}else{
echo ("<br>No user updated!");
}

Ray

#8 willwill100

willwill100
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 21 March 2006 - 08:58 PM

thats great, thanx for the help guys!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users