468,527 Members | 2,092 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,527 developers. It's quick & easy.

My function returns true but my code does not execute

Hi,

I must be missing something obvious.

I cannot get this function to run the update query on Line 6, although
the call to run the query evaluates true, and both update_cat_total
functions execute, while the function itself returns true.

A var_dump shows that all the variables are valid, and I can run the
exact same update query at the mysql command line without errors. I've
echoed mysql_error() before and after the call to mysql_query() and no
errors show up.

But the update itself never runs! Can anyone see why?

function new_category($category_id=null) {
global $category_id_new;

if (create_category($_SESSION['cat_name'])) {
// update this page with the new category_id
$query = "UPDATE
about
SET
category_id = $category_id_new
WHERE
about_id = $_SESSION[about_id]";

if ($result = mysql_query($query)) {
if (update_cat_total('add', $category_id_new) &&
update_cat_total('remove', $category_id)) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
else {
return false;
}
}

Thanks
J Moore
Oct 31 '05 #1
11 1990
>$query = "UPDATE
about
SET
category_id = $category_id_new
WHERE
about_id = $_SESSION[about_id]";


I think you want "about_id = $_SESSION['about_id']"; (notice the quotes
around 'about_id'). To be safe, you can also write it like this:

$query = "UPDATE about SET catergory_id = $catergory_id_new
WHERE about_id = " . $_SESSION['about_id'];

Oct 31 '05 #2
John Moore wrote:
$query = "UPDATE
about
SET
category_id = $category_id_new
WHERE
about_id = $_SESSION[about_id]";
You can't use multi-line strings like that in PHP. I presume in your code
it is actually written:
$query = "UPDATE about SET category_id = $category_id_new WHERE about_id = $_SESSION[about_id]";


Then you can't use array values like that in strings. Zeldor was partly
right that it needs quotes around the key (because that is a string, not a
constant), but it also needs curly braces around the whole name if you are
going to use it inside the string:

$query = "... about_id = {$_SESSION['about_id']}";

For goodness sake, see
http://php.net/manual/en/language.ty...string.parsing

--
E. Dronkert
Oct 31 '05 #3
I use multi-line strings like that all the time. Obviously I use curly
braces, quotes etc correctly (by the second or third attempt) but
there's nothing to stop you using lines like
db_query("CREATE TABLE {$sys['test_db_table_prefix']}idx (
idx_uid MEDIUMINT NOT NULL AUTO_INCREMENT,
idx_added_user VARCHAR(255),
idx_added_dtm DATETIME,

idx_type VARCHAR(255),
idx_category VARCHAR(255),
idx_subcat VARCHAR(255),

idx_name VARCHAR(255),
idx_company VARCHAR(255),
idx_phone VARCHAR(255),
idx_email VARCHAR(255),

idx_street VARCHAR(255),
idx_area VARCHAR(255),
idx_town VARCHAR(255),
idx_near VARCHAR(255),
idx_region VARCHAR(255),
idx_country VARCHAR(255),

idx_area_search VARCHAR(255),
idx_skill_search VARCHAR(255),

idx_other1 VARCHAR(255),
idx_other2 VARCHAR(255),
idx_other3 VARCHAR(255),
idx_other4 VARCHAR(255),
idx_other5 VARCHAR(255),
idx_other6 VARCHAR(255),
idx_other7 VARCHAR(255),
idx_other8 VARCHAR(255),
PRIMARY KEY (idx_uid)
)
");

Oct 31 '05 #4
Of course, it doesn't usually wrap like that

Oct 31 '05 #5
On Mon, 31 Oct 2005 09:54:48 +0100, in comp.lang.php Ewoud Dronkert
<fi*******@lastname.net.invalid> wrote:
John Moore wrote:
$query = "UPDATE
about
SET
category_id = $category_id_new
WHERE
about_id = $_SESSION[about_id]";


You can't use multi-line strings like that in PHP. I presume in your code
it is actually written:
$query = "UPDATE about SET category_id = $category_id_new WHERE about_id = $_SESSION[about_id]";


Then you can't use array values like that in strings. Zeldor was partly
right that it needs quotes around the key (because that is a string, not a
constant), but it also needs curly braces around the whole name if you are
going to use it inside the string:

$query = "... about_id = {$_SESSION['about_id']}";

For goodness sake, see
http://php.net/manual/en/language.ty...string.parsing


Thanks,

I changed the query to read like this:

$query = "UPDATE about SET category_id = $category_id_new WHERE
about_id = {$_SESSION['about_id']}";

All on one single line.

But no dice- no update occurs! I've echoed out the query itself, and
the variables are showing up as they should:

UPDATE about SET category_id = 31 WHERE about_id = 5

I wrote this function to execute 2 more functions IF this query
evaluates true. Both these functions are executing, but the
category_id field is not updating.

I dont mind rewriting my code, but I wish I could understand why when
it doesn't work as expected.

Thanks to all of you for trying to help.

J Moore
Oct 31 '05 #6
Try something along the lines of

$result = mysql_query($sql) or die("<br />Could not run query $sql
Reason: " . mysql_error());

to find out what it's complaining about.

($result = mysql_query($query)) will always return true even if the
query fails.

Ian

Oct 31 '05 #7
>But no dice- no update occurs! I've echoed out the query itself, and
the variables are showing up as they should:

UPDATE about SET category_id = 31 WHERE about_id = 5


Did you try taking the query that get's echoed out and pasting into
some other MySQL client (i.e. the command line version) ?

Oct 31 '05 #8
On 31 Oct 2005 05:46:12 -0800, in comp.lang.php "Ian B"
<ia********@gmail.com> wrote:
Try something along the lines of

$result = mysql_query($sql) or die("<br />Could not run query $sql
Reason: " . mysql_error());

to find out what it's complaining about.

Thanks Ian,

I just tried your suggestion, calling the query like this:

$result = mysql_query($query) or die("<br>Could not run query $sql
Reason: " . mysql_error());

But the script didn't die, the function continued to execute, and no
update took place.
($result = mysql_query($query)) will always return true even if the
query fails.

Ian


I tested the result like this:

if ($result) {
// keep doing stuff
}
else {
return false;
}

Here is the current version of this function:

function new_category2($category_id=null) {
global $category_id_new;

if (create_category($_SESSION['cat_name'])) {
// update this page with the new category_id
$query = "UPDATE about SET category_id = $category_id_new WHERE
about_id = {$_SESSION['about_id']}";

$result = mysql_query($query) or die("<br>Could not run query $sql
Reason: " . mysql_error());
if ($result) {
// both new and old categories must be updated to reflect new
totals
if (update_cat_total('add', $category_id_new) &&
update_cat_total('remove', $category_id)) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
else {
return false;
}
}

I'm going to try this under a different operating system. I'm just not
understanding why the update wouldn't occur here. This is PHP 4.4.3
running mysql 4.0.16-nt on Apache 1.3.24.

What kills me is that the update_cat_total() function updates a
different table using the exact same syntax for the query- and updates
the field every time.

Thanks again,
J Moore
Oct 31 '05 #9
On 31 Oct 2005 06:31:42 -0800, in comp.lang.php "ZeldorBlat"
<ze********@gmail.com> wrote:
Did you try taking the query that get's echoed out and pasting into
some other MySQL client (i.e. the command line version) ?


Thanks,

Yeah- I'm keeping the client open so I can run the update manually as
needed. Every time this update fails, it throws my totals off, so I
have to update manually every time I run the script. I'm up to my 35th
try now.
J Moore
Oct 31 '05 #10
Another thought...

Have you actually got an about_id equal to 5 (or whatever) because if
you haven't, then the query will not raise an error but also not update
anything.

and is about_id numeric? (Off the top of my head, I'm not sure what
you'd get if it was a VARCHAR, but it's easier for you to check than
for me to test :-)

Ian

Oct 31 '05 #11
On 31 Oct 2005 06:58:49 -0800, in comp.lang.php "Ian B"
<ia********@gmail.com> wrote:
Another thought...

Have you actually got an about_id equal to 5 (or whatever) because if
you haven't, then the query will not raise an error but also not update
anything.

and is about_id numeric? (Off the top of my head, I'm not sure what
you'd get if it was a VARCHAR, but it's easier for you to check than
for me to test :-)

Ian


Hi Ian,

All the id fields are int.

But I just found a problem with my global variable at the beginning of
the function. I'm going to get rid of it and try to add that variable
as a parameter.

I think the update is running, it's the variable that isn't changing.

Thanks again,
J Moore
Oct 31 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Penn Markham | last post: by
2 posts views Thread by Highlander416 | last post: by
2 posts views Thread by laredotornado | last post: by
5 posts views Thread by Raphael Bauduin | last post: by
53 posts views Thread by souporpower | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.