php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #1769 mysql() broken
Submitted: 1999-07-20 16:59 UTC Modified: 1999-07-22 20:01 UTC
From: sander at pilon dot com Assigned:
Status: Closed Package: MySQL related
PHP Version: 4.0 Beta 1 OS: Linux 2.2
Private report: No CVE-ID: None
 [1999-07-20 16:59 UTC] sander at pilon dot com
(Submitting it to the bug database right now too.)

Well, simply put - the mysql() call is broken. Example script at the bottom.

<script language="php">
 	function	mysql_test1()
	{
		$r = mysql("tdn_sections","select * from sections");
		echo mysql_error();
		$i=0;
		while($o=mysql_fetch_object($r))
        {
            echo    "$i : $o->url <BR>";
        }
	}
	mysql_test1();
	
	function mysql_test2()
	{
		$c = mysql_pconnect();
		mysql_select_db("tdn_sections",$c);
		$r = mysql_query("select * from sections");
		$i=0;
		while($o=mysql_fetch_object($r))
        {
            echo    "$i : $o->url <BR>";
			$i++;
        }
	}
	mysql_test2();
</script>

Output of 'mysql_test1()':
Warning: MySQL Connection Failed: Unknown MySQL Server Host 'tdn_sections' (111) in /o3dfx/work/php4/hs~mysql_tests.x on line 4
Warning: MySQL: A link to the server could not be established in /o3dfx/work/php4/hs~mysql_tests.x on line 4
Warning: 0 is not a MySQL result index in /o3dfx/work/php4/hs~mysql_tests.x on line 6

Output of 'mysql_test2()':
0 : @POLL:7 
1 : @3DSHOP 
2 : NULL 
3 : NULL 
4 : NULL 
... (correct output)

-----------------------------------------------------

MySQL table 'tdn_sections':
root:/usr/local/lib$ mysqldump tdn_sections
#
# Table structure for table 'sections'
#
CREATE TABLE sections (
  identifier varchar(20) DEFAULT '' NOT NULL,
  section blob,
  contents blob NOT NULL,
  urlname blob,
  url blob,
  added timestamp(14),
  PRIMARY KEY (identifier)
);

#
# Dumping data for table 'sections'
#

INSERT INTO sections VALUES ('tdn_fp1','tdn_fp','5','5','@POLL:7',19980618165020);
... (loads more of similar tables)


PHP.ini contents (mysql related) - NONE! 

Both functions work just fine in PHP3.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-07-22 12:31 UTC] sander at pilon dot com
I managed to track down and fix the mysql() bug. I have no clue who to call,
so I'll just post the bug and solution right here. It's also my fist 10
minutes in the php source :)

Hokay, bug description is here:
http://bugs.php.net/version4/bugs.php?id=1769

What causes this fucker? Well, in mysql_db_query() there is an arguement
count-check, it finds two arguements. (database,query)  There, it calls
php3_mysql_get_default_link(). That's our bad boy.

In php3_mysql_get_default_link() it checks if we already have a mysql
connection id, we dont, so it calls php3_mysql_do_connect().

In php3_mysql_do_connect() we have the real 'bug'. You see, all the time the
parameters to the mysql() call are passed through the functions. So, there
are STILL two arguements, and switch(ARG_COUNT(ht)) returns two. As a result
of that, in the first switch tree, the first arguement (table name) is
stored in the mysql 'host' variable, and the second variable (query) is
stored in the mysql 'user' variable. And, obviously, that's not going to
connect very well.

I fixed (well, hack-fixed, cuz it can be done cleaner.) it by adding a 3rd
variable to php3_mysql_do_connect(), 'usedefaults'. In
php3_mysql_get_default_link() I call php3_mysql_do_connect with usedefaults
= 1, all previous calls I modified so it calls with usedefaults = 0.

Last modification to complete the fix is to skip the entire switch tree in
the connect code if usedefaults == 1.

I leave it to the experts to make a clean fix.

Looking at the code, I'd say *ALL* non '*-connect' mysql functions that
implicitly connect (connect to the 'default' server) are affected by this
bug.
 [1999-07-22 20:01 UTC] zeev at cvs dot php dot net
Fixed in the latest CVS.  Thanks for reporting it!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 09 20:01:32 2024 UTC