Jump to content

Recommended Posts

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 by ignace
removed db credentials
Link to comment
https://forums.phpfreaks.com/topic/281001-can-someone-help-me/
Share on other sites

I know, I was trying to inspire you to go find out :tease-03:

 

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());
Link to comment
https://forums.phpfreaks.com/topic/281001-can-someone-help-me/#findComment-1444193
Share on other sites

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 by ignace
removed db credentials
Link to comment
https://forums.phpfreaks.com/topic/281001-can-someone-help-me/#findComment-1444199
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/281001-can-someone-help-me/#findComment-1444222
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/281001-can-someone-help-me/#findComment-1444488
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/281001-can-someone-help-me/#findComment-1444504
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.