FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP MySQL Question





_AVG_
Hi,
I'm new to MySQL.
So, here's my code:
Code:

UPDATE example_test="EG" WHERE test_field="X"

If I use this code in a PHP document as a MySQL Query will it update all the rows where test_field="X" or only one?
Please help me out here.
Thanks
MrBaseball34
It will update ALL rows where test_field="X"
to limit it to just one row, you need more criteria unless
there is only one row where test_field="X".
kv
There is an error in your syntax. It should have been

Code:
UPDATE example set test_field="EG" WHERE test_field="X"


As far your question, it updates all the rows that match the criteria in your where clause.
_AVG_
Thanks a lot guys.

And thanks for pointing out that crucial error.

Thanks Very Happy
_AVG_
Hi,
I had another question.

Can you put nested loops in MySQL? I mean, is this code correct? Will it work:

Code:

$result = mysql_query("SELECt * FROM test");
while($row = mysql_fetch_array($result))
{
..
..
..
$result = mysql_query("SELECT * FROM test WHERE name = '$row[field]');
while($row = mysql_fetch_array($result))
{
..
..
..
}
..
..
}
MrBlueSky
_AVG_ wrote:
Hi,
I had another question.

Can you put nested loops in MySQL? I mean, is this code correct? Will it work:

Code:

$result = mysql_query("SELECt * FROM test");
while($row = mysql_fetch_array($result))
{
..
..
..
$result = mysql_query("SELECT * FROM test WHERE name = '$row[field]');
while($row = mysql_fetch_array($result))
{
..
..
..
}
..
..
}


No that will not work, because the $result from the outer loop is overwritten in the innerloop. So you have to use different variable names.

Second you have to append $row['field'] to the query string, like this:

Code:

$result1 = mysql_query("SELECt * FROM test");
while($row = mysql_fetch_array($result))
{
  ..
  ..
  ..
  $result2 = mysql_query("SELECT * FROM test WHERE name = '".$row[field]."'");
  while($row = mysql_fetch_array($result2))
  {
    ..
    ..
    ..
  }
  ..
  ..
}
kv
You could. But the problem is since you are using the same variable ($result), the second query would overwrite the variable. So the loop condition also changes, which is not what you may want to do.

You can try with different variables though.

Code:
$result1 = mysql_query("SELECt * FROM test");
while($row = mysql_fetch_array($result1))
{
..
..
..
$result2 = mysql_query("SELECT * FROM test WHERE name = '$row[field]');
while($row = mysql_fetch_array($result2))
{
..
..
..
}
..
..
}
_AVG_
Thanks guys.

Anyway, so the problem was with "$result"? Will there be any problem with the "$row" variable?
MrBlueSky
_AVG_ wrote:
Thanks guys.

Anyway, so the problem was with "$result"? Will there be any problem with the "$row" variable?


Yes, a small syntax error. See my previous post.
MrBaseball34
_AVG_ wrote:
Thanks guys.

Anyway, so the problem was with "$result"? Will there be any problem with the "$row" variable?


No, as you will only be using the $row variable in the inner loop while it is
looping and the $row variable in the outside loop while it is outside the
inner loop.
Fire Boar
Still... it wouldn't hurt to use $row2 or something. I personally use a slightly different syntax for sql queries... this adds an element of debugging.

Code:
$query = "SELECT field FROM table";
if (!$result = @mysql_query($query))
{
  // Code to execute on error... could be anything you like.
}
// Rest of the script


This is pretty handy for completed webpages where you're trying to hide any errors. You might put in something like this, for example:

Code:
$query = "SELECT field FROM table";
if (!$result = @mysql_query($query))
{
  mail('your.email@server.tld', 'MySQL error at your website', 'The following error occurred when trying to perform the query:\n'.$query.'\n\n'.mysql_error());
  require('error.php');
  die();
}
// Rest of the script - this will only be executed if the query was successful.


That would email you the error, and output a page in error.php telling the viewer that something went wrong and the admin has been informed. (Or something.)

The great thing about using @mysql_query() instead of mysql_query() is that the error messages don't show up. So you can have everything going haywire and still get a nice friendly front-end apologising for the error.
manum
the syntax of update command is update tablename set columnname=value where constraint; use this syntax and it shall work
_AVG_
manum wrote:
the syntax of update command is update tablename set columnname=value where constraint; use this syntax and it shall work


I didn't understand fully. Could you give an example please?
shamil
_AVG_ wrote:
manum wrote:
the syntax of update command is update tablename set columnname=value where constraint; use this syntax and it shall work


I didn't understand fully. Could you give an example please?

3rd post to mention.
Code:
UPDATE example set test_field="EG" WHERE test_field="X"
Related topics
PHP, MySQL...
ASP +PHP+MySQL Tutorials
what is your php mysql apache installer package
Need some php/mysql dlls
The Basics (php, mysql etc)
What is useful way in this php+mysql problem?
PHP/Mysql - beginner!
A very good PHP MySQL Tutorial
Setup php,mysql and others...
php, mysql and iis5
 EASY 40 frih$ IF YOU KNOW PHP+MySQL 
Php Mysql security testing
The last foolish PHP/MySQL Question of the Year....
Newbie PHP/MySQL question
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.