Re: Insert MySQL/PHP

From: Date: Mon, 05 Aug 2002 02:32:33 +0000
Subject: Re: Insert MySQL/PHP
References: 1 2 3  Groups: php.db 
Request: Send a blank email to php-db+get-20988@lists.php.net to get a copy of this message
At 03:27 PM 8/4/02 -0600, Mike wrote:
<?php // create connection $connection = mysql_connect("localhost","me","mypass"); // test connection if (!$connection) { echo "Couldn't make a connection!"; exit; }
Change that echo to: echo ":Couldn't make a connection<BR>\n", mysql_error(); or simplify the logic even more try: ($connection = mysql_connect( 'localhost', 'me', 'mypass' )) OR die( "Couldn't make a connection!<BR>\n" . mysql_error() . "<BR>\n" ); This is an old Perl trick. If the mysql_connect() is successful the value will be returned in $connection and the die() will not be executed because it is not needed to determine the return value for OR, but if mysql_connect() fails the OR will have to evaluate the die() which combines echo and exit into a single statement.
// select database $db = mysql_select_db("asta", $connection); // test selection if (!$db) { echo "Couldn't select database!"; exit; }
Same thing here echo ":Couldn't select database<BR>\n", mysql_error();
// create SQL statement $sql = "SELECT * FROM skywire"; // execute SQL query and get result $sql_result = mysql_query($sql,$connection);
Then add: if( !$sql_result ) { echo "Query failed - ", mysql_error(), "<BR>\n", $sql, "<BR>\n"; } Now you will know what went wrong...
// free resources and close connection mysql_free_result($sql_result); mysql_close($connection); ?>
I would display your results like this... // start results formatting ?> <TABLE BORDER=1> <TR>
      <TR>Site name</TH>
      <TH>Sent_link</TH>
      <TH>Notes</TH>
      <TH>Day</TH>
      <TH>ASTA</TH>
      <TH>astawireless</TH>
      <TH>astaio</TH>
</TR>"; <? while extract(mysql_fetch_array($sql_result, MYSQL_ASSOC )) : ?> <TR>
      <TD><?=$sitename?></TD>
      <TD><?=$sent_link?></TD>
      <TD><?=$notes?></TD>
      <TD><?=$day?></TD>
      <TD><?=$asta?></TD>
      <TD><?=$astawireless?></TD>
      <TD>$astaio</TD>
</TR> <? endwhile ?> </TABLE> <?
// free resources and close connection
I find that much easier to read and understand, not to mention changing the HTML format of the page. Add similar error checking in your second page. Most important is probably an error check after the insert. Be sure to print both the mysql_error() and the query string when there is a failure. Rick

« previous php.db (#20988) next »