473,750 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to debug PHP's mysql_query() function?

Hi

I posted three days ago about a function I wrote which kept refusing
to run a mysql_query.

Well I did away with the function, and hard coded the variables into
the query itself. Here's what I have now:

mysql_query("UP DATE about SET category_id = 58 WHERE about_id = 5") or
die('error: ' . mysql_error());

Just running mysql_query, with no logic, I still dont get an update,
and the script doesn't die. What other things should I be looking at?

Here's another variation of the same thing I've tried:

mysql_query("UP DATE about SET category_id=60 WHERE about_id=5") or
trigger_error(" SQL", E_USER_ERROR);

I've also done: $update = mysql_query("Bl ah blah blah"); and testing
$update gives me nothing. No errors, no update.

Thanks,

J Moore
Nov 2 '05 #1
7 3927
John Moore said the following on 02/11/2005 18:39:
Hi

I posted three days ago about a function I wrote which kept refusing
to run a mysql_query.

Well I did away with the function, and hard coded the variables into
the query itself. Here's what I have now:

mysql_query("UP DATE about SET category_id = 58 WHERE about_id = 5") or
die('error: ' . mysql_error());

Just running mysql_query, with no logic, I still dont get an update,
and the script doesn't die. What other things should I be looking at?


Without seeing the function, it's difficult to say!

Have you tried getting the function to echo the query string that it
creates, and comparing that to what you expect?
--
Oli
Nov 2 '05 #2
On Wed, 02 Nov 2005 19:09:25 GMT, in comp.lang.php Oli Filth
<ca***@olifilth .co.uk> wrote:


Without seeing the function, it's difficult to say!

Have you tried getting the function to echo the query string that it
creates, and comparing that to what you expect?


Like I said, I got rid of the function. Now I'm just dealing with this
snippet of code which won't execute:

mysql_query("UP DATE about SET category_id=63 WHERE about_id=5") or
trigger_error(m ysql_error(), E_USER_ERROR);

I'm running it exactly like that.

I also tried it like this:

