visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: bracketing??


By: roe1and Great Britain  Date: 10/11/2007 14:24:16  English  Points: 20 Status: Answered
Quality : Excellent
at least i think that's the problem.
i'm trying to get information from a table in a mysql db using information from another table
$prac_name is Bellyeoman Surgery and $add_l1 is Bellyeoman Road
scenario 1:
l_57: $idsql = "SELECT id FROM marc_db1.tbl_nid WHERE name = '$prac_name' AND add_l1 = '$alll'";
l_58: $res_nid = mysql_query($idsql);
=
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 5 in /usr/www/users/resurrec/anke/index1.php on line 57

scenario 2:
$idsql = "SELECT id FROM marc_db1.tbl_nid WHERE name = '{$prac_name}' AND add_l1 = '{$alll}'";
$res_nid = mysql_query($idsql);
=
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 5 in /usr/www/users/resurrec/anke/index1.php on line 57

scenario 3:
$idsql = 'SELECT id FROM marc_db1.tbl_nid WHERE name = "$prac_name" AND add_l1 = "$alll"';
$res_nid = mysql_query($idsql);
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 5 in /usr/www/users/resurrec/anke/index1.php on line 57

scenario 4:
$idsql = "SELECT id FROM marc_db1.tbl_nid WHERE name = "$prac_name" AND add_l1 = "$alll"";
$res_nid = mysql_query($idsql);
Parse error: syntax error, unexpected T_VARIABLE in /usr/www/users/resurrec/anke/index1.php on line 53
- this makes sense because all the """" confuses everything. but see below

my fix:
$bob = '"';
$prac_name = "$bob$prac_nam$bob";
$alll = "$bob$al1$bob";

$idsql = "SELECT id FROM marc_db1.tbl_nid WHERE name = $prac_name AND add_l1 = $alll";
$res_nid = mysql_query($idsql);

basically same as s4, except it works. it is a bit tedious though.
By: VGR Date: 10/11/2007 15:37:43 English  Type : Comment
no, it hasn't to do with bracketing or quoting, but with your way iof handling SQL queries. If you look into mysql online documentation samples, AND in PHP's online manuel for mysql_* functions examples ( http://www.php.net/manual/en is an URI you'd better learn to type fast & right ;-) you'll see it's done more cautiously than in your code.

In short, the cause to your problem is that the query returned no results set (either it was in error or it really worked but failed to return any row) and you get subsequently the error you get on mysql_result() or mysql_fetch_array() BECAUSE YOU DON'T TEST FOR SUCCESS OF THE MYSQL_QUERY() CALL.

I let you rewrite your code based on this code skeleton :

// // if you agree on dying() whenever a failure occurs // $query="select * from ... WHERE ... ;"; // note the trailing semicolon $result=mysql_query($query,$linkID) or die ("bad query '$query' : ".mysql_error()); if (mysql_num_rows($result)>0) { while ($res=mysql_fetch_array($result)) { // handle results in a loop // if you want the first one only, don't do the while loop } } else { // no results // if you don't care for results, skip all of this altogether } // if results



or, if you don't want to die() in case of failure :

// // if you do not agree on dying() whenever a failure occurs // $query="select * from ... WHERE ... ;"; // note the trailing semicolon $result=@mysql_query($query,$linkID); if ($result===FALSE) { // a failure occured // handle error // the error is given by mysql_error() } else { // no error if (mysql_num_rows($result)>0) { while ($res=mysql_fetch_array($result)) { // handle results in a loop // if you want the first one only, don't do the while loop } } else { // no results // if you don't care for results, skip all of this altogether } // if results } // if failure or not




BTW, the most correct way of writing your queries is your "scenario 2" 's, if you add the trailing mandatory semicolon terminating an SQL sentence ;-)))
By: roe1and Date: 14/11/2007 16:17:39 English  Type : Comment
why is it ===FALSE ?
By: VGR Date: 14/11/2007 21:01:21 English  Type : Answer
because == means only that both sides of the operator evaluate to the same. In that sense, PHP being weakly typed (even worse : polymorphic is the rule, ie $a=0.1; followed by $a='toto'; is valid ) , FALSE evaluates the same as 0

Thus the === operator was invented to add the notion of type : are both operands of the same type ? And do they evaluate to the same ?

for instance, not using === is a very bad idea when it comes to standard PHP functions returned values, because their conceptors have used the VERY bad habit of returning either a typed value of some kind OR FALSE if the call fails. This the function returns in fact either a Boolean or a value of an other type. Hence the use of === to discriminate between "the function returned 0" and "the function retirned failure", as when you use strpos() for instance. PHP numeric indexed arrays being 0-based, a character can be found using strpos() at the position 0. Halas, 0==FALSE so you can't tell from strpos() returning negatively (not found). Hence the use of ===

A very good habit IMHO.

Just TOO bad PHP hasn't a directive to turn on strong typing, it would be nice to have warnings sometimes. the equivalent of IMPLICIT NONE of FORTRAN would also be nice (would prevent typos in variables names)

this is explained in http://fr3.php.net/manual/de/types.comparisons.php

Do register to be able to answer

EContact
browser fav
page generated in 93.122960 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page