monkeypaw201 Posted August 12, 2008 Share Posted August 12, 2008 $result = mysql_query("SELECT * FROM `clients` WHERE `cid` = '$values[0]'"); $count = mysql_num_rows($result); Whenever I run that it throws the error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /clients.php on line 64 I have tried everyhing i know.. bit its starting to just piss me off more than anything... before I throw my computer out the window (again.. ) any suggestions? Link to comment https://forums.phpfreaks.com/topic/119245-solved-annoying-little-mysql-row-count-problem/ Share on other sites More sharing options...
genericnumber1 Posted August 12, 2008 Share Posted August 12, 2008 $result = mysql_query("SELECT * FROM `clients` WHERE `cid` = '$values[0]'") or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/119245-solved-annoying-little-mysql-row-count-problem/#findComment-614193 Share on other sites More sharing options...
monkeypaw201 Posted August 12, 2008 Author Share Posted August 12, 2008 Notice: Undefined offset: 1 in clients.php on line 63 did I post the array incorrectly? Link to comment https://forums.phpfreaks.com/topic/119245-solved-annoying-little-mysql-row-count-problem/#findComment-614197 Share on other sites More sharing options...
genericnumber1 Posted August 12, 2008 Share Posted August 12, 2008 what is $values[0] set to? not what _should_ it be set to, mind you, echo it and find what it is EDIT: to address that error, post line 63 and a few lines around it Link to comment https://forums.phpfreaks.com/topic/119245-solved-annoying-little-mysql-row-count-problem/#findComment-614199 Share on other sites More sharing options...
monkeypaw201 Posted August 12, 2008 Author Share Posted August 12, 2008 $values = array(); foreach( $filtered as $filter ) { if ( count($filter) != count($columns) ) { die( ' Column counts did not match. MySQL expects ' .count($columns). ' columns and your data is giving it ' .count($filter). ' columns. '); } # Sanitize individual rows foreach ( $filter as $key => $val ) $filter[$key] = mysql_real_escape_string( $val ); # Format and implode $values[] = "('" . implode( "', '", $filter ) . "')"; $result = mysql_query("SELECT * FROM `clients` WHERE `cid` = '$values[1]'")or die(mysql_error()); echo $filtered[1]; $row = mysql_fetch_array($result); $count = mysql_num_rows($result); if($count == 0) { $q = "INSERT INTO `clients` (`" . implode( '`, `', $columns ) . "`) VALUES " . implode( ', ', $values ); echo $q; }else{ $q = "UPDATE `clients` SET" . $row['cid']; echo $q; } } That should cover all the releveant code.. i know the $q UPDATE isn't done.. im just trying to see if it works.. Link to comment https://forums.phpfreaks.com/topic/119245-solved-annoying-little-mysql-row-count-problem/#findComment-614200 Share on other sites More sharing options...
genericnumber1 Posted August 12, 2008 Share Posted August 12, 2008 Your code is a bit off.... you're adding elements to the $values array, and I can't see why you're doing that. try this... <?php foreach( $filtered as $filter ) { if ( count($filter) != count($columns) ) { die( ' Column counts did not match. MySQL expects ' .count($columns). ' columns and your data is giving it ' .count($filter). ' columns. '); } # Sanitize individual rows //Code removed, no need to sanitize single elements when we need to sanitize it all at once to get the single quotes in the implode escaped as well # Format and implode $values = mysql_real_escape_string( "('" . implode( "', '", $filter ) . "')" ); // Line messy looking, might consider rewriting $result = mysql_query("SELECT * FROM `clients` WHERE `cid` = '$values'")or die(mysql_error()); echo $filtered[1]; $row = mysql_fetch_array($result); $count = mysql_num_rows($result); if($count == 0) { $q = "INSERT INTO `clients` (`" . implode( '`, `', $columns ) . "`) VALUES " . implode( ', ', $values ); echo $q; }else{ $q = "UPDATE `clients` SET" . $row['cid']; echo $q; } } ?> That's just fixing some syntax issues, I don't know if that's what you want the script to do. Link to comment https://forums.phpfreaks.com/topic/119245-solved-annoying-little-mysql-row-count-problem/#findComment-614202 Share on other sites More sharing options...
monkeypaw201 Posted August 12, 2008 Author Share Posted August 12, 2008 Warning: implode() [function.implode]: Invalid arguments passed in clients.php on line 68 Link to comment https://forums.phpfreaks.com/topic/119245-solved-annoying-little-mysql-row-count-problem/#findComment-614207 Share on other sites More sharing options...
genericnumber1 Posted August 12, 2008 Share Posted August 12, 2008 it looks like $filter must not be an array... that's what implode is for... combining array elements. I really don't know what your code is trying to do so I can't help any more than that. Link to comment https://forums.phpfreaks.com/topic/119245-solved-annoying-little-mysql-row-count-problem/#findComment-614208 Share on other sites More sharing options...
monkeypaw201 Posted August 12, 2008 Author Share Posted August 12, 2008 ok, lemme start over, so we dont get confused... This script will: * Import a data file, and cut out the context we want * Separate each line, and feed each one into the loop * Load each line and seperate all the datapoints and insert into a database (if `cid` already exists in the database, simply update the current row.. $file = 'data.txt'; $start = '!CLIENTS:'; $end = ';'; # Put file to an array $lines = file( $file ); # Set up placeholders $loop = TRUE; $capture = FALSE; $filtered = array(); # Loop through lines, until we've found $end for( $i = 0, $count = count($lines); $i < $count && $loop === TRUE; $i++ ) { # Check to see if capturing has been turned on if ( $capture === TRUE ) { # Check to see if this is the endline if ( strpos($lines[$i], $end) === 0 ) # End the loop $loop = FALSE; else # Explode and add to filtered results $filtered[] = explode( ':', trim($lines[$i] ) ); } # Check to see if this is the starting line elseif ( strpos($lines[$i], $start) === 0 ) # Turn on capturing $capture = TRUE; } $columns = array('callsign', 'cid', 'realname', 'clienttype', 'frequency', 'latitude', 'longitude', 'altitude', 'groundspeed', 'planned_aircraft', 'planned_tascruise', 'planned_depairport', 'planned_altitude', 'planned_destairport', 'server', 'protrevision', 'rating', 'transponder', 'facilitytype', 'visualrange', 'planned_revision', 'planned_flighttype', 'planned_deptime', 'planned_actdeptime', 'planned_hrsenroute', 'planned_minenroute', 'planned_hrsfuel', 'planned_minfuel', 'planned_altairport', 'planned_remarks', 'planned_route', 'planned_depairport_lat', 'planned_depairport_lon', 'planned_destairport_lat', 'planned_destairport_lon', 'atis_message', 'time_last_atis_recieved', 'time_logon', 'heading', 'QNH_iHg', 'QNH_Mb','placeholder'); $values = array(); foreach( $filtered as $filter ) { if ( count($filter) != count($columns) ) { die( ' Column counts did not match. MySQL expects ' .count($columns). ' columns and your data is giving it ' .count($filter). ' columns. '); } # Sanitize individual rows //Code removed, no need to sanitize single elements when we need to sanitize it all at once to get the single quotes in the implode escaped as well # Format and implode $values = mysql_real_escape_string( "('" . implode( "', '", $filter ) . "')" ); // Line messy looking, might consider rewriting $result = mysql_query("SELECT * FROM `clients` WHERE `cid` = '$values'")or die(mysql_error()); $row = mysql_fetch_array($result); $count = mysql_num_rows($result); if($count == 0) { $q = "INSERT INTO `clients` (`" . implode( '`, `', $columns ) . "`) VALUES " . implode( ', ', $values ); echo $q; }else{ $q = "UPDATE `clients` SET" . $row['cid']; echo $q; } } Link to comment https://forums.phpfreaks.com/topic/119245-solved-annoying-little-mysql-row-count-problem/#findComment-614209 Share on other sites More sharing options...
genericnumber1 Posted August 12, 2008 Share Posted August 12, 2008 Oh, I see now.... you should go back to using your original code since my understanding of the code was completely different than what you were really trying to do. You use implode() once where you should use explode() (the first time). Try fixing that and it might help you fix your problems. I don't feel keen to go over the whole script. Link to comment https://forums.phpfreaks.com/topic/119245-solved-annoying-little-mysql-row-count-problem/#findComment-614221 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.