/    Sign up×
Community /Pin to ProfileBookmark

[code=php]<?php

$con = mysql_connect();

if (!$con)
{
die(‘Could not connect: ‘ .mysql_error());
}

mysql_select_db(“main”, $con)

$result = mysql_query(“SELECT * FROM Index”);

while($row = mysql_fetch_array($result))
{
echo “<p>”;
echo $row[‘Main’];
echo “</p>”;
}

mysql_close($con)
?>[/code]

Doesn’t work. Instead, the entire page goes white and says:

Parse error: syntax error, unexpected T_VARIABLE in C:wampwwwindex.php on line 26.

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiDec 12.2009 — I count 22 lines...
Copy linkTweet thisAlerts:
@GenixdeaeDec 12.2009 — You're missing a ; at the end of mysql_select_db() and mysql_close
Copy linkTweet thisAlerts:
@Not_to_mentionauthorDec 12.2009 — Gah! I hate semicolons. Thanks.
Copy linkTweet thisAlerts:
@MindzaiDec 12.2009 — Well spotted! You dont actually need one on the last line - I've even seen it said you shouldn't add one though I've never noticed any adverse effects myself.
Copy linkTweet thisAlerts:
@GenixdeaeDec 12.2009 — Well spotted! You dont actually need one on the last line - I've even seen it said you shouldn't add one though I've never noticed any adverse effects myself.[/QUOTE]You're welcome and thanks...call it naive(not even sure that's spelled right ?) or whatever you want, but why don't you need one at the end? is it because you're ending the PHP code segment? or is it just one of those "you don't have to if you don't want" lol
Copy linkTweet thisAlerts:
@MindzaiDec 13.2009 — I think the technical reason is that by adding the semicolon you are terminating a statement and starting a new one, which just gets terminated by the closing tag anyway. So no big deal, just means an extra CPU cycle or two.
Copy linkTweet thisAlerts:
@Not_to_mentionauthorDec 13.2009 — Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:wampwwwindex.php on line 28

now i get this.

heres the code again:

[code=php]<?php

$con = mysql_connect();

if (!$con)
{
die('Could not connect: ' .mysql_error());
}

mysql_select_db("main", $con);

$result = mysql_query("SELECT * FROM Index");

while($row = mysql_fetch_array($result)) --Line 26
{
echo "<p>";
echo $row['Main'];
echo "</p>";
}

mysql_close($con);
?>[/code]
Copy linkTweet thisAlerts:
@criterion9Dec 13.2009 — You should check to make sure the query executed:
[code=php]
if(!$result){
//Your result didn't execute
} else {
//the rest of your stuff here
}
[/code]
Copy linkTweet thisAlerts:
@Not_to_mentionauthorDec 13.2009 — Parse error: syntax error, unexpected '}', expecting ',' or ';' in C:wampwwwindex.php on line 32

it does that when i check if it executed.

line 32 is the else line.

[code=php]<?php

$con = mysql_connect();

if (!$con)
{
die('Could not connect: ' .mysql_error());
}

mysql_select_db("main", $con);

$result = mysql_query("SELECT * FROM Index");

if(!$result){

echo "Failed."

} else {

while($row = mysql_fetch_array($result))
{
echo "<p>";
echo $row['Main'];
echo "</p>";
}
}
mysql_close($con);
?>[/code]


*edit

HA! SEMICOLONS!
Copy linkTweet thisAlerts:
@Not_to_mentionauthorDec 13.2009 — Yes, it echoed failed, meaning that the query isnt working.
Copy linkTweet thisAlerts:
@GenixdeaeDec 13.2009 — heh those ;'s are out to get ya ?. Not sure if it makes a diff or not but you might want to add a ; at the end of your query[code=php]$result = mysql_query("SELECT * FROM Index;");[/code]
Copy linkTweet thisAlerts:
@NogDogDec 13.2009 — Do some debugging to find out [i]why[/i] the query failed. (The ";" is not needed at the end of the query string.)
[code=php]
$sql = "SELECT blah blah blah";
$result = msql_query($sql);
if($result == false)
{
user_error(mysql_error() . "<br />n$sql");
// whatever you want to do at this point if query fails
}
[/code]
×

Success!

Help @Not_to_mention spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...