Jump to content

Archived

This topic is now archived and is closed to further replies.

willwill100

Resource id #6 error

Recommended Posts

from this code:

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

i get the <pre> displaying:

[code]Resource id #6[/code]

what is the reason for this?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
ok, ive ammended my code but i still get a negative result:

[code]////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!");
}[/code]

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??

Share this post


Link to post
Share on other sites
on this line:

[code]
if (mysql_fetch_assoc($rs)==0){
[/code]

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
[code]
if (mysql_num_rows($rs) == 0) {
[/code]

Share this post


Link to post
Share on other sites
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.....

Share this post


Link to post
Share on other sites
[!--quoteo(post=357078:date=Mar 21 2006, 08:31 PM:name=WillWill)--][div class=\'quotetop\']QUOTE(WillWill @ Mar 21 2006, 08:31 PM) [snapback]357078[/snapback][/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]

[code]<?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

?>[/code]

Share this post


Link to post
Share on other sites
why not just do this

[code]////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!");
}[/code]

Ray

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.