Jump to content

Recommended Posts

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 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);
?>

 

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 

post-141682-0-50593300-1362175912_thumb.gif

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 by requinix
$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 by DuaneCawaling
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.