Maclovin Posted August 14, 2013 Share Posted August 14, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/ Share on other sites More sharing options...
Maclovin Posted August 14, 2013 Author Share Posted August 14, 2013 Can anyone help me with this problem? i would be grateful Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445068 Share on other sites More sharing options...
Ballam Posted August 14, 2013 Share Posted August 14, 2013 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] . "'"); Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445070 Share on other sites More sharing options...
Ballam Posted August 14, 2013 Share Posted August 14, 2013 Also, if the above post is correct, you will need to do it for all the other MYSQL statements too. Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445072 Share on other sites More sharing options...
Ballam Posted August 14, 2013 Share Posted August 14, 2013 (edited) 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 August 14, 2013 by Ballam Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445075 Share on other sites More sharing options...
Psycho Posted August 14, 2013 Share Posted August 14, 2013 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); } Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445076 Share on other sites More sharing options...
Yohanne Posted August 15, 2013 Share Posted August 15, 2013 could you post your dbconnection.php code. Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445095 Share on other sites More sharing options...
debascoguy Posted August 15, 2013 Share Posted August 15, 2013 (edited) 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 August 15, 2013 by debascoguy Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445192 Share on other sites More sharing options...
Psycho Posted August 15, 2013 Share Posted August 15, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445193 Share on other sites More sharing options...
debascoguy Posted August 15, 2013 Share Posted August 15, 2013 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). Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445206 Share on other sites More sharing options...
Psycho Posted August 15, 2013 Share Posted August 15, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445212 Share on other sites More sharing options...
Maclovin Posted August 17, 2013 Author Share Posted August 17, 2013 @jayson_ph this is my dbconnection <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("vworkshop"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445540 Share on other sites More sharing options...
Maclovin Posted August 17, 2013 Author Share Posted August 17, 2013 @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 Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445541 Share on other sites More sharing options...
Maclovin Posted August 17, 2013 Author Share Posted August 17, 2013 @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 Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445542 Share on other sites More sharing options...
Maclovin Posted August 17, 2013 Author Share Posted August 17, 2013 im guessing it has something to do with my database? i do have a service table where "serviecid" is a row in there. Quote Link to comment https://forums.phpfreaks.com/topic/281178-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1445544 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.