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
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".
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.
Thanks a lot guys.
And thanks for pointing out that crucial error.
Thanks 
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))
{
..
..
..
}
..
..
}
|
| _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))
{
..
..
..
}
..
..
}
|
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))
{
..
..
..
}
..
..
} |
Thanks guys.
Anyway, so the problem was with "$result"? Will there be any problem with the "$row" variable?
| _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.
| _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.
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.
the syntax of update command is update tablename set columnname=value where constraint; use this syntax and it shall work
| 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?
| _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" |