mxl Posted September 7, 2008 Share Posted September 7, 2008 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 More sharing options...
Goldeneye Posted September 7, 2008 Share Posted September 7, 2008 <?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 ?> Link to comment https://forums.phpfreaks.com/topic/123082-solved-my-first-db-query/#findComment-635629 Share on other sites More sharing options...
Goldeneye Posted September 7, 2008 Share Posted September 7, 2008 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 ?> Link to comment https://forums.phpfreaks.com/topic/123082-solved-my-first-db-query/#findComment-635634 Share on other sites More sharing options...
mxl Posted September 7, 2008 Author Share Posted September 7, 2008 Thank you.. The line mysql_query("SET concerts, 'utf8'" ); IS supposed to set the connection - encoding to UTF8. I'll give this changed code a whirl and be back if it doesn't work. Thanks again. Link to comment https://forums.phpfreaks.com/topic/123082-solved-my-first-db-query/#findComment-635643 Share on other sites More sharing options...
mxl Posted September 7, 2008 Author Share Posted September 7, 2008 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... ?> Link to comment https://forums.phpfreaks.com/topic/123082-solved-my-first-db-query/#findComment-635647 Share on other sites More sharing options...
jonsjava Posted September 7, 2008 Share Posted September 7, 2008 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 ?> Link to comment https://forums.phpfreaks.com/topic/123082-solved-my-first-db-query/#findComment-635650 Share on other sites More sharing options...
mxl Posted September 7, 2008 Author Share Posted September 7, 2008 Thank you jonsjava. It worked! Wow, now I'm on the way! Thanks so much. mxl Link to comment https://forums.phpfreaks.com/topic/123082-solved-my-first-db-query/#findComment-635662 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.