Issues connecting php and mysql as well as others

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluepsykoe
Forum Newbie
Posts: 13
Joined: Mon Jun 04, 2007 11:31 am

Issues connecting php and mysql as well as others

Post by bluepsykoe »

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am having issues with connecting to my mysql database. I am not getting any error messages but when trying to add records nothing happens. I tried writing a script to see if I could display the database or get the appropriate error messages. My code is as follows:

Code: Select all

<?
// Connecting, selecting database
$link = mysql_connect('localhost', 'user', 'pass')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('test') or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM addressbook';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>
However, what I get is a page that displays this:

Code: Select all

\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t\n"; foreach ($line as $col_value) { echo "\t\t$col_value\n"; } echo "\t\n"; } echo ""; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); ?>
and nothing else.

To my understanding, what it is doing is it is taking the ">" from the first "<table\>" and calling it the ending tag. For some reason it does not want to display the table. I am not sure if there is something wrong with the configuration or what. I am able to get access to my mysql server through phpmyadmin and am able to add databases and all functionality seems to be there. But for some reason it seems I am unable to connect through code on a PHP script I made. can anyone help me with this?

Also one more thing, which I'm not sure is really any kind of issue of concern or not but thought I should mention it is that, when I write php code using "<?php" I get absolutely nothing on the page where when using "<?" seems to be parsing the code fine on a simple "hello world" script as well as with a sendmail script.

I am a newbie at this and would really appreciate any help.

Thanks

Mike


Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

use <?php NOT <?.
short_open_tag is optional on the server, and is not reliable. Not to mention that it is being depreciated in future versions of PHP.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

If you get php in output another reasons could be that file extension is other than associated with php (i guess this is incorrect usage of word "associated" in English :? ) . By default php is executing files only with .php
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Re: Issues connecting php and mysql as well as others

Post by maliskoleather »

bluepsykoe wrote:To my understanding, what it is doing is it is taking the ">" from the first "<table\>" and calling it the ending tag.
just noticed this part.
this is impossible. most likley what is happeing, is that its not parsing any of the php, and just catching the <table> tag... to check this, you should view the source of your page, and see what is output there.

also, as pointed out above, make sure your page has a .php extension to it... not all servers support .php3 or .phtml or many of the other 'hybrid' php extensions.
bluepsykoe
Forum Newbie
Posts: 13
Joined: Mon Jun 04, 2007 11:31 am

Post by bluepsykoe »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[quote] bluepsykoe wrote:
To my understanding, what it is doing is it is taking the ">" from the first "<table\>" and calling it the ending tag.

just noticed this part.
this is impossible. most likley what is happeing, is that its not parsing any of the php, and just catching the <table> tag... to check this, you should view the source of your page, and see what is output there. [/quote]

This is what is output from the source of the page.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/php; charset=utf-8" />
<title>Untitled 1</title>
</head>

<body>
<?
// Connecting, selecting database
$link = mysql_connect('localhost', 'user', 'pass')
    or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('test') or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM addressbook';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
print("<table>\n");
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

</body>

</html>
the red is output as it is commented out.

The page is a .php file.
use <?php NOT <?.
short_open_tag is optional on the server, and is not reliable. Not to mention that it is being depreciated in future versions of PHP
When I use <?php I get nothing but a blank page, all html is ignored as well

Thanks for any further help

Mike


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

A blank page is usually a parse error. If you are getting output of PHP code in the resultant HTML then it is more than likely associated with the short_open_tag you are using.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try this as a quick test and report back any output (make sure to replace the $line array index 'fieldname' to an actual fieldname of your table):

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

// Connecting, selecting database
$link = mysql_connect('localhost', 'user', 'pass') or die('Could not connect: ' . mysql_error());
echo 'Connected successfully<br />';
mysql_select_db('test') or die('Could not select database');
echo 'We have selected our DB as well.<br />';

// Performing SQL query
$query = 'SELECT * FROM addressbook';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

if (mysql_num_rows($result) > 0) {
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo '<p>' . $line['fieldname'] . '</p>';
    }
} else {
    echo 'There are no results to report.<br />';
}

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>
AvalonMel
Forum Newbie
Posts: 4
Joined: Mon Jun 04, 2007 12:29 pm

Post by AvalonMel »

I just had a similar problem and changing <? to <?php fixed the error for me.
bluepsykoe
Forum Newbie
Posts: 13
Joined: Mon Jun 04, 2007 11:31 am

