Jump to content

[SOLVED] First script - First problem


christofurr

Recommended Posts

Shows a parse error when I access the page it's uploaded to.

 

<html>
<body>

<?php

/* PHP script for MySQL transactions */

$sql = mysql_connect(server_host,user,pass);

if (!&sql)
die('Error message.' . mysql_error());
else
echo "Successful connect.";

/* This orders some data to be displayed*/

mysql_select_db("database_name", $con);

$selalldat = mysql_query("SELECT * FROM table_name");

while($shoalldat = mysql_fetch_row($selalldat))
{
echo "<p>";
echo $shoalldat[1] . " " . $shoalldat[2] . " " . $shoalldat[3] . " " . $shoalldat[4] . " " . $shoalldat[5] . " " . $shoalldat[6] . " " . $shoalldat[7] . " " . $shoalldat[8] . " " . $shoalldat[9] . " " . $shoalldat[10] . " " . $shoalldat[11];
echo "</p>";
}

mysql_close($sql);

?>

</body>
</html>

 

My database has 11 columns and a dozen rows of information. I made sure that my connection information was correct. Where's my mistake?

Link to comment
https://forums.phpfreaks.com/topic/53810-solved-first-script-first-problem/
Share on other sites

ps; I'll gibe you a tip nice and early. This is a good habit to get into. Always check your queries succeed and return a result before trying to use them.

 

<?php

if ($selalldat = mysql_query("SELECT * FROM table_name") && mysql_num_rows($selalldat)) {
  while($shoalldat = mysql_fetch_row($selalldat)) {
    echo "<p>";
    echo $shoalldat[1] . " " . $shoalldat[2] . " " . $shoalldat[3] . " " . $shoalldat[4] . " " . $shoalldat[5] . " " . $shoalldat[6] . " " . $shoalldat[7] . " " . $shoalldat[8] . " " . $shoalldat[9] . " " . $shoalldat[10] . " " . $shoalldat[11];
    echo "</p>";
  }
}

mysql_close($sql);

?>

The $con variable was another error. Let me switch it up and see if that fixes it.

 

Parse errors are caused by incorrect syntax. Using incorrect variables will generate other errors / warnings. the only thing I can see is a lack of curly braces around your first if. PHP should handle that OK though.

 

if (!$sql) {
  die('Error message.' . mysql_error());
} else {
  echo "Successful connect.";
}

<html>
<body>


<?php

/* PHP script for MySQL transactions */

$sql = mysql_connect(db946.perfora.net,dbo206008956,password);
if (!$sql)
{
die('Connect failed.' . mysql_error());
}


/* This orders some data to be displayed*/

mysql_select_db("db206008956", $sql);

$selalldat = mysql_query("SELECT * FROM Users");

while($shoalldat = mysql_fetch_row($selalldat))
{
echo "<p>";
echo $shoalldat[1] . " " . $shoalldat[2] . " " . $shoalldat[3] . " " . $shoalldat[4] . " " . $shoalldat[5] . " " . $shoalldat[6] . " " . $shoalldat[7] . " " . $shoalldat[8] . " " . $shoalldat[9] . " " . $shoalldat[10] . " " . $shoalldat[11];
echo "</p>";
}


mysql_close($sql);

?>

</body>
</html>

OOOH I SEEE. And does it matter if I use quotes instead of apostrophes?

 

You can use single or double quotes.

 

And could you explain how the check is done and how it works?

 

Sure. An example.

 

<?php

  $sql = "SELECT * FROM foo";
  $result = mysql_query($sql);
  // because mysql_query returns false on failure or a result resource (remember that everything
  // other than false is considered true in php) on success its a good idea (can generate errors otherwsie)
  // to check it before trying to use it.
  if ($result) {
    // it is now safe to use $result.

    // you also want to make sure you actually got some results from your query.
    if (mysql_num_rows($result)) {
      // now... in here you can display your data.
    } else {
      // no data was retrieved by your query
    }
    
  } else {
    // its not safe to use result in here.

  }

?>

 

Hope that helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.