DuaneCawaling Posted March 1, 2013 Share Posted March 1, 2013 i'm a newbie at PHP prodramming and i want to add data to an existing data base, but unfortunately, the " Warning: mysql_query() expects parameter 2 to be resource, "always pops up @@ here are my codes <?php$con=mysql_connect("localhost","root","duane");// Check connectionif(!$con){echo ("Could not connect." .mysql_error());}echo ("Connected successfully");$selected = mysql_select_db("dummy") or die ("No existing database".mysql_error());mysql_query($selected,"INSERT INTO prepurchase (ID, Sum, Reply, items, time) VALUES ('080392',15,1,'2m2','5:46'')") or die(mysql_error());mysql_query($selected,"INSERT INTO prepurchase (ID, Sum, Reply, items, time) VALUES ('080390',20,1,'2m3','9:46'')") or die(mysql_error());$data = mysql_query($selected, "SELECT * from prepurchase") or die (mysql_error());while ($row = mysql_fetch_array($data)) {echo "FN: ".$row{'Firstname'}." LN: ".$row{'Lastname'}." Age: ".$row{'Age'}."<br>";//display the results}mysql_close($con);?> the italic and bold codes are the error..please enlighten me with your knowledge. attached is the database i'm planning to modify using ph Quote Link to comment https://forums.phpfreaks.com/topic/275099-warning-mysql_query-expects-parameter-2-to-be-resource/ Share on other sites More sharing options...
requinix Posted March 1, 2013 Share Posted March 1, 2013 (edited) In a form that's actually readable, <?php $con=mysql_connect("localhost","root","duane"); // Check connection if(!$con){ echo ("Could not connect." .mysql_error()); } echo ("Connected successfully"); $selected = mysql_select_db("dummy") or die ("No existing database".mysql_error()); mysql_query($selected,"INSERT INTO prepurchase (ID, Sum, Reply, items, time) VALUES ('080392',15,1,'2m2','5:46'')") or die(mysql_error()); mysql_query($selected,"INSERT INTO prepurchase (ID, Sum, Reply, items, time) VALUES ('080390',20,1,'2m3','9:46'')") or die(mysql_error()); $data = mysql_query($selected, "SELECT * from prepurchase") or die (mysql_error()); while ($row = mysql_fetch_array($data)) { echo "FN: ".$row{'Firstname'}." LN: ".$row{'Lastname'}." Age: ".$row{'Age'}."<br>";//display the results } mysql_close($con); ?> Read the manual page for mysql_query() to see how the function should be used. Spoiler: you're close. Also, echo "FN: ".$row{'Firstname'}." LN: ".$row{'Lastname'}." Age: ".$row{'Age'}."<br>";//display the results {} array syntax is deprecated. Use []s instead. Edited March 1, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/275099-warning-mysql_query-expects-parameter-2-to-be-resource/#findComment-1415906 Share on other sites More sharing options...
DuaneCawaling Posted March 2, 2013 Author Share Posted March 2, 2013 (edited) $con=mysql_connect("localhost","root","duane"); // Check connection if(!$con){ echo ("Could not connect." .mysql_error()); } echo ("Connected successfully"); $selected = mysql_select_db("dummy") or die ("No existing database".mysql_error()); $result = mysql_query("INSERT INTO prepurchase(ID, Sum, Reply, items, time) VALUES('080390',20,1,'2m3','9:46')") or die(mysql_error()); $data = mysql_query("SELECT * from prepurchase") or die (mysql_error()); while ($row = mysql_fetch_array($data)) { echo "ID: ".$row['ID']." Sum: ".$row['Sum']." Items: ".$row['items']." ";//display the results } mysql_close($con); i'm sorry about the way i posted the codes, i was in a rush it works!changed the mysql_query parameters.. THANK YOU for the kind reply hoping to hear from you more coz i know his will not be the first time i'll ask questions about php Edited March 2, 2013 by DuaneCawaling Quote Link to comment https://forums.phpfreaks.com/topic/275099-warning-mysql_query-expects-parameter-2-to-be-resource/#findComment-1416001 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.