Post by bluepsykoe »

ry this as a quick test and report back any output (make sure to replace the $line array index 'fieldname' to an actual fieldname of your table):
php:
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

// Connecting, selecting database
$link = mysql_connect('localhost', 'user', 'pass') or die('Could not connect: ' . mysql_error());
echo 'Connected successfully<br />';
mysql_select_db('test') or die('Could not select database');
echo 'We have selected our DB as well.<br />';

// Performing SQL query
$query = 'SELECT * FROM addressbook';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

if (mysql_num_rows($result) > 0) {
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<p>' . $line['fieldname'] . '</p>';
}
} else {
echo 'There are no results to report.<br />';
}

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>
Everah
PostPosted: Mon Jun 04, 2007 12:36 pm Post subject:
A blank page is usually a parse error. If you are getting output of PHP code in the resultant HTML then it is more than likely associated with the short_open_tag you are using.
OK this is what the output was

Code: Select all

Fatal error: Call to undefined function mysql_connect() in C:\Users\Michael\apache\htdocs\addrecord.php on line 6
so I gather that php can't find the function to connect to mysql???

again I am not sure if this means anything or not but I am able to connect to the mysql server using phpmyadmin

thanks

Mike
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That means that the mysql extension is not enabled. Run the following in a new file and tell us the results please.

Code: Select all

<?php

$neg = array('off', 0, false, '', null);
$flags = array(
	'Register Globals' => 'register_globals',
	'Short Tags' => 'short_open_tag',
	'Display Errors' => 'display_errors',
	'Magic Quotes GPC' => 'magic_quotes_gpc',
	'Magic Quotes Runtime' => 'magic_quotes_runtime',
	'Magic Quotes Sybase' => 'magic_quotes_sybase',
);
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
foreach ($flags as $n => $v)
{
	$flags[$n] = (in_array(strtolower(ini_get($v)), $neg) ? 'Off' : 'On');
}
$flags['Config file'] = get_cfg_var('cfg_file_path');
if (empty($flags['Config file']))
{
	$flags['Config file'] = '-';
}
$cli = (php_sapi_name() == 'cli');
$eol = "\n";

$gle = get_loaded_extensions();
$rows = array();
$le = '';
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
	$le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . $eol;
}

$ec = array(
	'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
	'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
	'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
	'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
	if (($t & $v) == $v)
	{
		$e[] = $n;
		$t ^= $v;
	}
}
if (ceil(count($ec) / 2) + 1 < count($e))
{
	$e2 = array();
	foreach ($ec as $n => $v)
	{
		if (!in_array($n, $e) and $n != 'E_ALL')
		{
			$e2[] = $n;
		}
	}
	$er = $er . ' ((E_ALL | E_STRICT) ^ ' . implode(' ^ ', $e2) . '))';
}
else
{
	$er = $er . ' (' . implode(' | ', $e) . ')';
}

if (!$cli)
{
	echo '<html><head><title>quick info</title></head><body><pre>', $eol;
}

echo 'PHP Version: ', $ve, $eol;
echo 'PHP OS: ', $os, $eol;
echo 'Error Reporting: ', $er, $eol;
foreach ($flags as $n => $v)
{
	echo $n, ': ', $v, $eol;
}
echo 'Loaded Extensions:', $eol, $le, $eol;

if (!$cli)
{
	echo '</pre></body></html>', $eol;
}

?>
bluepsykoe
Forum Newbie
Posts: 13
Joined: Mon Jun 04, 2007 11:31 am

Post by bluepsykoe »

OK I got it to connect I hade to change

mysql_connect to mysqli_connect

but then the following error shows up

Code: Select all

Connected successfully

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\Users\Michael\apache\htdocs\addrecord.php on line 8
Could not select database
line 8 is written as:

Code: Select all

mysqli_select_db('test') or die('Could not select database');
what is the other parameter?

Thanks

Mike
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

mysqli_select_db()... Read up on it.
bluepsykoe
Forum Newbie
Posts: 13
Joined: Mon Jun 04, 2007 11:31 am

Post by bluepsykoe »

OK Thank you all for your help in sending me in the right direction. I am at a point now where I should be able to look up any issues I have.

Thank you very much

Mike
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Remember, the PHP manual is your friend. Once you get to that place, everything gets a little easier. :wink:
Post Reply