Jump to content

[SOLVED] my first db query...


mxl

Recommended Posts

Hello,

 

I'm new to PHP and MySQL. 

 

This is the code that I'm been able to cobble together.

 

I just want to read the values from my database and assign each value (same field name as variable name) to a variable for use building my page...

 

Here's my code.  It bombs where I"m trying to get the results here:  $result = @conn->query($q_string);

 

 

 

<?php

 

$conn = mysql_connect ("localhost", "prefseat_reader", "pwd") or die('Cannot connect to the database because: ' . mysql_error());

mysql_select_db ("prefseat_events");

 

$pid = '695370';

$q_string = "SELECT * FROM concerts WHERE pid='".trim($pid)."'";

$conn->query("SET concerts, 'utf8'" );

 

$result = @conn->query($q_string);

if ($result=== FALSE)

{

$errno = $conn->errno;

    $errmsg = $conn->error;

    echo "Connect Failed with: ($errno) $errmsg<br />\n";

    $conn->close();

    exit;

}

else

{  //$result === TRUE

// fetch the data.  It should only come in as one row because $pid is unique.

while(($row_data = @result->fetch_assoc()) !== NULL)

{

echo <<<EOM

 

$event=$row_data['event'];

$venueName=$row_data['venueName'];

$venueAddress=$row_data['venueAddress'];

$venueCity=$row_data['venueCity'];

$venueState=$row_data['venueState'];

$venueZIP=$row_data['venueZIP'];

$venueSeatingChart=$row_data['venueSeatingChart'];

$weekday=$row_data['weekday'];

$month=$row_data['month'];

$day=$row_data['day'];

$year=$row_data['year'];

$time=$row_data['time'];

$pid2=$row_data['pid2'];

$time2=$row_data['time2'];

}

} //$result === TRUE

 

Any help would be appreciated.

 

Marcus

Link to comment
https://forums.phpfreaks.com/topic/123082-solved-my-first-db-query/
Share on other sites

<?php
$conn->query("SET concerts, 'utf8'" );

