[SOLVED] PHP & MYsql Newbie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
StephenL
Forum Newbie
Posts: 3
Joined: Sun Jun 27, 2004 12:05 pm

PHP & MYsql Newbie

Post by StephenL »

For some reason this insert code doesn't work

Code: Select all

<?php
$linkID = @mysql_connect("****", "****", "******");
mysql_select_db("*****", $linkID);
$result = mysql_query("INSERT INTO customers (firstname, lastname) VALUES ('bob', 'bob1')", $linkID);
if ($result != false)
&#123;
	echo 'Success!';
&#125;
else
&#123;
	echo 'failure';
&#125;
mysql_close($linkID);
?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Try:

Code: Select all

<?php
$linkID = @mysql_connect("****", "****", "******");
mysql_select_db("*****");
$result = mysql_query("INSERT INTO customers (firstname, lastname) VALUES ('bob', 'bob1')");

if (mysql_num_rows($result))
{
   echo 'Success!';
}
else
{
   echo 'failure';
}
mysql_close($linkID);
?>
StephenL
Forum Newbie
Posts: 3
Joined: Sun Jun 27, 2004 12:05 pm

Post by StephenL »

I get an error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in

Code: Select all

<?php
$linkID = @mysql_connect("*", "*", "*");
mysql_select_db("*");
$result = mysql_query("INSERT INTO customers (firstname, lastname) VALUES ('bob', 'bob1')");

if (mysql_num_rows($result)) \\\\\\\\This line
&#123;
   echo 'Success!';
&#125;
else
&#123;
   echo 'failure';
&#125;
mysql_close($linkID);
?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Use mysql_error to help debugging.

Code: Select all

$linkID = @mysql_connect("*", "*", "*") or die(mysql_error());
mysql_select_db("*") or die(mysql_error());
$sql = "INSERT INTO customers (firstname, lastname) VALUES ('bob', 'bob1')";
$result = mysql_query($sql) or die(mysql_error());
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Then your mysql is causing the error. Is there a table called 'customers' in your database and does it have two fields called 'firstname' and 'lastname'? Can both of these fields accept strings?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Im not sure if this will make a difference but try:

Code: Select all

<?php
$linkID = @mysql_connect("****", "****", "******");
mysql_select_db("*****") or die(mysql_error());
$query = "INSERT INTO customers (firstname, lastname) VALUES ('bob', 'bob1')";
$result = mysql_query($query) or die(mysql_error());

if (mysql_num_rows($result))
{
  echo 'Success!';
}
if (!mysql_num_rows($result))
{
  echo 'failure';
}
mysql_close($linkID);
?>
Also have a read at what kettle_drum suggested, that could be your problem!
StephenL
Forum Newbie
Posts: 3
Joined: Sun Jun 27, 2004 12:05 pm

thanks

Post by StephenL »

Got it Working :D
Post Reply