$update = mysql_query("UP DATE about SET category_id=63 WHERE
about_id=5") or trigger_error(m ysql_error(), E_USER_ERROR);
if (!$update) {
echo 'Error: ' . mysql_error();
}
echo '<hr>Info: ';
mysql_info($upd ate);
echo '<hr>';

mysql_info returns a warning:

Warning: mysql_info() expects parameter 1 to be resource, boolean
given in c:\Path\to\scri pt.php on line 384

Thanks,

J Moore
Nov 2 '05 #3
John Moore (no****@nomail. com) wrote:
: Hi

: I posted three days ago about a function I wrote which kept refusing
: to run a mysql_query.

: Well I did away with the function, and hard coded the variables into
: the query itself. Here's what I have now:

: mysql_query("UP DATE about SET category_id = 58 WHERE about_id = 5") or
: die('error: ' . mysql_error());

Are you sure you have a record with about_id = 5? It is not an error to
update zero rows. A successful update of zero rows is documented to
return TRUE from mysql_query.

You need to check the row count after running the query.

But I don't have an example handy of doing that, so you'll have to look it
up. I suspect that google can find examples of php mysql updates and
checking the row count afterwards.
--

This programmer available for rent.
Nov 2 '05 #4
John Moore wrote:

$update = mysql_query("UP DATE about SET category_id=63 WHERE
about_id=5") or trigger_error(m ysql_error(), E_USER_ERROR);
if (!$update) {
echo 'Error: ' . mysql_error();
}
echo '<hr>Info: ';
mysql_info($upd ate);
echo '<hr>';

mysql_info returns a warning:

Warning: mysql_info() expects parameter 1 to be resource, boolean
given in c:\Path\to\scri pt.php on line 384


Well, you're running it on MS-Windows - thats not good for starters.

Try switching on the replication log in mysql and see if it changes size /
timestamp when you run the query (you can also convert the log to a
readable format - RTFM for more details).

mysql_info(), like all the mysql_ fns should use the default...accor ding to
the manual (same for both _info & _query):

: If by chance no connection is found or established,
: an E_WARNING level warning is generated.

....but of course you read that already. It seems most peculiar that the
script is getting that far the way you've written it (the connection exists
at line 1, becuase you're not getting a fatal error, yet nothing is
(allegedly) getting updated, then there is no connection at line 5. I guess
your installation might be fscked.

C.
Nov 2 '05 #5
On 2 Nov 2005 12:16:30 -0700, in comp.lang.php
yf***@vtn1.vict oria.tc.ca (Malcolm Dew-Jones) wrote:

Are you sure you have a record with about_id = 5? It is not an error to
update zero rows. A successful update of zero rows is documented to
return TRUE from mysql_query.

You need to check the row count after running the query.

But I don't have an example handy of doing that, so you'll have to look it
up. I suspect that google can find examples of php mysql updates and
checking the row count afterwards.


Well thanks for trying. I will try your suggestion for checking the
row count.

Maybe it will help if I put this into some context.

In this script there are 5 queries, and the third one refuses to run
or kill the script. I've tested this over and over, within a function
and outside of the function. I've done var_dumps and echoed out the
queries and variables at every step of the script, but still the third
query refuses to run.

It's a simple update query which works perfectly at the command line:

UPDATE about SET category_id=65 WHERE about_id=5;

Here it is in the context of my current version of the script:

// create the category
$cat_name = $_SESSION['cat_name'];
$insert_query = "INSERT INTO about_category (category_id, cat_name,
total_pages) VALUES ('', '$cat_name', 0)";
$insert_result = mysql_query($in sert_query) or die(mysql_error ());

// get the new category_id
$select_query = "SELECT category_id AS new_id FROM about_category
WHERE cat_name ='$cat_name'";
$select_result = mysql_query($se lect_query) or die(mysql_error ());
$new_id = mysql_result($s elect_result, 0, 'new_id');

// This is the query that refuses to run
// update the page
$update_sql = "UPDATE about SET category_id=65 WHERE about_id=5";
$update_result = mysql_query($up date_sql) or die(mysql_error ());

// update the category totals
include_once('i nc/about_inc2.php' );
update_cat_tota l('remove', $_SESSION['category_id']);
update_cat_tota l('add', $new_id);

I have this written as a set of procedures without any logic because I
thought it would be easier to see why the third query won't run. Once
I get this bug fixed I'll go back and add some logic to test all the
results, and put the code back into a function.

Here is the layout of the two tables; about and about_category:

CREATE TABLE about (
about_id int(10) unsigned NOT NULL auto_increment,
category_id int(10) unsigned NOT NULL default '0',
name varchar(35) NOT NULL default '',
display enum('show','hi de') NOT NULL default 'show',
text text NOT NULL,
meta_data_id int(10) unsigned NOT NULL default '0',
PRIMARY KEY (about_id)
) TYPE=MyISAM;

CREATE TABLE about_category (
category_id int(10) unsigned NOT NULL auto_increment,
cat_name varchar(35) NOT NULL default '',
total_pages tinyint(3) unsigned NOT NULL default '0',
PRIMARY KEY (category_id)
) TYPE=MyISAM;

Also, the final two updates take place in another function, which
works every time:

function update_cat_tota l($task, $category_id) {
// get the current count for total pages
$query = "SELECT total_pages FROM about_category WHERE category_id =
'$category_id'" ;
$result = mysql_query($qu ery) or trigger_error(" SQL", E_USER_ERROR);
if ($result) {
$total_pages = mysql_result($r esult, 0, 'total_pages');
// $task may be 'add' or 'remove'
switch ($task) {
case 'add':
$expression = $total_pages + 1;
break;
case 'remove':
if ($total_pages > 0) {
$expression = $total_pages - 1;
}
else {
$expression = 0;
}
break;
}

$query = "UPDATE
about_category
SET
total_pages = $expression
WHERE
category_id = '$category_id'" ;

$result = mysql_query($qu ery) or trigger_error(" SQL",
E_USER_ERROR);

if ($result) {
return true;
}
else {
return false;
}
}
else {
die('<hr>No result from query: ' . $query . ' Error: ' .
mysql_error() . '<hr>');
}
}

I may not be able to code php very well, but I make a damn good plate
of spaghetti.
J Moore
Nov 2 '05 #6
On Wed, 02 Nov 2005 21:04:54 GMT, in comp.lang.php Colin McKinnon
<co************ **********@ntlw orld.deletemeun lessURaBot.com> wrote:


Well, you're running it on MS-Windows - thats not good for starters.

Thanks, but it behaves the same way in Slackware9.1, PHP 4.3.2,
mysql4.0.13, Apache1.3.24
Try switching on the replication log in mysql and see if it changes size /
timestamp when you run the query (you can also convert the log to a
readable format - RTFM for more details).

mysql_info() , like all the mysql_ fns should use the default...accor ding to
the manual (same for both _info & _query):

: If by chance no connection is found or established,
: an E_WARNING level warning is generated.

...but of course you read that already. It seems most peculiar that the
script is getting that far the way you've written it (the connection exists
at line 1, becuase you're not getting a fatal error, yet nothing is
(allegedly) getting updated, then there is no connection at line 5. I guess
your installation might be fscked.

C.


This gives me something to go on- I really appreciate it and will
check it out.
J Moore
Nov 2 '05 #7
John Moore wrote:
Hi

I posted three days ago about a function I wrote which kept refusing
to run a mysql_query.

Well I did away with the function, and hard coded the variables into
the query itself. Here's what I have now:

mysql_query("UP DATE about SET category_id = 58 WHERE about_id = 5") or
die('error: ' . mysql_error());

Just running mysql_query, with no logic, I still dont get an update,
and the script doesn't die. What other things should I be looking at?

Here's another variation of the same thing I've tried:

mysql_query("UP DATE about SET category_id=60 WHERE about_id=5") or
trigger_error(" SQL", E_USER_ERROR);

I've also done: $update = mysql_query("Bl ah blah blah"); and testing
$update gives me nothing. No errors, no update.


after the query, run something like:
$rows = mysql_affected_ rows()

if var_dump(mysql_ query(...)) shows boolean true, then the query was a
success, then if $rows > 0, something was changed in the database, if
-1, the query failed, and 0 indicates a successful query with nothing
updated. if 0, perhaps the row already had those values or no rows have
the about_id = 5...

Also, I have a tendency to include the link identifier when using the
database functions just so that I know exactly which connection I am
working with (even if there is only one in the script) - it just removes
some more unknowns for debug time.

--
Justin Koivisto, ZCE - ju****@koivi.co m
http://koivi.com
Nov 2 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1920
by: no-email | last post by:
Hi, Below is a bunch of programs to demo the basic sql commands. Suggestions on possible improvements will be appreciated. <?php // mysql-demo.php include ("connectdb.php"); $setSQL="SET FOREIGN_KEY_CHECKS=0"; $dropSQL="DROP TABLE IF EXISTS temp";
3
2943
by: josh.kuo | last post by:
Sorry about the subject, I can't think of a better one. I recently wrote some PHP classes that I think might be of interest to this group. Since I have been reaping the benefits of reading news groups for years, I figure it's time for me to contribute a little bit back, maybe some people out there will find this useful. * Introduction This is a "how-to" style article, showing by example how to dynamically
1
2095
by: danxavier | last post by:
I successfuly installed dd.php and sajax.php files. It runs fine, but I would like to link the $items to an image. I called the field in mysql with the link "pic". Any help would be AWESOME!!! I've run out of ideas. Here's the script <?php /* CREATE TABLE `layout` ( `set` varchar(50) NOT NULL default '', `item` varchar(50) NOT NULL default '', `order` int(9) NOT NULL default '0',
3
2147
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when Record (i) is shown and modified, the change will come to Record (i+1). Can anyone provide suggestion? thanks.
2
2578
by: Tarik Monem | last post by:
OK! I've gone through a few tutorials and I cannot understand what I'm doing wrong casting_registration.php <table> <tr> <td> <form enctype="multipart/form-data" action="thankyou.php" method="post" name="registrationform"> Choose a shows:
8
9645
by: seni786 | last post by:
Hi i am having problems with some code that i wrote out for a shopping cart. the shopping cart it self works when a product is added but when the 'CART CONTENT' button is clicked on it comes up with an error. the error is Notice: Undefined index: action in D:\www\students\khank\WebsiteFinal1SHAZAD\cart.php on line 5 the code that i have written for this is <?php
0
3119
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td colspan="2"><p>
1
4366
George Lft
by: George Lft | last post by:
ok, first of all, i built my register page using dreamweaver tool which the codes haven been out of control. Now i'm thinking that turning over everything - by using this another set of codes. And these new sets of codes have overwhelmed me a bit. Here's the new code: CREATE TABLE `users` ( `ID` int(11) NOT NULL auto_increment, `Username` varchar(255) NOT NULL, `Password` varchar(255) NOT NULL, `Temp_pass` varchar(55)...
39
5866
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f ISO-8859-1 -t UTF-8 mydb.sql mydb_utf8.sql mysqlCREATE DATABASE mydb_utf8 CHARACTER SET utf8 COLLATE utf8_general_ci;
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.