$result = @conn->query($q_string);
if ($result=== FALSE)
{
   $errno = $conn->errno;
    $errmsg = $conn->error;
    echo "Connect Failed with: ($errno) $errmsg
\n";
    $conn->close();
?>

 

Using $conn->function() tells PHP that you're using a class. If you actually do have a class, you forgot to declare it at the beginning of the script. If you don't actually have a class, then try this instead:

 

I'll give you a preemptive warning that this code is not tested, so it may not work 'out-of-the-box'.

 

Edit: I don't quite understand what mysql_query("SET concerts, 'utf8'" ); does. Are you setting the connection-encoding to UTF-8?

 

<?php
$conn = mysql_connect ("localhost", "prefseat_reader", "pwd") or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("prefseat_events", $conn);

$pid = '695370';
$q_string = "SELECT * FROM concerts WHERE pid=".trim($pid)."";
mysql_query("SET concerts, 'utf8'" );

$result = mysql_query($q_string);
if (mysql_num_rows($result) == 0) //Returns the number of rows found (if any)
{
    echo 'There are no results!';
    mysql_close($conn);
    exit;
}
else
{  //$result === TRUE
   // fetch the data.  It should only come in as one row because $pid is unique.
   while($row_data = mysql_fetch_array($result))
   {
      echo <<<EOM

      $event=$row_data['event'];
      $venueName=$row_data['venueName'];
      $venueAddress=$row_data['venueAddress'];
      $venueCity=$row_data['venueCity'];
      $venueState=$row_data['venueState'];
      $venueZIP=$row_data['venueZIP'];
      $venueSeatingChart=$row_data['venueSeatingChart'];
      $weekday=$row_data['weekday'];
      $month=$row_data['month'];
      $day=$row_data['day'];
      $year=$row_data['year'];
      $time=$row_data['time'];
      $pid2=$row_data['pid2'];
      $time2=$row_data['time2'];
    EOM;
   }
} //$result === TRUE
?>

Sorry, try this instead:

 

 

 

<?php
$conn = mysql_connect ("localhost", "prefseat_reader", "pwd") or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("prefseat_events", $conn);

$pid = '695370';
$q_string = "SELECT * FROM concerts WHERE pid=".trim($pid)."";
mysql_query("SET concerts, 'utf8'" );

$result = mysql_query($q_string);
if (mysql_num_rows($result) == 0) //Returns the number of rows found (if any)
{
    echo 'There are no results!';
    mysql_close($conn);
    exit;
}
else
{  //$result === TRUE
   // fetch the data.  It should only come in as one row because $pid is unique.
   while($row_data = mysql_fetch_array($result))
   {
      $data = <<<EOM

      $event=$row_data['event'];
      $venueName=$row_data['venueName'];
      $venueAddress=$row_data['venueAddress'];
      $venueCity=$row_data['venueCity'];
      $venueState=$row_data['venueState'];
      $venueZIP=$row_data['venueZIP'];
      $venueSeatingChart=$row_data['venueSeatingChart'];
      $weekday=$row_data['weekday'];
      $month=$row_data['month'];
      $day=$row_data['day'];
      $year=$row_data['year'];
      $time=$row_data['time'];
      $pid2=$row_data['pid2'];
      $time2=$row_data['time2'];
EOM;
echo $data;
   }
} //$result === TRUE
?>

Here's the error I get:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/prefseat/public_html/trans-siberian-orchestra/trans-siberian-orchestra-winnipeg-mb-november-3.php on line 23

 

Here's what I consider line 23:

 

    $event=$row_data['event'];

 

 

Here's the way I "adjusted" the code...  My very next line <?php is really the first line on the page.

 

 

<?php

 

$conn = mysql_connect ("localhost", "prefseat_reader", "pwd") or die('Cannot connect to the database because: ' . mysql_error());

mysql_select_db ("prefseat_events", $conn);

 

$pid = '695370';

$q_string = "SELECT * FROM concerts WHERE pid=".trim($pid)."";

mysql_query("SET concerts, 'utf8'" );

 

$result = mysql_query($q_string);

if (mysql_num_rows($result) == 0) //Returns the number of rows found (if any)

{

    echo 'There are no results!';

    mysql_close($conn);

    exit;

}

else

{  //$result === TRUE

  // fetch the data.  It should only come in as one row because $pid is unique.

  while($row_data = mysql_fetch_array($result))

  {

      $data = <<<EOM

 

      $event = $row_data['event'];

      $venueName = $row_data['venueName'];

      $venueAddress = $row_data['venueAddress'];

      $venueCity = $row_data['venueCity'];

      $venueState = $row_data['venueState'];

      $venueZIP = $row_data['venueZIP'];

      $venueSeatingChart = $row_data['venueSeatingChart'];

      $weekday = $row_data['weekday'];

      $month = $row_data['month'];

      $day = $row_data['day'];

      $year = $row_data['year'];

      $time = $row_data['time'];

      $pid2 = $row_data['pid2'];

      $time2 = $row_data['time2'];

EOM;

echo $data;

  }

} //$result === TRUE

 

$title = $event.' Tickets '.$venueCity.', '.$venueState.' at '.$venueName.' '.$month.' '.$day.', '.$year.' - Buy '.$event.' Tickets for '.$venueCity.', '.$venueState.' '.$venueName;

 

 

...  and the rest of the page continues...

 

?>

 

you tried defining variables within an echo.

cleaned up version:

<?php
$conn = mysql_connect ("localhost", "prefseat_reader", "pwd") or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("prefseat_events", $conn);

$pid = '695370';
$q_string = "SELECT * FROM concerts WHERE pid=".trim($pid)."";
mysql_query("SET concerts, 'utf8'" );

$result = mysql_query($q_string);
if (mysql_num_rows($result) == 0) //Returns the number of rows found (if any)
{
    echo 'There are no results!';
    mysql_close($conn);
    exit;
}
else
{  //$result === TRUE
   // fetch the data.  It should only come in as one row because $pid is unique.
   while($row_data = mysql_fetch_array($result))
   {
   	  $event=$row_data['event'];
      $venueName=$row_data['venueName'];
      $venueAddress=$row_data['venueAddress'];
      $venueCity=$row_data['venueCity'];
      $venueState=$row_data['venueState'];
      $venueZIP=$row_data['venueZIP'];
      $venueSeatingChart=$row_data['venueSeatingChart'];
      $weekday=$row_data['weekday'];
      $month=$row_data['month'];
      $day=$row_data['day'];
      $year=$row_data['year'];
      $time=$row_data['time'];
      $pid2=$row_data['pid2'];
      $time2=$row_data['time2'];
      $data = <<<EOM
EOM;
echo $data;
   }
} //$result === TRUE
?>

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.