Re: RES: [PHP-DB] input field validation
Ok...so, I'm a couple days behind... [somewhat OT tidbit] !isNaN(null) will also return true since null is parsed as 0. Now, if you're going to use a String search, might as well use something built-in that does just that. First advantage: shorter code and smaller files. Also, RegExp's usually (but, no always) parse faster than charAt loops. Javascript: function isInteger(value) { // accepts untrimmed strings return (/^\s*[0-9]+\s*$/).test(String(value)); } function findInteger(value) { return String(value).replace(/^[^0-9]*([0-9]*).*?$/, '$1'); } Tests: var i, j; var funcs = [isInteger, findInteger]; var input = ['12 ', '12sds', 'fhe34', 'a12.3b', '1abc2', 99, 2.4, null]; for (i = 0; i funcs.length; i += 1) { for (j = 0; j input.length; j += 1) { document.write(funcs[i](input[j]), '; '); } document.write('br'); } Output: true; false; false; false; false; true; false; false; 12; 12; 34; 12; 1; 99; 2; ; Usage: input type=text onblur=this.value=findInteger(this.value); /!-- silently strips invalid characters -- span(integers only)/span function validate() { if (!isInteger(FormObject.input1.value)) { alert('Please enter an integer for ...'); } } - Jon L. On Mon, May 12, 2008 at 11:47 AM, YVES SUCAET [EMAIL PROTECTED] wrote: Nope, it's not. The problem was formulated as Integer detection. not numerical detection in general. isNaN will return positive for e.g. floating point values as well. The solutions described in this thread all result in integer-only values. All solutions presented are still off-topic, of course... Yves -- Original Message -- Received: Mon, 12 May 2008 11:12:02 AM CDT From: Thiago Pojda [EMAIL PROTECTED] To: 'sahabettin akca' [EMAIL PROTECTED], php-db@lists.php.net Subject: RES: [PHP-DB] input field validation It'd be simpler to use isNaN() Thiago Henrique Pojda Desenvolvimento Web +55 41 3033-7676 [EMAIL PROTECTED] Excelência em Softwares Financeiros -Mensagem original- De: sahabettin akca [mailto:[EMAIL PROTECTED] Enviada em: domingo, 11 de maio de 2008 14:31 Para: php-db@lists.php.net Assunto: Re: [PHP-DB] input field validation hi, *javascript function : function isNumber(obj) { var len = obj.value.length; var lastChar = obj.value.charAt(len-1); if( lastChar != '0' lastChar != '1' lastChar != '2' lastChar != '3' lastChar != '4' lastChar != '5' lastChar != '6' lastChar != '7' lastChar != '8' lastChar != '9' ) { obj.value = obj.value.substring(0, len-1); } } this is function use : input *onKeyUp='isNumber(this)' / do not alert , but add alert * obj.value = obj.value.substring(0, len-1); --- this line before --- alert('this field only integer!'); * add. good luck ?ahabettin akca (saho) 2008/5/11 arafat uddin [EMAIL PROTECTED]: - and this database field data type select int (integer) , tinyint, int, bigint... or if(gettype($_POST['field'])!=integer) // or if(intval($_POST['field'])==0) or if(!intval($_POST['field'])) { printnot integer; location(header: .php); #or printmeta http-equiv='refresh' content='0;url=.php'; } else { ... } 2008/5/11 Yves Sucaet [EMAIL PROTECTED]: Allow me to point out that this is not database-related... This website should get you everything you want: http://www.acmesoffware.com/acme/ExamplesJS/jsExm_ValidateInteger.asp HTH, Yves - Original Message - From: arafat uddin [EMAIL PROTECTED] To: php-db@lists.php.net Sent: Sunday, May 11, 2008 12:48 AM Subject: [PHP-DB] input field validation hi, i want to input only integer in my field.if without integer any input put in the field it will show warning messege. such as 12 will be accept but 12sds will not accept . fhe34 will not accept plz help me by any javascript code. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- ?ahabettin akca // saho -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Why $row is EMPTY
I could be wrong, but I don't think table aliases continue to exist in PHP; only the column names. $g_name = $row[gigName]; $vname = $row[venueName]; $genre = $row[name]; You may also consider revising the query to only grab the columns you need, using an alias for at least genre.name. $query = 'SELECT gig.gigName AS gig, venue.venueName AS venue, genre.name AS genre FROM `gig` LEFT JOIN genre ON gig.genreId=genre.genreId LEFT JOIN venue ON gig.venueID = venue.vid where gig.gigid = '.$gigDetail.' ORDER BY gig.gigid'; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo Program is in While loop; $g_name = $row[gig]; $vname = $row[venue]; $genre = $row[genre]; echo(Gig Name: .$g_name); } - Jon L. On Tue, Apr 29, 2008 at 1:27 PM, Nasreen Laghari [EMAIL PROTECTED] wrote: Hi, Why my program is not going in while loop? When I run the same query in SQL cmd, it brings result but here when I print $result gives Resouce ID number means $result has data then why $row is empty. $query = 'SELECT * FROM `gig` LEFT JOIN genre ON gig.genreId=genre.genreId LEFT JOIN venue ON gig.venueID = venue.vid where gig.gigid = '.$gigDetail.' ORDER BY gig.gigid'; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { echo Program is in While loop; $g_name = $row[gig.gigName]; $vname = $row[venue.venueName]; $genre = $row[genre.name]; echo(Gig Name: .$g_name); } Regards Nasreen Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
Re: [PHP-DB] Dynamic Navigation with Limit
1) Something that stands out is one of your links: if ($screen 0) { print a href=\view.php?$screen=.( $screen+1).\Previous Entries/a ; } The '$' in '?$screen' is probably not helping, as that'll probably translate to '?0=1' (or similar). And, don't you want that to be ($screen - 1)?? 2) Depending on what version of PHP you're running. You're assuming that $screen will equal $_GET['screen']. Which, since 4.2.0 and with default settings, is a bad assumption. if ($screen != $_GET['screen']) $screen = $_GET['screen']; // or just use the assignment. There's a register_globals setting you can change (default is false): http://php.net/ini.core#ini.register-globals But, changing it to true also has security implications: http://php.net/security.globals Then again, you're also referencing a lot of variables that aren't defined in your example. So, it's hard to know what you may be doing that you just didn't post. 3) You're basing $pages on the ceil([number of rows] / [rows per page]). Problem with this, the LIMIT won't allow [number of rows] to ever be greater than [rows per page]. So, $pages will never be anything besides 0 or 1. You may want to try running a 2nd, similar query, without the LIMIT, using COUNT(*). (just in case: http://dev.mysql.com/doc/refman/5.0/en/counting-rows.html) 4) You have an extra '}' after the while scope that doesn't line up with anything. Since you stated the page loads, I'm assuming that's just because it's a snippet. - Jon L. On Thu, Apr 3, 2008 at 6:57 PM, Nasreen Laghari [EMAIL PROTECTED] wrote: Hi, I'm trying to put limit of data fetch as well as dynamic navigation. Below is the try which I did but something is going wrong as $screen value is not increasing so when I click on NEXt button it refreshes the same page and Previous button does even show. Could you kindly have look on the coding and help me where i'm mistaking. Regards Nasreen ?php $rows_per_page = 10; $i=0; if (!isset($screen)) $screen=0; $start = $screen * $rows_per_page; $sql = SELECT * FROM gig g, venue v WHERE g.gigName LIKE '%.$gig_name.%' OR g.gig_date LIKE '%.$sdate.%' OR g.genre LIKE '%.$genre.%' OR g.ticket_price LIKE '%.$ticket_price1.%' OR g.ticket_price LIKE '%.$ticket_price2.%' OR v.venueName LIKE '%.$vname.%' OR v.vCity LIKE '%.$city.%' order by gig_Date LIMIT $start,$rows_per_page; $result = mysql_query($sql) or die(Query error: . mysql_error()); $num_rows = mysql_num_rows($result) ; $pages = ceil($num_rows / $rows_per_page); $j=0; while ($row = mysql_fetch_array($result)) { global $limit; $j = $j+1; $gigid = $row['gigid']; $gigname = $row['gigName']; $sdate = $row['gig_fdate']; $fdate =$row['gig_tdate']; $genre = $row['genre']; $ticket_price = $row['ticket_price']; $gigdetail= $gigid; echo(br $gigid a href='detail.php?gigDetail=$gigid' $gigname/a); } } if ($screen 0) { print a href=\view.php?$screen=.( $screen+1).\Previous Entries/a ; } else if ($screen $pages) { $screen = $screen+1; print a href=\view.php?screen=.($screen).\Next Entries/a ; } ? You rock. That's why Blockbuster's offering you one month of Blockbuster Total Access, No Cost. http://tc.deals.yahoo.com/tc/blockbuster/text5.com
Re: [PHP-DB] Problem when using SQL_CUR_USE_ODBC on connect
(This isn't a solution, per se...just a suggestion.) I don't know how they compare; I've never personally used the ODBC functions... But, you may give the MSSQL functions a try: http://php.net/mssql - Jon L. On Mon, Mar 31, 2008 at 3:27 PM, cfs [EMAIL PROTECTED] wrote: I'm using PHP with Apache. PHP code connects to MS SQL server using ODBC. I'm doing a query against a table that is very simple: one column of the real data type, one of the text data type. The text field is set to testing 1,2,3. The real column is set to 10.015. When I use the default connect options, I get both values back fine. When I use SQL_CUR_USE_ODBC, which I very much want to use, then the text column data comes back as boolean(false). Does anyone know of a solution? Table: CREATE TABLE [dbo].[test1]( [ID] [int] IDENTITY(1,1) NOT NULL, [real1] [real] NULL, [text1] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_test1] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] Code: ?php $conn = odbc_connect(db-name, user-name,password, SQL_CUR_USE_ODBC); $result = odbc_exec($conn, select * from test1); if (odbc_fetch_row($result)) { print Values: . odbc_result($result,real1) . , . odbc_result($result,text1); } ? -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] resources and resource types
Here's the manual: http://php.net/language.types.resource Also: http://php.net/resource An entire list of all possible resource types and respective functions. Read up, man. - Jon L. On Mon, Mar 31, 2008 at 1:39 PM, Richard Dunne [EMAIL PROTECTED] wrote: When I did a search on resource(5) within the PHP online documentation, I found the resource page resource.php. While although it lists all the resources used with mysql, it does not mention how resources are related to resource numbers as above. A query such as a row count on a table results in an integer value. What is the best way of accessing or converting the integer value from the resource? I can't see any particular function in the function list which does this, without getting into strings or arrays, or have I missed something blaringly obvious, hope not? -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] resources and resource types
What I get for not refreshing. ;) Sorry, man. Didn't know you already gave the links. - Jon L. On Mon, Mar 31, 2008 at 1:56 PM, Andrés G. Montañez [EMAIL PROTECTED] wrote: The ID of the resource changes on every new resource. There is no co-relation between resources and resources id. The only thing you can get is the Type of the Resource, read: * http://www.php.net/manual/en/language.types.resource.php * http://www.php.net/manual/en/resource.php * http://www.php.net/manual/en/function.get-resource-type.php -- Atte, Andrés G. Montañez PHP Senior Técnico en Telecomunicaciones Montevideo - Uruguay -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: [PHP-DB] numeric string to single digit array
Not sure if this is relevant anymore, but... i.e. 1223123 into ['1','2,','2','3','1,'2','3'] ? $num = 1223123; $nums = array_filter(preg_split('//', $nums)); Or you can use this function. It's probably better since the array_filter will probably get rid of any 0's in the string. function string_to_array($str) { $arr = preg_split('//', $str); array_shift($arr); // removing leading null from split array_pop($arr); // remove training null from split return $arr; } Or, if you just want the numbers from your student id (and you wanted only numbers): $num = A123456789; $nums = string_to_array(preg_replace(/[^0-9]+/, , $nums)); - Jon L. On Wed, Mar 26, 2008 at 1:35 PM, Richard Dunne [EMAIL PROTECTED] wrote: Using this extract from http://ie.php.net/manual/en/control-structures.foreach.php Amaroq 09-Mar-2008 06:40 Even if an array has only one value, it is still an array and foreach will run it. ?php $arr[] = I'm an array.; if(is_array($arr)) { foreach($arr as $val) { echo $val; } } ? The above code outputs: I'm an array. - So if I use: $query = Select answer from answers where studentID='A123456789'; $result = mysql_query($query,$connection) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { foreach($row as $answer) { echo $answer . \n; } } I thought I would be able to print out each array element, but I am not getting any output. Has anyone got a better idea? -- Forwarded message -- From: Evert Lammerts [EMAIL PROTECTED] To: Richard Dunne [EMAIL PROTECTED] Date: Wed, 26 Mar 2008 18:22:53 +0100 Subject: Re: [PHP-DB] numeric string to single digit array Tried that as well and got the same result. I tried Select count(answer) as total from answers where studentID='A123456789'; from the CLI and got total = 2 as a result. So, we got rid of the Invalid Resource error and we know that the student id you use occurs in both rows in your table and that your query works fine. Did you get rid of the semicolon @ line 15 while($row = mysql_fetch_assoc($result));, as Jason suggested? Also, an: error_reporting(E_ALL); at the top of your code might help in backtracing. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Re:Multiple values in SELECT query
Now I've spoon fed you Damn, Neil...you're a d-bag. Next time you go to offer help, it may assist you to know that most people don't respond well to being talked down to. (obviously, I'm contradicting my own advice, here; but, I doubt you'll learn from it, anyways) Now, Ron... I'm hoping the following can add a few extra tidbits for you over what Matt already explained. The initial error, I think, was an assumption that natural or spoken language translates directly into SQL. Or, that you'd say: does variable equal 1 or 2. Makes sense to us, right? ;) Now, I could be wrong about that. But, regardless, SQL read your query as: (does variable equal 1) or (is 2 non-zero). And, off course, 2 isn't 0...so that'll always return true; making the 'OR' return true as well. You'll have to associate the variable with each possible match: WHERE (variable = 1) OR (variable = 2) Or simplify it, as Matt suggested, using IN: WHERE variable IN (1, 2) Did that help any? - Jon L. On Mon, Mar 10, 2008 at 3:40 PM, Neil Smith [MVP, Digital media] [EMAIL PROTECTED] wrote: At 19:34 09/03/2008, you wrote: From: Ron Piggott [EMAIL PROTECTED] Date: Sun, 09 Mar 2008 15:34:05 -0400 Message-Id: [EMAIL PROTECTED] What is the correct syntax for where the results may be 1 or 2? What have I done wrong? SELECT * FROM table WHERE name LIKE ABC AND listing_type = 1 or 2 C'mon Ron, this is basic SQL. The query you provided would have given all rows where name was like 'abc', listing type was 1, then returned all rows because `OR 2` results in a value of 'true' which matches everything (you could also have written OR 2 = 2 with the same effect) SELECT * FROM table WHERE name LIKE ABC AND (listing_type = 1 OR listing_type = 2); SELECT * FROM table WHERE name LIKE ABC AND listing_type IN (1, 2); Now I've spoon fed you, please read the manual on Operator Precedence which explains how to combine stuff like this so you don't get the wrong result http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html : To override this order and group terms explicitly, use parentheses (first query above) Cheers - Neil -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Re:Multiple values in SELECT query
As much as I hate admitting to it at this point...Neil did, at least, have something right. You'll want to be cautious of how you group your tests. If you copy the variable for both tests, you may get: WHERE name LIKE ABC AND listing_type = 1 OR listing_type = 2 But, SQL can only compare 2 tests, separated by AND/OR/etc., at a time. So, it'll assume some grouping because of the order: WHERE (name LIKE ABC AND listing_type = 1) OR listing_type = 2 You'll get a lot more rows than you expect because of listing_type = 2 being on its own. So, you'll want to group the listing_type tests yourself: WHERE name LIKE ABC AND (listing_type = 1 OR listing_type = 2) Also, reversing the order should work (since grouping is left-to-right): WHERE listing_type = 1 OR listing_type = 2 AND name LIKE ABC This'll be read as: WHERE (listing_type = 1 OR listing_type = 2) AND name LIKE ABC Now, if you use IN, the listing_type tests will already be grouped. WHERE name LIKE ABC AND listing_type IN (1, 2) - Jon L. On Mon, Mar 10, 2008 at 4:02 PM, Jon L. [EMAIL PROTECTED] wrote: Now I've spoon fed you Damn, Neil...you're a d-bag. Next time you go to offer help, it may assist you to know that most people don't respond well to being talked down to. (obviously, I'm contradicting my own advice, here; but, I doubt you'll learn from it, anyways) Now, Ron... I'm hoping the following can add a few extra tidbits for you over what Matt already explained. The initial error, I think, was an assumption that natural or spoken language translates directly into SQL. Or, that you'd say: does variable equal 1 or 2. Makes sense to us, right? ;) Now, I could be wrong about that. But, regardless, SQL read your query as: (does variable equal 1) or (is 2 non-zero). And, off course, 2 isn't 0...so that'll always return true; making the 'OR' return true as well. You'll have to associate the variable with each possible match: WHERE (variable = 1) OR (variable = 2) Or simplify it, as Matt suggested, using IN: WHERE variable IN (1, 2) Did that help any? - Jon L. On Mon, Mar 10, 2008 at 3:40 PM, Neil Smith [MVP, Digital media] [EMAIL PROTECTED] wrote: At 19:34 09/03/2008, you wrote: From: Ron Piggott [EMAIL PROTECTED] Date: Sun, 09 Mar 2008 15:34:05 -0400 Message-Id: [EMAIL PROTECTED] What is the correct syntax for where the results may be 1 or 2? What have I done wrong? SELECT * FROM table WHERE name LIKE ABC AND listing_type = 1 or 2 C'mon Ron, this is basic SQL. The query you provided would have given all rows where name was like 'abc', listing type was 1, then returned all rows because `OR 2` results in a value of 'true' which matches everything (you could also have written OR 2 = 2 with the same effect) SELECT * FROM table WHERE name LIKE ABC AND (listing_type = 1 OR listing_type = 2); SELECT * FROM table WHERE name LIKE ABC AND listing_type IN (1, 2); Now I've spoon fed you, please read the manual on Operator Precedence which explains how to combine stuff like this so you don't get the wrong result http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html : To override this order and group terms explicitly, use parentheses (first query above) Cheers - Neil -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Re:Multiple values in SELECT query
[Correction] Change: Also, reversing the order should work (since grouping is left-to-right): To: Also, reversing the order should work (since AND/OR/etc. are read left-to-right): - Jon L. On Mon, Mar 10, 2008 at 4:18 PM, Jon L. [EMAIL PROTECTED] wrote: As much as I hate admitting to it at this point...Neil did, at least, have something right. You'll want to be cautious of how you group your tests. If you copy the variable for both tests, you may get: WHERE name LIKE ABC AND listing_type = 1 OR listing_type = 2 But, SQL can only compare 2 tests, separated by AND/OR/etc., at a time. So, it'll assume some grouping because of the order: WHERE (name LIKE ABC AND listing_type = 1) OR listing_type = 2 You'll get a lot more rows than you expect because of listing_type = 2 being on its own. So, you'll want to group the listing_type tests yourself: WHERE name LIKE ABC AND (listing_type = 1 OR listing_type = 2) Also, reversing the order should work (since grouping is left-to-right): WHERE listing_type = 1 OR listing_type = 2 AND name LIKE ABC This'll be read as: WHERE (listing_type = 1 OR listing_type = 2) AND name LIKE ABC Now, if you use IN, the listing_type tests will already be grouped. WHERE name LIKE ABC AND listing_type IN (1, 2) - Jon L. On Mon, Mar 10, 2008 at 4:02 PM, Jon L. [EMAIL PROTECTED] wrote: Now I've spoon fed you Damn, Neil...you're a d-bag. Next time you go to offer help, it may assist you to know that most people don't respond well to being talked down to. (obviously, I'm contradicting my own advice, here; but, I doubt you'll learn from it, anyways) Now, Ron... I'm hoping the following can add a few extra tidbits for you over what Matt already explained. The initial error, I think, was an assumption that natural or spoken language translates directly into SQL. Or, that you'd say: does variable equal 1 or 2. Makes sense to us, right? ;) Now, I could be wrong about that. But, regardless, SQL read your query as: (does variable equal 1) or (is 2 non-zero). And, off course, 2 isn't 0...so that'll always return true; making the 'OR' return true as well. You'll have to associate the variable with each possible match: WHERE (variable = 1) OR (variable = 2) Or simplify it, as Matt suggested, using IN: WHERE variable IN (1, 2) Did that help any? - Jon L. On Mon, Mar 10, 2008 at 3:40 PM, Neil Smith [MVP, Digital media] [EMAIL PROTECTED] wrote: At 19:34 09/03/2008, you wrote: From: Ron Piggott [EMAIL PROTECTED] Date: Sun, 09 Mar 2008 15:34:05 -0400 Message-Id: [EMAIL PROTECTED] What is the correct syntax for where the results may be 1 or 2? What have I done wrong? SELECT * FROM table WHERE name LIKE ABC AND listing_type = 1 or 2 C'mon Ron, this is basic SQL. The query you provided would have given all rows where name was like 'abc', listing type was 1, then returned all rows because `OR 2` results in a value of 'true' which matches everything (you could also have written OR 2 = 2 with the same effect) SELECT * FROM table WHERE name LIKE ABC AND (listing_type = 1 OR listing_type = 2); SELECT * FROM table WHERE name LIKE ABC AND listing_type IN (1, 2); Now I've spoon fed you, please read the manual on Operator Precedence which explains how to combine stuff like this so you don't get the wrong result http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html : To override this order and group terms explicitly, use parentheses (first query above) Cheers - Neil -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] mysql_connect() Help
Since you seem to want to handle the errors yourself, you may try using the @ operator on the mysql functions. It'll silence any errors or warning that the functions might usually generate; but, of course, you'll be left to test for errors yourself. $link = @mysql_connect($hostname,$mysql_login,$mysql_password); @mysql_select_db($dbname); With that, the following statement should return any errors that MySQL would otherwise generate: if (!$link) { die (mysql_error()); } Now, I don't see any reason for the 2nd echo (i.e., echo After connecting to the database...;) to not be called. Is it in an if statement or other structure/scope in your actual script? However, I do see a reason for another to never be called: if (!$link) { die('Could not connect: ' . mysql_error()); echo Some problem here; } Once you call die, script execution discontinues. The echo after it will never be called, either by !$link returning false to if or by die being called first. On another note... This sounds like you have PHP MySQL installed for private use. But, even if that's the case, I don't recommend connecting to the admin account unless you really need to. You should check out the following page on adding users: http://dev.mysql.com/doc/refman/5.0/en/adding-users.html And read up on the following commands: CREATE USER: http://dev.mysql.com/doc/refman/5.0/en/create-user.html GRANT: http://dev.mysql.com/doc/refman/5.0/en/grant.html - Jon L. On Sat, Mar 8, 2008 at 1:36 PM, Manysh [EMAIL PROTECTED] wrote: Hi, I am trying to make a connection to the MySQL database from PHP but it seems like I am missing a step or something and hence cannot connect to the database. I have tried everything but I can't figure out why. Please suggest/help (I am a beginner in PHP/MySQL/Apache). I am using MySQL 5.0.51a-community-nt on my Windows XP machine. PHP version used is 5.2 and Apache version is 2.2. Code Snippet: $hostname=localhost; $mysql_login=admin; $mysql_password=admin; //$database=sampleapp; echo Print this...; // THIS MESSAGE IS PRINTED $link = mysql_connect($hostname,$mysql_login,$mysql_password); echo After connecting to the database...; // THIS DOES NOT PRINT if (!$link) { die (mysql_error()); // NOR THIS ONE } $dbname = 'sampleapp'; mysql_select_db($dbname); echo $link; if (!$link) { die('Could not connect: ' . mysql_error()); echo Some problem here; } //echo 'Connected successfully'; mysql_close($link); ? Any help is appreciated. Thanks, Manysh -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] mysql_connect() Help
Sorry. I can't really help you with server and PHP installation and configuration issues. I use XAMPP w/ a separate installation of MySQL. http://www.apachefriends.org/en/xampp.html But, find the following in your php.ini: extension_dir=... extension=php_mysql.dll You may already have an ext/ folder, just not where you were thinking it'd be. And, the extension may be commented out (semicolon at the beginning of the line). Theoretically, you should've recieved an undefined function error if it was commented. If the folder that extension_dir points to is empty or undefined, then I'd say go download the Windows ZIP and rebuild the folder. As for the script, do you have an example output? If you're up for it, I wrote test script that outputs a few more details. ?php header('Content-type: text/plain'); $hostname = 'localhost'; $mysql_login = 'admin'; $mysql_password = 'admin'; $dbname = 'sampleapp'; echo PHP: . phpversion() . \n\n; echo Connecting to (MySQL) $hostname (user: $mysql_login)\n; $link = @mysql_connect($hostname, $mysql_login, $mysql_password); echo \n; echo Status: . serialize($link) . ($link)\n; echo Error : ' . mysql_error() . ' ( . mysql_errno() . )\n; echo \n; echo Server: . @mysql_get_server_info() . \n; echo Host : . @mysql_get_host_info() . \n; echo \n; echo Select database `$dbname`\n; $dbselect = @mysql_select_db($dbname); echo \n; echo Status: . serialize($dbselect) . ($dbselect)\n; echo Error : ' . mysql_error() . ' ( . mysql_errno() . )\n; echo \n; echo Closing connection\n; $dbclose = @mysql_close($link); echo \n; echo Status: . serialize($dbclose) . ($dbclose)\n; echo Error : ' . mysql_error() . ' ( . mysql_errno() . )\n; ? The output should be something like this: PHP: 5.2.5 Connecting to (MySQL) localhost (user: admin) Status: i:0; (Resource id #2) Error : '' (0) Server: 5.0.45-community-nt Host : localhost via TCP/IP Select database `sampleapp` Status: b:1; (1) Error : '' (0) Closing connection Status: b:1; (1) Error : '' () - Jon L. On Sat, Mar 8, 2008 at 5:53 PM, Manysh [EMAIL PROTECTED] wrote: Thanks for your input, Jon. Few more observations... Yes, I have PHP+Apache+MySQL installed for private use. I am just trying to connect to the database successfully first and will create other users when I make a successful connection. I made the changes as below and still cannot connect to the database. The echo After connecting to the database... is not in an if statement. I modified the script to make it real simple for me I found this document: http://www.artfulsoftware.com/php_mysql_win.html which outlines installation and configuration steps for MySQL and PHP. Unfortunately, I can't get it to work. One thing I noticed is that my PHP inatallation didn't have any ext directory so I manually created one and copied php_mysqli.dll to this directory and restarted my computer. It didn't work still. Any comments/ideas? ?php /*- DATABASE CONNECTION INFO-*/ $hostname=localhost; $mysql_login=admin; $mysql_password=admin; echo Connected successfully yet? Probably not.; $link = @mysql_connect($hostname,$mysql_login,$mysql_password); if (!$link) { //die('Could not connect: ' . mysql_error()); echo Some problem here; } else echo Connected?; $dbname = 'sampleapp'; $dbselect = @mysql_select_db($dbname); if ($dbselect) echo Connected to the database; else echo Still not connected to the database; mysql_close($link); ? On Sat, Mar 8, 2008 at 3:07 PM, Jon L. [EMAIL PROTECTED] wrote: Since you seem to want to handle the errors yourself, you may try using the @ operator on the mysql functions. It'll silence any errors or warning that the functions might usually generate; but, of course, you'll be left to test for errors yourself. $link = @mysql_connect($hostname,$mysql_login,$mysql_password); @mysql_select_db($dbname); With that, the following statement should return any errors that MySQL would otherwise generate: if (!$link) { die (mysql_error()); } Now, I don't see any reason for the 2nd echo (i.e., echo After connecting to the database...;) to not be called. Is it in an if statement or other structure/scope in your actual script? However, I do see a reason for another to never be called: if (!$link) { die('Could not connect: ' . mysql_error()); echo Some problem here; } Once you call die, script execution discontinues. The echo after it will never be called, either by !$link returning false to if or by die being called first. On another note... This sounds like you have PHP MySQL installed for private use. But, even if that's the case, I don't recommend
Re: [PHP-DB] Help with JOIN query
You can try adding a quick test to the ON statement... SELECT * FROM TableA INNER JOIN TableB ON TableA.record_id = TableB.record_id AND TableB.timestamp = MAX(TableB.timestamp) Now, I haven't tested it. I can only say the theory of it is accurate. - Jon L. On Thu, Mar 6, 2008 at 12:46 PM, Graham Cossey [EMAIL PROTECTED] wrote: I can't see how to accomplish what I need so if anyone has any suggestions they would be gratefully received... I'm using mysql 4.0.20 by the way. I have two tables : TableA record_id product_ref TableB timestamp record_id action I want to create a SELECT that joins these 2 tables where the JOIN to TableB only returns the most recent entry by timestamp. At present (using PHP) I do a SELECT on TableA then for each record returned I perform a 2nd SELECT something like : SELECT timestamp, action FROM TableB WHERE record_id = '$record_id' ORDER BY timestamp DESC LIMIT 1 I now want to do it with one query to enable sorting the results by 'action' from TableB. Any suggestions? Hopefully I've made sense, if not I'll happily try and explain further on request. -- Graham -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php