willwill100 Posted March 20, 2006 Share Posted March 20, 2006 [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?! Quote Link to comment https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/ Share on other sites More sharing options...
ober Posted March 20, 2006 Share Posted March 20, 2006 It's probably because: "SELCT * FROM `$compl` WHERE `Name`=$name"1) you spelled "SELECT" wrong.2) $name should be in quotes: "SELECT * FROM `$compl` WHERE `Name`= '$name'" Quote Link to comment https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19070 Share on other sites More sharing options...
willwill100 Posted March 20, 2006 Author Share Posted March 20, 2006 good spot but i still get the error!! Quote Link to comment https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19071 Share on other sites More sharing options...
ober Posted March 20, 2006 Share Posted March 20, 2006 Post the code again with the changes and double check to make sure your tablenames and column names match what you actually have. Quote Link to comment https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19077 Share on other sites More sharing options...
willwill100 Posted March 20, 2006 Author Share Posted March 20, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19090 Share on other sites More sharing options...
ober Posted March 20, 2006 Share Posted March 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19094 Share on other sites More sharing options...
kenrbnsn Posted March 20, 2006 Share Posted March 20, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19097 Share on other sites More sharing options...
willwill100 Posted March 20, 2006 Author Share Posted March 20, 2006 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 93INSERT 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? Quote Link to comment https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19103 Share on other sites More sharing options...
ober Posted March 20, 2006 Share Posted March 20, 2006 Tablenames cannot have spaces, unless I'm mistaken! Quote Link to comment https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19112 Share on other sites More sharing options...
willwill100 Posted March 21, 2006 Author Share Posted March 21, 2006 sorry took so long to reply, table names can have spaces (it has worked for the rest of my project) what could the problem be? is the only problem my database or what?thanx Quote Link to comment https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19401 Share on other sites More sharing options...
txmedic03 Posted March 22, 2006 Share Posted March 22, 2006 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... Quote Link to comment https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19595 Share on other sites More sharing options...
kenrbnsn Posted March 22, 2006 Share Posted March 22, 2006 You need to do the mysql_num_rows() function after the mysql_query()[code]<?php$rs = mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());if (mysql_num_rows($rs)==0){?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/5357-sql-num_rows-query-help/#findComment-19629 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.