Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using PHP to Access MySQL

Status
Not open for further replies.

Hawki

Programmer
Oct 16, 1999
63
US
Hi

New to PHP accessing MySQL and getting a blank Web page. This is my first attempt to access data and looking for a good way to do so. This is code from two books. If you have examples, Web site or code, that work can you let me know

Thanks

***** PDP Code:
<?php
//common_db.inc
// Code from Beginning PHP4 p.402
$dbhost = 'localhost';
$dbusername = 'root';
$dbuserpassword = 'hawk';
$default_dbname = 'phpuser';

$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';

function db_connect() {
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;

$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);

// Code from Wrow - Beginning Databases with MySQL Start
// p.387 - 388
$query = "select * from access_log;";
$result_set = msql_quert($query) or die ("Query failed\n");
echo "<P>We have had " . mysql_num_row($result_set) . "patrons so far.";
// Code from Wrow - Beginning Databases with MySQL End

if(!$link_id) {
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host $dbhost.";
return 0;
}
else if(empty($dbname) && !mysql_select_db($default_dbname)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else return $link_id;
}

function sql_error() {
global $MYSQL_ERRNO, $MYSQL_ERROR;

if(empty($MYSQL_ERROR)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
?>

*** Access.log reads:******************************
127.0.0.1 - - [16/Oct/2007:05:33:19 -0400] "GET /test/ccommon_db.php HTTP/1.1" 200 -
127.0.0.1 - - [16/Oct/2007:05:34:34 -0400] "GET /test/ccommon_db.php HTTP/1.1" 200 -

*** Error Log reads:**********************************
C:\\Program Files\\Apache Group\\Apache2\\htdocs\\Test\\ccommon_db.php on line 21, referer: [Tue Oct 16 05:31:31 2007] [error] [client 127.0.0.1] PHP Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\\Program Files\\Apache Group\\Apache2\\htdocs\\Test\\ccommon_db.php on line 21, referer:
 
$query = "select * from access_log;";

should be:

$query = "select * from access_log";

-CALV1N
 
-CALV1N, I believe you have jumped to conclusions.

The semicolon in MYSQL is allowed and works in the same way as it does in Javascript and PHP, eg. it states that "do this, when you reach the semicolon".

The mysql can though be run without it.

I believe his error is here:
Code:
$result_set = [b]msql_quert[/b]($query) or die ("Query failed\n");

I guess he was thinking about:
and simply mis-spelled this function.


Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top