cilian29 Posted June 6, 2012 Share Posted June 6, 2012 hi all I'm trying to make this work, can you help on my script i'm doing now I'm trying to call my php link within functions. pls bear with me bec. I'm lost my my self. im trying to call a list of items from my database when I clicked the employee nos.by the way I'm using posrgesql and apache server and its running good. I'm getting this error Warning: pg_query() [function.pg-query]: Query failed: ERROR: syntax error at or near ")" at character 44 in C:\Program Files\sample.php Warning: pg_fetch_array() expects parameter 1 to be resource, string given in C:\Program Files\sample.php I have a query function called function callme(){ $sql2 =" SELECT * FROM employee WHERE empid = ($myid)"; $result=pg_query($sql2); //or die(pg_last_error()); while($row=pg_fetch_array($sql2)){ echo $row[empid]; } Then I create a swich case to call my function switch($_REQUEST["num"]) { case "1": callme(); break; case "2": echo 'no record!!'; break; default: break; } } this is my php file: $mod = $_GET[mod]; $myid =$_GET[id]; $sq1=pg_query("SELECT DISTINCT empid FROM employee INNER JOIN client c ON(merchantable.ccindex = c.clientindex) ORDER BY merindex ASC"); :shy:this code is running its showing me what distinct items i need. while ($row = pg_fetch_array($sq1)){ echo '<table>'; echo '<tr>'; echo "<td><a href='?mod=$mod&num=1&id=$myid& $row[refid]>".$row["refid"]."</a></td>"; echo "</tr>"; echo '</table>'; } then my url :http://localhost/preprod/overwriteA.php?mod=&num=1&id=&%201004SA%3E1004SA%3C/a%3E%3C/td%3E%3Ctable%3E%3Ctr%3E%3Ctd%3E%3Ca%20href= Why am I not getting anywhere PLEase HELp Quote Link to comment https://forums.phpfreaks.com/topic/263747-a-href-and-php-combination/ Share on other sites More sharing options...
btherl Posted June 6, 2012 Share Posted June 6, 2012 Inside your function callme(), $myid is not defined. You can pass it as an argument like this: callme($myid); function callme($myid) { ... } A good debugging technique for this situation is to print out the query which fails. Then you would see that $myid is missing. Quote Link to comment https://forums.phpfreaks.com/topic/263747-a-href-and-php-combination/#findComment-1351583 Share on other sites More sharing options...
cilian29 Posted June 6, 2012 Author Share Posted June 6, 2012 Thank you for your quick reply and I hope you help me further when you mention pass the callme($myid) as an argument heres what I did. I just added the call me above my original function. I created anew php file to test on it sample.php $callme($myid); function callme($myid){ $sql2 =" SELECT * FROM employee WHERE empid = ($myid)"; $result=pg_query($sql2); //or die(pg_last_error()); while($row=pg_fetch_array($sql2)){ echo $row[empid]; } } my question is, do I remove my switch case when I call the function? when I run the my code above this time I dont see any errors but there is no display on my browser and my url: http://localhost/preprod/sample.php?mod=&nir=1&id=&%201004SA>1004SA</a></td><table><tr><td><a%20href=http://localhost/preprod/sample.php?mod=&num=1&id=&%201004SA>1004SA</a></td><table><tr><td><a%20href= Quote Link to comment https://forums.phpfreaks.com/topic/263747-a-href-and-php-combination/#findComment-1351592 Share on other sites More sharing options...
Jessica Posted June 6, 2012 Share Posted June 6, 2012 Please use code tags. When you call it in the switch statement, you'll need to pass the argument. Quote Link to comment https://forums.phpfreaks.com/topic/263747-a-href-and-php-combination/#findComment-1351675 Share on other sites More sharing options...
btherl Posted June 7, 2012 Share Posted June 7, 2012 If that is your whole sample.php, a few things are missing. Try this: $myid =$_GET[id]; /* Here you need the call to pg_connect(), so you can call pg_query() on that connection */ callme($myid); /* Don't put "$" in front of callme */ function callme($myid){ $sql2 =" SELECT * FROM employee WHERE empid = ($myid)"; $result=pg_query($sql2); or die(pg_last_error()); /* Don't comment out the "or die" - you need it to find the bug */ while($row=pg_fetch_array($result)){ /* Use pg_fetch_array() on $result, not on $sql2 */ echo $row[empid]; } } Quote Link to comment https://forums.phpfreaks.com/topic/263747-a-href-and-php-combination/#findComment-1351776 Share on other sites More sharing options...
cilian29 Posted June 7, 2012 Author Share Posted June 7, 2012 good day thank you for all helping me and for the replies on seeing the code you suggested I immediately tried adding the changes but unfortunately I came upon one problem to the next so here it goes and I made some difference on my previous script above. Parse error: parse error, unexpected T_LOGICAL_OR in C:\Program Files\xampp\htdocs\preprod\overwriteB.php on line 40 im getting this error when the code is inserted so i comment it out for a while. or die(pg_last_error()); /* Don't comment out the "or die" - you need it to find the bug */ Warning: pg_query() [function.pg-query]: Query failed: ERROR: syntax error at or near ")" at character 35 in C:\Program Files\xampp\htdocs\preprod\sample.php on line 39 Warning: pg_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files\xampp\htdocs\preprod\sample.php on line 41 1 <?php 2 include('server/configA.php'); 3 4 $mod = $_GET[mod]; 5 $myid =$_GET[id]; 6 7 $link=pg_connect("host= " .DB_HOST. " port= " .DB_PORT. " dbname= " .DB_DATABASE. " user= " .DB_USER. " password= " .DB_PSWD); 8 if(!$link) 9 { 10 die('Failed to connect to server: ' .pg_result_error_field($link)); 11 } 12 callme($myid); /* Don't put "$" in front of callme */ 13 if($_REQUEST[nir]==1){callme();} 14 15 16 17 18 $sq1 ="SELECT * FROM merchantable"; 19 $result =pg_query($sq1); 20 21 //for refid display below 22 while ($row = pg_fetch_array($result)){ 23 echo '<table>'; 24 echo '<tr>'; 25 26 27 echo "<td><a href='?mod=$mod&nir=1&id=$myid& $row[empid]'>".$row["empid"]."</a></td>"; 28 29 echo '</tr>'; 30 echo '</table>'; 31 } 32 33 34 35 function callme($myid){ 36 37 $q1 =" SELECT * FROM nir WHERE empid = ($myid)"; 38 //$q1 ="SELECT * FROM nir"; 39 $result =pg_query($q1); 40 //or die(pg_last_error()); /* Don't comment out the "or die" - you need it to find the bug */ 41 42 43 while($rows=pg_fetch_array($result)){ /* Use pg_fetch_array() on $result, not on $q1 */ 44 45 echo $rows[empid]; 46 } 47 } 48 ?> thank you again hope you van help me... pls! Quote Link to comment https://forums.phpfreaks.com/topic/263747-a-href-and-php-combination/#findComment-1351810 Share on other sites More sharing options...
btherl Posted June 10, 2012 Share Posted June 10, 2012 Change this: $result =pg_query($q1); //or die(pg_last_error()); /* Don't comment out the "or die" - you need it to find the bug */ to this: $result =pg_query($q1) /* Remove ";" here when uncommenting the next line */ or die(pg_last_error()); /* Don't comment out the "or die" - you need it to find the bug */ Then the next thing to do is to print out the query which has the error. Add this line: echo "<br>Query is $q1<br>"; Add that after $q1 has been set, but BEFORE it use used in pg_query(). Then post what it prints here. Quote Link to comment https://forums.phpfreaks.com/topic/263747-a-href-and-php-combination/#findComment-1352582 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.