Jump to content

Pass array to mysql_fetch_row()


isimpledesign

Recommended Posts

Hi

 

I am looking to be able to pass an array to mysql_fetch_row() not sure where i am going wrong.

 

works fine like this.

 

mysql_select_db($DB_NAME);

$new = mysql_query("SELECT * FROM {$DB_TABLE}");

if (!$new) {
   echo 'MySQL Error: ' . mysql_error();
   exit;
}

$row = mysql_fetch_array($new);

while ($row = mysql_fetch_array($new)) {

echo $row['username'] . $DELIMITER . $row['firstname'] . $DELIMITER . $row['lastname'] . $DELIMITER . $row['password'] . $DELIMITER. "[email protected]" . $DELIMITER . "author";
  echo "<br />";
  
}

 

 

But really need too be able to do it this way.

 

mysql_select_db($DB_NAME);

$new = mysql_query("SELECT * FROM {$DB_TABLE}");

if (!$new) {
   echo 'MySQL Error: ' . mysql_error();
   exit;
}

$row = mysql_fetch_array($new);

echo "<pre>";
//print_r($row);
echo "</pre>";

$sam = array('username','firstname','lastname');

while ($row = mysql_fetch_row($new)) { 

echo $row[$sam] . $DELIMITER;
  echo "<br />";
  
}

So it will just loop through the array so i can keep adding to it by just adding an extra parameter to the array.

 

Any Help Stuck????

Link to comment
https://forums.phpfreaks.com/topic/230101-pass-array-to-mysql_fetch_row/
Share on other sites

really need help any ideas on how i can change this

 

 

while ($row = mysql_fetch_array($new)) {

echo $row['username'] . $DELIMITER . $row['firstname'] . $DELIMITER . $row['lastname'];
  echo "<br />";
  
}

 

 

to something like this

 

$sam = array('username','firstname','lastname');


while ($row = mysql_fetch_array($new)) { 

  echo $row[$sam] . $DELIMITER;
  echo "<br />";
  
}

Try something like this:

<?php
$sam = array('username','firstname','lastname');


while ($row = mysql_fetch_assoc($new)) { 
  $tmp = array();
  foreach ($sam as $v) {
    $tmp[] = $row[$v];
  }
  echo implode($DELIMITER,$tmp) . "<br />\n";
}
?>

 

Ken

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.