ChrisMarsdenUK Posted August 9, 2013 Share Posted August 9, 2013 (edited) Hi Guys ( & Girls?) I am trying to figure out why I am getting the following error but cant see the mistake... can anyone help? Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in ****[/size] on line [/size]37 The offending code is apparently here: <?php $host = "localhost"; $user = "xxx"; $pass = "xxxx"; $dbname = "xxxx"; $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>"); mysql_select_db($dbname); $sql = "SELECT user_name FROM permitportal WHERE user_name = '".$_SESSION['user_name']."'"; $query = mysql_query($sql); ** This is line 37 ** while ($row = mysql_fetch_array($query)) { ?> Activate your permit: <a href="edit.php?user_name=<?php echo $row['user_name']; ?>">Edit</a> <?php } ?> Please could anyone point me in the right direction? Edited August 9, 2013 by ignace removed db credentials Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted August 9, 2013 Share Posted August 9, 2013 your query is failing, you need to get some error capture in there to see what the problem is. Quote Link to comment Share on other sites More sharing options...
ChrisMarsdenUK Posted August 9, 2013 Author Share Posted August 9, 2013 im not sure what the above means... im a novice... Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted August 9, 2013 Share Posted August 9, 2013 I know, I was trying to inspire you to go find out try this: $sql = "SELECT user_name FROM permitportal WHERE user_name = '".$_SESSION['user_name']."'"; $query = mysql_query($sql) or die ($sql."<br>Caused an error - the server returned the following:<br>".mysql_error()); Quote Link to comment Share on other sites More sharing options...
ChrisMarsdenUK Posted August 9, 2013 Author Share Posted August 9, 2013 (edited) AHA... I was too close to it... I had named the wrong table. However... I now have another problem if you would be kind enough to help... once i click the edit button i get: edit.php?user_name=789 which is what it should be but then im preparing to edit the file and cant bring up the result... can you spot the error (like wheres wally this) <?php $host = "localhost"; $user = "xxx"; $pass = "xxx"; $dbname = "xxx"; $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>"); mysql_select_db($dbname); $user_name = $_GET['user_name']; $results = mysql_query("select user_name from users where user_name = $user_name"); ** line 17 ** $row = mysql_fetch_assoc($results); ?> <form action="edit_inc.php" method="post"> <input type="hidden" name="id" value="<?php echo $row['user_name']; ?>"> <input type="text" name="PCN" Value="<?php echo $row['user_name']; ?>" readonly> </td> error occurs on line 17 Edited August 9, 2013 by ignace removed db credentials Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted August 9, 2013 Share Posted August 9, 2013 again with the no error capture on the mysql_query()?? My money is on the fact that you didn't wrap the sting that you are comparing user_name to in quotes - ie. the value translated from parsing $user_name - you normally need to have single or double quotes around string literal values. Consider the following example using some of MySql's table aliasing: SELECT user_name AS harry FROM users_table WHERE user_first_name = harry And now: SELECT user_name AS harry FROM users_table WHERE user_first_name = 'harry' Obviously you wouldn't normally aliase a table a name like that, but it should help to clarify the point - string litterals should be wrapped in quotes (either single or double it doesn't matter in SQL) as that's what the parser expects. When you feed things to the parser that it does not expect strange - and normally bad - things will happen. Also, when posting up your code please use the code tags (the <> icon in the toolbar of the editor) - paste your code, highlight all of it and then click on the icon - it will wrap your code in formatting that makes everyones life easier. Quote Link to comment Share on other sites More sharing options...
ignace Posted August 9, 2013 Share Posted August 9, 2013 When posting code, mind that this is a public forum. And any sensitive data is for the world to see. I removed your db credentials, but I advice you to change them. Quote Link to comment Share on other sites More sharing options...
ChrisMarsdenUK Posted August 12, 2013 Author Share Posted August 12, 2013 Ok, so I have looked at an older peice of code written which works fine... <?php $host = "localhost"; $user = "XXX"; $pass = "XXX"; $dbname = "xxx"; $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>"); mysql_select_db($dbname); $PCN = $_GET['PCN']; $results = mysql_query("select REG, MAKE, MODEL, COLOUR, LOCATION, SEENEXPIRED, DATEISSUED, TIMEISSUED, REASON, OFFICERID, PCN, cancelled_by, cancel_reason, active_pcn from selfticket where PCN = $PCN"); $row = mysql_fetch_assoc($results); ?> this works fine, however, all i want to do is ammend it slightly to this: <?php $host = "localhost"; $user = "XXX"; $pass = "XXX"; $dbname = "XXX"; $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>"); mysql_select_db($dbname); $user_name = $_GET['user_name']; $results = mysql_query("select user_name from users where user_name = $user_name"); $row = mysql_fetch_assoc($results); ?> I am missing something as i get the following: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in xxx on line 17 can someone help? Quote Link to comment Share on other sites More sharing options...
tanzeelniazi Posted August 12, 2013 Share Posted August 12, 2013 (edited) I think the rows affected by your are query are zero, therefore, you are getting this error. Edited August 12, 2013 by tanzeelniazi Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 12, 2013 Share Posted August 12, 2013 in your first query, $PCN is apparently a numerical data type (not a string.) in your second query $user_name is obviously a string. string data must be enclosed by single-quotes inside the query statement so that they are treated as a literal string, rather than a mysql identifier or keyword. your query is failing due to an error. whatever is in $user_name is being treated as a column name. if you had some error checking logic for the query, you would getting an error such as "unknown column 'something' in where clause", the something being the actual user_name you submitted. put single-quotes around string data inside the query statement and you always need to test for errors before trying to use the data you expect from a statement to prevent follow-on errors like this one. @tanzeelniazi a query that matches or affects zero rows does not produce php errors. Quote Link to comment Share on other sites More sharing options...
tanzeelniazi Posted August 12, 2013 Share Posted August 12, 2013 @tanzeelniazi a query that matches or affects zero rows does not produce php errors. Thanks mac_gyver, I will keep a note of it. Quote Link to comment 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.