Jump to content

Recommended Posts

Hi. I've just started working on a PHP (5.2.1) project and am having a hard time fetching rows as arrays in a while loop from my database (mySQL 4.1.22).

 

The code below works perfectly if I use *if* on the marked line, and handles the first row of the data set as I would expect. But when I change it to *while*, nothing happens, just punctuation marks on the screen and when I check the table with phpMYAdmin, nothing changes (I reset the database manually between tests). Any suggestions?

 

<?php

 

include ('../string_link.php'); # Set error reporting and connect to database

 

$query = 'SELECT ek, prform FROM dchords';

$result = mysql_query ($query) or die (mysql_error());

 

if ($row = mysql_fetch_assoc ($result)); { # WORKS PERFECTLY WITH "if", BUT NOT "while"

 

  $prform=$row['prform'];

  $ek=$row['ek'];

  $v_beauty=$prform+1;

  print "$ek: $prform, $v_beauty. ";

 

  $query = "UPDATE dchords SET v_beauty=$v_beauty WHERE ek=$ek LIMIT 1 ;";

  mysql_query ($query);                                                   

  }

 

?>

 

p.s., The fields are all integers, and I only have 7 rows of data in the table for testing. Default value of $v_beauty is NULL.

Link to comment
https://forums.phpfreaks.com/topic/133400-solved-newbie-stumped/
Share on other sites

haha change  mysql_fetch_assoc() to mysql_fetch_array() and it should work with the while :D

 

<?php

include ('../string_link.php'); # Set error reporting and connect to database

$query = 'SELECT ek, prform FROM dchords';
$result = mysql_query ($query) or die (mysql_error());

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

  $prform=$row['prform'];
  $ek=$row['ek'];
  $v_beauty=$prform+1;
  print "$ek: $prform, $v_beauty. ";

  $query = "UPDATE dchords SET v_beauty=$v_beauty WHERE ek=$ek LIMIT 1 ;";
  mysql_query ($query);                                                   
  }

?>

<?php

include ('../string_link.php'); # Set error reporting and connect to database

$query = 'SELECT ek, prform FROM dchords';
$result = mysql_query ($query) or die (mysql_error());

while($row = mysql_fetch_assoc ($result)) { # WORKS PERFECTLY WITH "if", BUT NOT "while"

  $prform=$row['prform'];
  $ek=$row['ek'];
  $v_beauty=$prform+1;
  print "$ek: $prform, $v_beauty. ";

  $query = "UPDATE dchords SET v_beauty='$v_beauty' WHERE ek='$ek' LIMIT 1 ;";
  mysql_query ($query);                                                   
  }

?>

 

In your update statement you need the single quotes around the data so it does not throw errors, especially if it is in a string format.

 

Also you have a semicolon after the if statement. Above is an updated code and should work.

 

As per the remark above, mysql_fetch_assoc will work fine, and if you are not planning on also using the index in the array is better because it does not return both assoc array and and index array which will double the amount of data coming into the script.

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.