Web Hosting Talk







View Full Version : mysql connet error


brcolow
09-24-2002, 09:27 PM
in this script i get this error...



<?
if (!$title || !$about || !$screenshots || !$source)
{
echo "You have not completed all of the required feilds in your article report.<br>"
."please go back and try again.";
exit;
}

$title = addslashes($title);
$about = addslashes($about);
$screenshots = addslashes($screenshots);
$source = addslashes($source);

@ $db = mysql_pconnect("localhost", "mike", "notshown");
if (!$db)
}
echo "<b>Could not connect to database, try again later.</b>";
exit;
}
mysql_select_db("news");

$query = "insert into articles (id) values
("null", '".$title"', '".$about."', '".$screenshots"', '".$source."')";

$result = mysql_query($query);
if ($result)
echo mysql_affected_rows()." article insterted into database. Thank you for posting your article!";
?>


Parse error: parse error, unexpected '}' in D:\www\www.flashstand.com\articles\article.php on line 18

michaeln
09-24-2002, 09:35 PM
if (!$db)

}

echo "<b>Could not connect to database, try again later.</b>";

exit;

}


Should be


if (!$db)

{

echo "<b>Could not connect to database, try again later.</b>";

exit;

}


You have that first curly brace going the wrong way...

brcolow
09-24-2002, 10:19 PM
thanks :D

edit: ok i added that and now it gives me this error...

Parse error: parse error, unexpected T_STRING in D:\www\www.flashstand.com\articles\article.php on line 30

Thanks for the help!

CritticAge
09-24-2002, 10:42 PM
if ($result)
echo mysql_affected_rows()." article insterted into database. Thank you for posting your article!";



Would that be...



if ($result)

{

echo mysql_affected_rows()." article insterted into database.

}

Thank you for posting your article!";



I know Perl, not PHP...but that may be it?

