Page 1 of 1

php and mysql problem

Posted: Mon Dec 18, 2006 6:06 pm
by garry27
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi,

why does the code below return " array: , , , , , , , , , , , , , , , , , , , " ?

the num_rows returned is 1 and i'm only querying 10 columns so the maximum number of values in the array should be no more than this.

when i try to remove the empty values using the loop, it makes no difference either.

what am i doing wrong and why are there so many values in the $row array?

Code: Select all

/***********get db record for the current current pub crawl ***************/
	db_connect();
    $sql = mysql_query("SELECT e1,e2,e3,e4,e5,e6,e7,e8,e9,e10 from Pub_Crawl_Order 
	                    WHERE crawlID = $crawl_id") 
				        or die ('query1 invalid on table_data.php: ' . mysql_error() );
	$row = mysql_fetch_array($sql); //creat array of the record
	
	/*****remove empty values from array********/
	foreach ($row as &$val) {
	 if ($val == 'NULL' || '') {
	 unset($val); 
	 }
    }
	
	$a = implode(", ", $row );         //test code

  
    print 'array values: ' . $a;

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Dec 18, 2006 6:14 pm
by hrubos
so you can try

Code: Select all

$num=mysql_numrows($sql );
$i=0;

while($i<$num){
	$e1=mysql_result($result,$i,"e1");
$e2=mysql_result($result,$i,"e2");
$e3=mysql_result($result,$i,"e3");
$e4=mysql_result($result,$i,"e4");
........................
$e10=mysql_result($result,$i,"e10");

echo "$e1" ;
.............
echo "$e10";
$i++
       
}

Posted: Mon Dec 18, 2006 6:26 pm
by garry27
thanks hrubos, but that doesn't really explain why i'm getting the result i described.

i was actually hoping to get rid of the e1, e2 .. and use select *. there's only 11 columns in the table: the first is the index key and the others are the e1,e2,e3.

Posted: Mon Dec 18, 2006 7:40 pm
by garry27
bump.

Posted: Mon Dec 18, 2006 7:54 pm
by volka
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:4. All users of any level are restricted to bumping (as defined here) any given thread within twenty-four (24) hours of its last post. Non-trivial posts are not considered bumping. A bump post found in violation will be deleted, and you may or may not recieve a warning. Persons bumping excessively be considered as spammers and dealt with accordingly.
Where does $crawl_id come from?
try

Code: Select all

$query = "SELECT
		e1,e2,e3,e4,e5,e6,e7,e8,e9,e10
	FROM
		Pub_Crawl_Order
	WHERE
		crawlID=$crawl_id";
$sql = mysql_query($query) or die (mysql_error().': '.$query );

$row = mysql_fetch_array($sql, MYSQL_NUM);

echo '<pre>'; var_export($row); echo "</pre>\n";
$a = join(', ', array_filter($row));
echo 'array values: ' , $a;

Posted: Mon Dec 18, 2006 7:58 pm
by garry27
i've changed to using an associative array and i've stopped getting the duplicate entries.

ta for the help.

Re: php and mysql problem

Posted: Mon Dec 18, 2006 8:14 pm
by chakhar86
garry27 wrote:

Code: Select all

foreach ($row as &$val) {             //you don't need & before $val
	 if ($val == 'NULL' || '') {
	 unset($val); 
	 }
  
  
    print 'array values: ' . $a;
As long I know you don't need an ampersand there..

Posted: Mon Dec 18, 2006 10:33 pm
by RobertGonzalez
Let's look at this code really close and see if you can spot what it is doing...

Code: Select all

<?php
/***********get db record for the current current pub crawl ***************/
    // I'm assuming this is your database connection
    db_connect();

    // Run a query on 10 fields in the Pub_Crawl_Order table fetching records in the table that have a crawlID of $crawl_id
    $sql = mysql_query("SELECT e1,e2,e3,e4,e5,e6,e7,e8,e9,e10 from Pub_Crawl_Order
                       WHERE crawlID = $crawl_id")
                    or die ('query1 invalid on table_data.php: ' . mysql_error() );

   // Assign the last row of the result into a vaiable called row
   $row = mysql_fetch_array($sql); //creat array of the record
   
   /*****remove empty values from array********/
   // Loop the one record row array looking at a reference to the member values
   foreach ($row as &$val) {

    // THIS SYNTAX LOOKS WONKY,
    // If $val is equal to the string 'NULL' OR [empty] :: THIS WILL NOT EVALUATE HOW YOU THINK IT WILL
    if ($val == 'NULL' || '') {
    // Make $val an unset value
    unset($val);
    }
    }

   // What are you hoping to do here?
   $a = implode(", ", $row );         //test code

    // You could always do a var_dump of the array, no?
    print 'array values: ' . $a;
?>

Posted: Mon Dec 18, 2006 10:58 pm
by garry27
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


this is what i was after. i get a bit narky if i can't write the code the same way as i have in my pseudocode. anyway seems to work well. 

basiclly this script is a response to an ajax call (no the actual xmlhttp response, as yet) by my google maps application.

Code: Select all

if ($_GET['action'] == 'addpub')
  {
    $crawl_id = $_GET['crawlid'];
    $pub_id = $_GET['pubid'];

    /***********get db record for the current current pub crawl **********************/
	db_connect();
    $sql = mysql_query("SELECT * from Pub_Crawl_Order 
	                    WHERE crawlID = $crawl_id") 
				        or die ('query1 invalid on table_data.php: ' . mysql_error() );
	$row = mysql_fetch_assoc($sql); //creat array of the record
	
	
	/*****remove index key and empty values from array********/
	unset($row['crawlID']);
	foreach ($row as $key => &$val) {
	 if ($val == 'NULL' || $val =='') {
	  unset($row[$key]); 
	 }
    }
	
	/**** declare next empty column in Pub_Crawl_Order ******/
	$list_count = count($row);
	$i = $list_count + 1;
	$next_col = 'e' . $i;  
	//echo $next_col;
	
	/****** add pub to db ***********************************/
	$sql2 = mysql_query("UPDATE Pub_Crawl_Order 
	                     SET $next_col = $pub_id
	                     WHERE crawlID = $crawl_id");
: :lol: :) :lol: :D :) :D :) :D


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]