Help: PHP w/ mySQL on MacOS 10.3: no connection to localhost

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
imme
Forum Newbie
Posts: 14
Joined: Fri Nov 21, 2003 11:51 am

Help: PHP w/ mySQL on MacOS 10.3: no connection to localhost

Post by imme »

Hi,
I got a big problem with MacOS X, PHP and mySQL. I installed mySQL and PHP and both are running (I can do mySQL stuff through the Terminal and phpMyAdmin) but php scripts which have been working on other machines don't work on my localhost. There is no error, it just isn't doing anything.
Thank you in advance!

- imme
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post by mrvanjohnson »

phpMyAdmin is a web based PHP application so if that's working then everything seems like it's set up correctly. What's the error message you are getting?

What is the address you use to connect to phpMyAdmin?

Drop a

Code: Select all

<?php 
phpinfo () ; 

?>
into your web server root and see what you get.
imme
Forum Newbie
Posts: 14
Joined: Fri Nov 21, 2003 11:51 am

Post by imme »

phpmyadmin runs on localhost...
that what's confussing me so much. it seems to be set up correctly.
I get no error message. The select statements don't fetch any data or it is not displayed. I don't know... :-(
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post by mrvanjohnson »

Run a phpinfo and let establish that is OK. Also post some of your code. Sounds more like a coding problem than anything else.

Post the Select statment you are trying
imme
Forum Newbie
Posts: 14
Joined: Fri Nov 21, 2003 11:51 am

Post by imme »

phpinfo works



here's one simple code example that does not work:
(it does work perfectly fine on our ISP server)




<?php
/* connect db */
mysql_connect("localhost");
mysql_select_db("kaspar");
?>

<HTML>
<HEAD>
<title>Telefonverzeichnis</title>
</HEAD>
<BODY>
<H2>Telefonverzeichnis</H2>

<!----------- Eingabemaske----------------------->

<form action="telefon.php" method="post">
<input type="Hidden" name="ersteraufruf" value="1">
<table border="0" align="center">
<tr>
<td>Name:</td>
<td><input type="Text" name="name"></td>
</tr>
<tr>
<td>Vorname:</td>
<td><input type="Text" name="vorname"></td>
</tr>
<tr>
<td>Telefon:</td>
<td><input type="Text" name='telnr'></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="Text" name='email'></td>
</tr>
<tr>
<TD COLSPAN="2" ALIGN="CENTER"><input type="Submit" value="Abschicken"></td>
</tr>
</table>
</form>


<!----------- Auswertung der Eingabe ----------------------->

<table border=0 >
<tr>
<B>
<td>Name:</td>
<td>Vorname:</td>
<td>Telefonnummer:</td>
<td>Email:</td>
</B>
</tr>
<?php

//-------------------- Eingabefelder auswerten---------------

if ($ersteraufruf==1) {
if ($name=="" || $vorname=="" || $telnr=="" || $email=="" ) {
?>
<script>
window.alert('Bitte alle Felder ausf&uuml;llen.');
</script>
<?php
} else {

// -------------------- Werte in Db eintragen---------------


$datum=date("Y-m-d");
mysql_query
("INSERT INTO kk_tel (name,vorname,telnr,email) VALUES ('$name','$vorname','$telnr','$email')");
}
}


// --------------------Ausgeben alle Datens‰tze der DB ---------------
// -------------------- SQL-Statement ausf&uuml; ren---------------

$anweisungs_id=mysql_query
("SELECT name,vorname,telnr,email FROM kk_tel ORDER BY name");

// -------------------- Anzahl der Datens‰tze ---------------

$num = mysql_numrows($anweisungs_id);

// -------------------- Datens&auml;tze Zeile f¸r Zeile ausgeben---------------

for($i=0;$i<$num;$i++)
{
// -------------------- eine Zeile vorbereiten ---------------

$zeile = mysql_fetch_array($anweisungs_id);
$name=htmlspecialchars($zeile["name"]);
$vorname=htmlspecialchars($zeile["vorname"]);
$telnr=htmlspecialchars($zeile["telnr"]);
$email=htmlspecialchars($zeile["email"]);


// --------------------eine Zeile ausgeben---------------

echo "<tr>
<td>$name</td>
<td>$vorname</td>
<td>$telnr</td>
<td><a href='mailto:$email'>$email</a></td>
</tr>";
}
?>
</table>

</BODY>
</HTML>

<! -------------------- Verbindung zur DB schliessen--------------->

<?php
mysql_close();
?>
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post by mrvanjohnson »

OK very good. What doesn't work? Does the page not display when you first attempt to hit it or is it after you try to submit the form.. A little more information please.


You might want to edit your last post and throw your code into [ code ] tags.
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post by mrvanjohnson »

Alright first question, is this file telefon.php? So in other words you are submitting back to itself right?

Next question, after you POST you don't seem to pick up the variables again fro mthe POST. Unless you have register_globals=ON this will not work.

Next, depending on the status of "$ersteraufruf" (which I can't find defined anywhere in your code before the IF statment) you do an insert. Then regardless of the INSERT you turn around and do a SELECT to fill you table at the bottom of the page. But you don't actually grab the data you just get a [php_man]mysql_numrows[/php_man] and then go into a for loop. I think what you wanted to do here was something like this

Code: Select all

<?php
while ($zeile = mysql_fetch_array ($anweisungs_id)){
echo "<tr> 
<td>".$zeile['name']."</td> 
<td>".$zeile['vorname']."</td> 
<td>".zeile['telnr']."</td> 
<td><a href="mailto:".$zeile['email']."">".$zeile['email']."</a></td> 
</tr>"; 

}
?>
in place of this chunk of code

Code: Select all

$num = mysql_numrows($anweisungs_id); 

// -------------------- Datens&amp;auml;tze Zeile f¸r Zeile ausgeben--------------- 

for($i=0;$i&lt;$num;$i++) 
&#123; 
// -------------------- eine Zeile vorbereiten --------------- 

$zeile = mysql_fetch_array($anweisungs_id); 
$name=htmlspecialchars($zeile&#1111;"name"]); 
$vorname=htmlspecialchars($zeile&#1111;"vorname"]); 
$telnr=htmlspecialchars($zeile&#1111;"telnr"]); 
$email=htmlspecialchars($zeile&#1111;"email"]); 


// --------------------eine Zeile ausgeben--------------- 

echo "&lt;tr&gt; 
&lt;td&gt;$name&lt;/td&gt; 
&lt;td&gt;$vorname&lt;/td&gt; 
&lt;td&gt;$telnr&lt;/td&gt; 
&lt;td&gt;&lt;a href='mailto:$email'&gt;$email&lt;/a&gt;&lt;/td&gt; 
&lt;/tr&gt;"; 
&#125;
Also I think in your situation, you would mainly use the [php_man]htmlspecialchars[/php_man] during the insert of data into your database.

Hope some of this helps.
imme
Forum Newbie
Posts: 14
Joined: Fri Nov 21, 2003 11:51 am

Post by imme »

hi mrvanjohnson,
thanks for your help so far. I guess it really is a coding problem. Funny that it works on the ISP server, though.
I'll try your suggestions. Unfortunately, I won't get to do that before Monday...
- imme
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post by mrvanjohnson »

Just let us know how it goes.
imme
Forum Newbie
Posts: 14
Joined: Fri Nov 21, 2003 11:51 am

Post by imme »

Besides the badly written script, the problem turned out to be a configuration error. Since I'm using a relatively new PHP version, "Register_globals" in the php.ini was by default set to "off".
:?
Post Reply