Jump to content

[SOLVED] Annoying little MySQL Row count problem


monkeypaw201

Recommended Posts

    $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.. :D) any suggestions?

Link to comment
Share on other sites

$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
Share on other sites

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.