Jump to content

sql num_rows query - help!


willwill100

Recommended Posts

[code]
////if their name does not already exist in the relevant table, insert it////
$namesthere = mysql_fetch_array(mysql_query("SELCT * FROM `$compl` WHERE `Name`=$name"));
if (mysql_num_rows($namesthere)==0){
$query = mysql_query("INSERT INTO '$compl' ( `Name` , `Sail Number` , `Class` , `Score` ) VALUES ( '$name', '$snum', '$boattype', '$score')");
} [/code]

The code above is giving the "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource" error. Can anyone give me a hand please?!
Link to comment
https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/
Share on other sites

i'm sure ive got the table name correct but i now only get one error (before I got 2) saying:

[code]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\Will\My Documents\xampp\htdocs\sailing\rescalc.php on line 91[/code]

with this code:

[code]////if their name does not already exist in the relevant table, insert it////
$namesthere = mysql_fetch_array(mysql_query("SELECT * FROM `$compl` WHERE `Name`='$name'"));
if (mysql_num_rows($namesthere)==0){
$query = mysql_query("INSERT INTO `$compl` ( `Name` , `Sail Number` , `Class` , `Score` ) VALUES ( '$name', '$snum', '$boattype', '$score')");
echo ("<pre>" . $query . "</pre>");
}

////////////
[/code]

the <pre> outputs:

[code]1[/code]

and thats it! any ideas?
Link to comment
https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19090
Share on other sites

That's because mysql_query is going to return the result set number.

You really need to stop nesting your functions so you can troubleshoot more easily:

[code]$query = "INSERT INTO `$compl` ( `Name` , `Sail Number` , `Class` , `Score` ) VALUES ( '$name', '$snum', '$boattype', '$score')";
$result = mysql_query($query) or die(mysql_error());
if($result && mysql_num_rows($result) > 0)
{
   $row = mysql_fetch_array($result);
}
[/code]

That's how it should be structured. Then you can echo the query quite easily.
Link to comment
https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19094
Share on other sites

You have too many compound statement to really determine what is causing the problem. Let's break them down to the individual statements with error checking along the way:
[code]<?php
////if their name does not already exist in the relevant table, insert it////
$q = "SELECT * FROM `$compl` WHERE `Name`='$name'";
$rs = mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());
$namesthere = mysql_fetch_assoc($rs);
if (mysql_num_rows($namesthere)==0){
    $q = "INSERT INTO `$compl` ( `Name` , `Sail Number` , `Class` , `Score` ) VALUES ( '$name', '$snum', '$boattype', '$score')";
    echo ("<pre>" . $q . "</pre>");
    $rs = mysql_query($q) or die('Problem with insert query: ' . $q . '<br>' . mysql_error());
}
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19097
Share on other sites

okay, ive used the improved code.

heres what i get when i input some dummy values

[code]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\Will\My Documents\xampp\htdocs\sailing\rescalc.php on line 93

INSERT INTO `topper main points` ( `Name` , `Sail Number` , `Class` , `Score` ) VALUES ( 'Will Williams', '45052', 'Topper', '129')[/code]

i put the sql into phpmyadmin and it worked so what could be the problem?
Link to comment
https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19103
Share on other sites

if mysql_query() did not return anything then mysql_fetch_assoc() will fail. There is nothing there for it to fetch. You do no get num_rows from fetch_assoc...you get an associative array of one row from mysql_fetch_assoc() based on the query. Take the query and do mysql_num_rows() on it to get the number of rows or a SELECT count(*) with conditions...
Link to comment
https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19595
Share on other sites

Archived

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

×
×
  • Create New...

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.