PDA

View Full Version : php code check



kill.robot.kill
January 4th, 2004, 04:24 PM
Can't seem to figure out what is causing an error here.

<?php
$user = "****";
$pass = "****";
$db = "***";


$link = mysql_connect("localhost", "$user", "$pass");
if (! $link)
die ("couldn't connect to MySQL");
mysql_select_db($db, $link) or die ("couldn't open $db: ".mysql_error());

$result = mysql_query("SELECT * FROM myDB WHERE id=1");
$a_row = mysql_fetch_array($result);
$num_rows =mysql_num_rows($result);

print "$num_rows Rows \n "; //prints 1 Rows
print $a_row; // prints Array

//each of these below cause an error

foreach($a_row as $key => $value) {
print $key $value;
}

while ($a_row)) {
print("yes");
}

?>

λ
January 4th, 2004, 04:38 PM
corrected:


foreach($a_row as $key => $value) {

print $key . $value;

}

while ($a_row ) ) {
print("yes");

}

kill.robot.kill
January 4th, 2004, 05:02 PM
Looks like I had a couple typos in there, thanks for catching those, but I am still getting errors.

This crashes my browser:

$a_row = mysql_fetch_array($result);
while ($a_row){
print "yes";
}

This works:

while($a_row = mysql_fetch_array($result){
print "yes";
}

I don't understand why the second crashes my browser, and I was hoping to get the foreach to work as well

kill.robot.kill
January 4th, 2004, 05:11 PM
*****EDIT

ok, so I must have had a typo in my foreach because I got that to work.

Back to my post above concerning the second and third examples.

Does the second example crash my browser because its just checking the variable of $a_row? and not the statement of $a_row = mysql.... like in the third example?

hamza84
January 6th, 2004, 08:10 PM
If you use $a_row = mysql_fetch_array as the while condition, it keeps looping until mysql_fetch_array returns false, which it does when there arent any rows left.

If you simply use $a_row as the while condition after you've already used mysql_fetch_array like you did in the first example, the condition will always be true and therefore, you get an infinite loop.

Hope you understand :)

kill.robot.kill
January 6th, 2004, 10:05 PM
thanks for the reply, that's what I thought.

is there anyway to create variable that doesn't act as boolean, but as just a reference...just for the sake of cleaner code?

hamza84
January 6th, 2004, 10:06 PM
Anytime :)

kill.robot.kill
January 6th, 2004, 10:06 PM
holy fast reply!!

hamza84
January 6th, 2004, 10:07 PM
haha, i just happened to be online at the time you replied...

kill.robot.kill
January 6th, 2004, 10:10 PM
incase you missed it above (i edited when you were replying)
is there anyway to create variable that doesn't act as boolean, but as just a reference...just for the sake of cleaner code?

hamza84
January 6th, 2004, 10:12 PM
I didn't understand your question. Tell me what you are trying to achieve and we can go from there.

hamza84
January 6th, 2004, 10:13 PM
oh hold on a minute. are you referring to the while loop?

kill.robot.kill
January 6th, 2004, 10:22 PM
well I really liked how this worked, here it recognizes the value of $a_row, and not whether it evaluates as true or false.

$a_row = mysql_fetch_array($result);
foreach($a_row as $key => $value) {
____print $key $value;
____}


and while using

$a_row = mysql_fetch_array($result);
while ($a_row)) {
print("yes");
}

won't work because it evaluates $a_row as a boolean in the loop, is there a way to make it evaluate as the number of rows in the database?

see I'd like to keep the $a_row variable, because its used in other places, but I want to also use the while loop.

I hope I am making sense.

hamza84
January 6th, 2004, 10:30 PM
what i don't get is the part where you're printing "yes". Thats where I'm gettin confused.

kill.robot.kill
January 6th, 2004, 10:34 PM
that's just a place holder, there will be other code there later

hamza84
January 6th, 2004, 10:36 PM
you could use a for loop to keep a check on the number of rows.

hamza84
January 7th, 2004, 02:11 PM
This is what I had in mind if you want to make things simpler. I've commented out the old code below:




<?php
$user = "****";
$pass = "****";
$db = "***";


$link = mysql_connect("localhost", "$user", "$pass");
if (! $link)
die ("couldn't connect to MySQL");
mysql_select_db($db, $link) or die ("couldn't open $db: ".mysql_error());

$result = mysql_query("SELECT * FROM myDB WHERE id=1");
//$a_row = mysql_fetch_array($result);
$num_rows =mysql_num_rows($result);

//print "$num_rows Rows \n "; //prints 1 Rows
//print $a_row; // prints Array


for($i=1; $i<=$num_rows; $i++){

$a_row = mysql_fetch_array($result);

foreach($a_row as $key => $value) {
print $key.$value;
}

/*You could also use this instead of using foreach():

while(list($key, value) = each($a_row)){
print $key.$value;
}
*/

}
//while ($a_row)) {
//print("yes");
//}

?>


Within the above for loop, you can perform many operations... I don't know if you like the idea though.

kill.robot.kill
January 7th, 2004, 03:12 PM
thanks!!, I hadn't had time to go back and work on this since yesterday. I will play with this later tonight.