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

?>

Link to comment
Share on other sites

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.";
}

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

I knew it. mysql_connect expects 3 strings.

 

$sql = mysql_connect('db946.perfora.net','dbo206008956','password');

 

ps: You'll do well to follow my tip on checking your queries succeed before use.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.