brcolow
09-25-2002, 12:54 AM
nope :(, that didnt work does anyone else know?

Barak
09-25-2002, 09:02 AM
Try separating the last echo statement out to see which bit's going wrong, eg.
if ($result) {
echo mysql_affected_rows();
echo "article inserted into database. Thank you for posting your article!";
}

Studio64
09-25-2002, 10:45 AM
Originally posted by brcolow
thanks

edit: ok i added that and now it gives me this error...

Parse error: parse error, unexpected T_STRING in D:\www\www.flashstand.com\articles\article.php on line 30

Thanks for the help!

Originally posted by brcolow
in this script i get this error...
$query = "insert into articles (id) values
("null", '".$title"', '".$about."', '".$screenshots"', '".$source."')";


OK... The MySQL statement... I'll take it part by part...

insert into articles (id) values
--- It's general syntax just to CAPS your reserved words for ease of reading.... Also the table usually set off by ` (backwards apostrophe [ top left key on keyboard ]). Also the (id) statement. Your not just inserting the ID of the table your putting in a whole new record. You can just leave out that part.

Corrected:
INSERT INTO `articles` VALUES

Next part....
("null", '".$title"', '".$about."', '".$screenshots"', '".$source."')";

This is a bit of a mess here.... PHP has a wonderful feature called magic quotes that can do some wonders in text parsing but, it can't handle this. You can't open and close " like that in PHP. It appears like you have a single quote then a double quote holding all of the $var's inside the query string. There are three apporiate ways to do this. My prefered method.

('', '$title', '$about', '$screenshots', '$source')

Those are all single quotes around each $var not doubles. If you want to use a double quote in a string you must set it off with a \ charecter.

echo "Hello \"Joe\" welcome";

Would output
Hello "Joe" welcome

If you want to do it that way the query would be like this

" (\"\", \"$title\", \"$about\", \"$screenshots\", \"$source\") "

Both would work although the 1st is much easier to read.

Also... You don't need to include 'null' for the ID column. By simple using '' (single quotes :D) MySQL will auto_increment the table anyhow (That is of course if the field is an auto_increment field).

You could also write the query using the string concatenation function. The .(period) [Combining strings]
$v = "hello";
$b = "bash";
echo $v." -- ".$b;

That would output
hello -- bash

So your query there would look like
" ('', '".$title."', '".$about."', '".$screenshots."', '".$source."')"

For example the '".$title."' would translate to english (b/c VBul uses a weird font) to Apostrophe Quote Period $title Period Quote Apostrophe....

So..........

Your final query would look like this

$sql = "INSERT INTO `articles` ('', '$title', '$about', '$screenshots', '$source')";


That should solve you problem and teach you a little more PHP at the same time....



BTW... Don't worry about it. Everyone's gotta learn sometime. It's best to ask questions then to bang you head on the table not understanding it :D....
So don't worry about the fact that I'm 2 years younger than you :D

brcolow
09-25-2002, 11:05 AM
well i did all of that but no for some very weird reason its giving me...

Notice: Undefined variable: title in D:\www\www.flashstand.com\articles\article.php on line 4

and all line four is is this, if (!$title || !$about || !$screenshots || !$source)



Thanks alot for the help already

Studio64
09-25-2002, 12:00 PM
Originally posted by brcolow
well i did all of that but no for some very weird reason its giving me...

Notice: Undefined variable: title in D:\www\www.flashstand.com\articles\article.php on line 4


If the information is being passed to this page by a form you'd have to reference the vars like this

If the form method was "post" i.e. <form action="some.php" method="post">

$title = $_POST['title'];

If it was a get then

$title = $_GET['title'];

If it was passed by parameter....

well that should work.... If it doesn't, you could either try

global $title;

or $title = $GLOBAL['title'];

Barak
09-25-2002, 04:21 PM
Heh, completely missed the dodgy SQL query. If you're getting an 'undefined variable' error message then you've got error checking on too high a level in php.ini

Basically, on PHP's high error reporting level, it'll report stuff that doesn't actually stop the script working such as variables not existing, so you can't do a if (!$var) check, because if that's true (there is no $var variable) then the variable hasn't been defined yet.

Have a look in your php.ini for error reporting - it should give you an example of a better setting to use (I can't remember it off the top of my head, sorry).

brcolow
09-25-2002, 07:43 PM
well studio, i did what u said and oddly enough no errors were returned but it didnt say 1 article instered etc, anyway on how to fix this?

brcolow
09-25-2002, 10:42 PM
bump

Studio64
09-25-2002, 11:01 PM
Originally posted by brcolow
well studio, i did what u said and oddly enough no errors were returned but it didnt say 1 article instered etc, anyway on how to fix this?

Try replacing
$result = mysql_query($query);
if ($result)
echo mysql_affected_rows()." article insterted into database. Thank you for posting your article!";


With this

$result = mysql_query($query);
if ($result)
echo mysql_affected_rows()." article insterted into database. Thank you for posting your article!";
else
echo "Error adding article to database<hr>".mysql_error()."<hr>";


What is the resulting output from that statment.

brcolow
09-25-2002, 11:05 PM
i got this...


Parse error: parse error, unexpected T_STRING in D:\www\www.flashstand.com\articles\article.php on line 40

Studio64
09-26-2002, 12:54 AM
Whats on line 40?

Plus the 5 lines before it...

brcolow
09-26-2002, 01:14 AM
line 40 is this..

echo "Error adding article to database<hr>".mysql_error()."<hr>



and the five lines before it...


$sql = "INSERT INTO `articles` ('', '$title', '$about', '$screenshots', '$source')";

$result = mysql_query($query);
if ($result)
echo mysql_affected_rows()." article insterted into database. Thank you for posting your article!";
else
echo "Error adding article to database<hr>".mysql_error()."<hr>

Barak
09-26-2002, 06:34 AM
You missed the "; from the end of line 40 ie.
echo "Error adding article to database<hr>".mysql_error()."<hr>";

Barak
09-26-2002, 06:36 AM
oops - forgot you also can't put mysql_error() inside an echo. Rather than the else clause, just change $result to
$result = mysql_query($query) or die (mysql_error());

brcolow
09-26-2002, 10:24 AM
ok, i changd result to that, but now what else do i change in this..


if ($result)
echo mysql_affected_rows()." article insterted into database. Thank you for posting your article!";
else
echo "Error adding article to database<hr>".mysql_error()."<hr>";r article!";
}

brcolow
09-26-2002, 10:43 AM
bump it on up now!

Studio64
09-26-2002, 12:53 PM
You were supposed to replace the if statment with the assignment or statment and post what was printed out for the error...

My_SQLError() will display the error that occured in the statement when it is parsed to the SQL Server

brcolow
09-26-2002, 07:27 PM
i will post line one and some lines after it here...

<?
$title = $_POST['title'];
$about = $_POST['about'];
$screenshots = $_POST['screenshots'];
$source = $_POST['source'];

if (!$title || !$about || !$screenshots || !$source)
{


ok i did ALL of that now it gives me this, i hope this will be the final error....

You have an error in your SQL syntax near ''', 'this is the title', 'this is about', 'this are screenshots', 'this is sourc' at line 1

brcolow
09-26-2002, 11:08 PM
bump.... cmon studioooo

Studio64
09-27-2002, 03:18 AM
Originally posted by brcolow
bump.... cmon studioooo

Don't worry about bumping the thread... I can see it when you post to it. I'm just not on here 24/7 :D

I screwed up a few pages ago...

$sql = "INSERT INTO `articles` ('', '$title', '$about', '$screenshots', '$source')";

was wrong :D... As I said everyone makes mistakes.

The proper format of the statment should have been

"INSERT INTO `articles`VALUES ('', '$title', '$about', '$screenshots', '$source')";

brcolow
09-27-2002, 10:49 AM
ok i got this error,

Column count doesn't match value count at row 1

does that have something do do with my table set up incorrectly cause i had alot of trouble with that....?

brcolow
09-27-2002, 06:55 PM
bababaBUMP

Studio64
09-28-2002, 05:57 AM
I have never seen that error before so I don't really know what to say about it....

Quite possibly be because of a faulty table design...

Anyone else have any suggestions to the problem.

(once again.... don't bump threads..... it's against the rules and a mod will get on to you about it if you keep doing it)

Barak
09-28-2002, 06:44 AM
It means that the number of values you're trying to enter (5 based on Studio64's post) doesn't match the number of columns in your table (ie. you probably have less than 5 olumns in your table).

I'd have a look at the table you've made and check how many columns are in it.

It could also be that you're trying to insert 5 values without saying which columns they're supposed to go in. If you do this, you have to give a value for every column in your table, ie. there are more than 5 columns in the table. In this case (more likely I think), you'll need to do something like this (I don't know your column names, but I'm sure you'll figure out what I mean):
INSERT INTO articles (column_name, title, about, screenshots, source) VALUES ('', '$title', '$about', '$screenshots', '$source');

brcolow
09-28-2002, 03:02 PM
do alll of my coulumns need to have auto_increcement on them to access them via mysql and php?


also i am getting this error...

Parse error: parse error, unexpected $ in D:\www\www.flashstand.com\articles\article.php on line 37

and line 37 is this...

?>

and 5 lines above it..

$sql ="INSERT INTO articles (column_name, title, about, screenshots, source) VALUES ('', '$title', '$about', '$screenshots', '$source');

$result = mysql_query($sql) or die (mysql_error());;
if ($result)
My_SQLError()


and YES those r my table coulmn names..

Studio64
09-28-2002, 07:33 PM
Your SQL statement doesn't have a trailing "

brcolow
09-28-2002, 08:13 PM
ok i did that and NOW i get


Unknown column 'column_name' in 'field list'

thanks for the remendous ammount of help already

brcolow
09-28-2002, 10:24 PM
lol, that was really newbie of me, i deletd the coulmn_name part so everything is looking good except now i get this error, hopefully this will be the last one...


Column count doesn't match value count at row 1

brcolow
09-29-2002, 02:05 PM
does anyone out there know what this error means and how i can fix it?

Column count doesn't match value count at row 1

brcolow
10-01-2002, 09:17 PM
Ok, all of that is fixed and now im programming the session part of it... now all i need to do is the SELECT part, can someone gimme an url of where i can learn about them?

Studio64
10-02-2002, 12:11 AM
Best 35 Bucks you'll ever spend. Seriously (http://www.amazon.com/exec/obidos/tg/detail/-/0672317842/103-6980500-3372668)

But, for the free version...

SELECT [the field names] FROM [the table name] WHERE [condtion] LIMIT [number of records]

So...

SELECT ID, Name, Content FROM `articles` WHERE ID=4

brcolow
10-02-2002, 05:59 PM
the where id part... how would i make that a varible to the one there on, and for the name part, what is that?

Studio64
10-02-2002, 06:16 PM
"WHERE ID=$id" or "WHERE ID=".$id;

the "Name" reference was just a Field name I made up....

brcolow
10-02-2002, 07:28 PM
ok so for name should it be $title, $about, $screenshots, $source

?

Studio64
10-02-2002, 07:37 PM
no... When you put a $ infront of name it becomes a php variable.

When refering to SQL Field names simply call it by name.

brcolow
10-02-2002, 08:40 PM
i dont get it, what is a frigin field name!?

Studio64
10-02-2002, 10:58 PM
Originally posted by brcolow
i dont get it, what is a frigin field name!?

Best 35 Bucks you'll ever spend. Seriously (http://www.amazon.com/exec/obidos/tg/detail/-/0672317842/103-6980500-3372668)

brcolow
10-04-2002, 07:02 PM
Everything is working except this samll thing, I have this line to validate the session..


PHP:--------------------------------------------------------------------------------
if (session_is_registered("valid_user"))

--------------------------------------------------------------------------------


obviously because this is a windows machine it says undefined varbile, so should it be

PHP:--------------------------------------------------------------------------------
if (session_is_registered("$HTTP_POST_VARS['vaild_user]'))

--------------------------------------------------------------------------------


?
Thanks,
Brcolow