Jump to content

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in


Maclovin

Recommended Posts

I keep getting the following error Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in.... on line 7 in the code below:

<?phpsession_start();include("header.php");include("sidebar.php");include("dbconnection.php");$result= mysql_query("select * from service where serviecid='$_POST[serviceid]'");$row1 = mysql_fetch_array($result);$result1= mysql_query("select * from customer where custid='$_POST[customerid]'");$arrrec2= mysql_fetch_array($result1);$dt = date("Y-m-d");if($_POST["vehstatus"] == "Completed"){	$sql ="insert into billing(serviceid,particulars,scost,date,paidstatus) values('$_POST[serviceid]','$_POST[particulars]','$_POST[vehcost]','$dt','$_POST[vehstatus2]')";	if (!mysql_query($sql,$con))	  {	  die('Error: ' . mysql_error());	  }	 $insid =  mysql_insert_id();	 	 mysql_query("UPDATE service SET status='$_POST[vehstatus]' where serviecid='$_POST[serviceid]'");}if($_POST["vehstatus"] == "Cancelled"){	$sql ="insert into billing(serviceid,particulars,scost,date,paidstatus) values('$_POST[serviceid]','Calncelled','0.00','$dt','Cancelled')";	if (!mysql_query($sql,$con))	  {	  die('Error: ' . mysql_error());	  }	 $insid =  mysql_insert_id();	 	 mysql_query("UPDATE service SET status='$_POST[vehstatus]' where serviecid='$_POST[serviceid]'");}?>											<div id="main">										<a name="TemplateInfo"></a>			            <?phpif($ctins == 1){	echo "<center><b>Employees account created successfully...</b></center><br>";	echo "<center><b><a href='emplogin.php'>Click here to Login.</a></b></center>";}else{	?>		<form id="form1" name="form1" method="post" action="">		  <table width="494" border="1">		    <tr>		      <th colspan="2" scope="col"><h1>E-Workshop</h1></th>	        </tr>		    <tr>		      <th height="40" scope="col">Bill No. : <?php echo $insid; ?></th>		      <th scope="col">Date: <?php echo $dt;  ?></th>	        </tr>		    <tr>		      <th width="237" height="45" scope="col">Sevice ID: </th>		      <th width="241" scope="col">  <?php echo $_POST[serviceid]; ?></th>	        </tr>            <?p						...			...			... to be continued.

viewservicebillingreport.php

 

 

 

Link to comment
Share on other sites

Sorry, I was completely wrong before.  I've just noticed what is actually wrong with the code....  you appear to have a slight typo on your column name, you have wrote serviecid, rather than serviceid  :-*

Edited by Ballam
Link to comment
Share on other sites

 

I'm not sure if it is just the way I do it, but I thought the PHP variable/array should be outside of the quotes like below:

$result= mysql_query("select * from service where serviecid='" . $_POST[serviceid] . "'");

Nope. It is perfectly valid to put variables inside strings that are defined with double quotes. However, you have to be careful. Based upon whether the variable is next to other text or not it can be mis-parsed. Plus, arrays cannot use quotes around the key when used in double quoted strings without specific delimiters (more below). but, your example above is actually incorrect. because the array variable is defined outside a quoted string, you should have quotes around the array key. That will work, but it is the wrong way to do it because the PHP parser will first try to find a constant with the name serviceid. If not found it will then use that value as the key name. I typically define my variables inside double quoted strings but I always enclose them in curly braces, {}, to ensure they are parsed correctly. You should read this page which has all the information you need to understand how to define strings and how they are interpreted: http://php.net/manual/en/language.types.string.php

 

@Maclovin: First off, use [ code ] tags when posing code. Your problem is that your query is failing so the return value of $result is the Boolean false. You shoudl first test your queries in PHPMyAdmin or whatever database management tool you use. Then, you should also add error handling to your code. The way you have it provides no information if there is an error or not or how to fix it. plus, the code you are using has a lot of flaws:

1) The mysql_ functions are deprecated

2) You are using input from the user directly in the query without escaping it - opening yourself up to SQL Injection

3) The two select queries at the top have no purpose - the results aren't used anywhere

 

Anyway, you can change those queries to show whatever error is displayed. This is just a quick example script to point you in the right direction. I would have more code for a real application.

 

//Escape the user submitted value
$serviceid = mysql_real_escape_string(trim($_POST['serviceid']));
//Create the query as a string
$query = "select * from service where serviceid='{$serviceid}'";
//Execute the query
$result= mysql_query($query);
//Check if query failed or had no results
if(!$result)
{
    echo "Query Failed!<br>Query: {}<br>Error: " . mysql_error();
}
elseif(!mysql_num_rows($result))
{
    echo "The query returned 0 results.<br>{$query}";
}
else
{
    $row1 = mysql_fetch_array($result);
}
Link to comment
Share on other sites

You code is ok. The problem is a syntax error, Please check the bold part of your code below:

$result= mysql_query("select * from service where serviecid='$_POST[serviceid]'");$row1 = mysql_fetch_array($result);

Due to syntax error in: $result= mysql_query("select * from service where serviecid='$_POST[serviceid]'");

 

Then, the $row1 = mysql_fetch_array($result);  Could not fetch anything from the table service.
 

Edited by debascoguy
Link to comment
Share on other sites

Due to syntax error in: $result= mysql_query("select * from service where serviecid='$_POST[serviceid]'");

 

There is no syntax error there - it is perfectly valid code. Now, there is a good chance the field name is not spelled correctly (which Ballam already pointed out on reply #5). But, that is not a syntax error. A syntax error is something that prevents the PHP parser from parsing the script.

Link to comment
Share on other sites

There is no syntax error there - it is perfectly valid code. Now, there is a good chance the field name is not spelled correctly (which Ballam already pointed out on reply #5). But, that is not a syntax error. A syntax error is something that prevents the PHP parser from parsing the script.

Check the spelling in bold...The code is valid but the spelling of variable looks wrong...Check carefully the spelling (serviecid).

Link to comment
Share on other sites

Check the spelling in bold...The code is valid but the spelling of variable looks wrong...Check carefully the spelling (serviecid).

 

Um, yeah, I already stated that. Did you not read what I posted? Let me break it down for you.

 

1) What the OP posted is NOT a syntax error. A syntax error is something that prevents the PHP engine from parsing the code. A typo in a field name for a query does not do that and, thus, is not a syntax error.

 

2) As I JUST stated, Ballam pointed out the apparent typo of that field name back on reply #5. So, I'm trying to understand, what your response added to this conversation since it provided no new information and was factually incorrect.

Link to comment
Share on other sites

@Guru "Anyway, you can change those queries to show whatever error is displayed.This is just a quick example script to point you in the right direction. I would have more code for a real application."

 

I tried your suggestion using the code you provided as a start and got the following error

 

Query Failed!
Query: {}
Error: Unknown column 'serviceid' in 'where clause'Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'serviecid' in C:\xampp\htdocs\Vehicleworkshop\Vehicleworkshop\viewservicebill' at line 1

Link to comment
Share on other sites

@Guru. i really appreciate your comments and will take everything into consideration on my continued work on this. i am not an advanced php coder so i knew i made a lot of mistakes. the whole aim is to produce a service bill and post the results to the relevant field names in that bill

Link to comment
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.