The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   Proper path for PHP? (http://hintsforums.macworld.com/showthread.php?t=30960)

fvs 11-21-2004 10:29 AM

Proper path for PHP?
 
I'm just trying to learn PHP and would like to know how to setup an easy path to the proper directory to store my php files. Any suggestions,Thanks

ezmobius 11-21-2004 04:45 PM

php path?
 
If you are talking about .php script files that you are working on, there is really no 'right' place to put them. If you are using apache on localhost to test these scripts then a good place to put them is in ~/Sites. This way you can run your scripts in a web browser using the url:
http://localhost/~USERNAME/phpscript.php
Where USERNAME is your short username and phpscript.php is the name of your php script inside of ~/Sites.
Good luck with the PHP. It's a really fun language thats pretty easy to learn.

fvs 11-21-2004 05:43 PM

Hello ezmobius, I had them there, but they wouldn't run, I had to put them in my
/Websever/Documents to run them, I'm on a Mac OS X.2 and I can't rum them from my ~/Sites? They run from there, I have to go there to run them. Thanks

jeffo 11-22-2004 11:03 AM

are you sure that you have apache set to run php files? php is not turned on by default i dont think. the apache manual is on your own computer at http://localhost/manual/ or you can just reply back and we can help to get you going.

Edit: I did not read carefully enough. if you go into the /etc/httpd/users/USERNAME.conf file you will have to add the option in there i believe. That give the admin more control over who can do what on the computer. man do i wish i was near my mac right now.

ezmobius 11-22-2004 11:28 AM

Hosting a website on your computer
You can use the Apache Web server software included with Mac OS X to host a website on your computer.
Open System Preferences and click Sharing.
Click Services.
Turn on Personal Web Sharing.
webpages you put in your Sites folder (in your home folder) can be browsed at http://your.computer.address/~yourusername/.
For yourusername, use your short user name, and include the ending slash (/).

Pages you put on your hard disk in Library/WebServer/Documents can be browsed at http://your.computer.address.
To find your computer's address, select Personal Web Sharing in the Services window of Sharing preferences. The address is displayed below the Services window.
To learn more, use a browser to open index.html in your Sites folder, or look in /Library/WebServer/Documents/Manual.

This came right from apples help. You might have already done this though. You can tell if you can see your .php pages at http://localhost/
but not at http://localhost/~username/
Also by default you need to turn on .php parsing in your apache.conf file. Look for a line that say something about server parsed applicationX languages or something to that extent and uncomment the line with the .php extenstions. If you can't get it working right email me at ezra@yakima-herald.com and I will wend you my apache.conf file that is all set up for everything you'll ever need to do with your mac server.

fvs 11-23-2004 05:46 PM

Thanks Ezra & Jeffo, I think I have it all set up, I'm now trying to get mysql/php to my apache web page. I'd like to read mysql data on my web page. I've tried a few scripts and I haven't succeeded yet. Got error;

Warning: Failed opening 'dbconnect' for inclusion (include_path='.:/usr/lib/php') in /Library/WebServer/Documents/usedata.php on line 2
No Database Selected

I don't know what it means? I guess my script is wrong or I left out something.

ezmobius 11-23-2004 05:55 PM

"Warning: Failed opening 'dbconnect' for inclusion (include_path='.:/usr/lib/php') in /Library/WebServer/Documents/usedata.php on line 2
No Database Selected"

It looks like you are trying to include a file called 'dbconnect' and your script is having a hard time finding the file to include. You need to have the 'dbconnect' file in the same directory as the script that you are trying to include it in. Or if the 'dbconnect' script needs to be in a different directory then you should include it like this:
<?php include '/path/to/dbconnect'; ?>
That way it is unambiguous(sp?) and the include will work. Also that line where it says No Database selected is probably because your include failed so it did not give your script the info it needed to connect to the db which is what I assume 'dbconnect' is trying to do.

Post your whole script here and I will help you get it running.

fvs 11-23-2004 07:21 PM

Hello Ezra, I probebly need more that this script to link up to php, I put a few scripts in this file already,

<?php
unset ($dbfailed);

if (mysql_connect("localhost","root"))
{
mysql_select_db("duckwear") or $dbfailed=mysql_error();
}
else
{
$dbfailed="Could not connect to database";
}
if(isset($dbfailed))
{
echo $dbfailed;

}
?>

ezmobius 11-23-2004 07:39 PM

It looks like you are making things too complicated for yourself. Without the rest of the script, I can't tell exactly what you are trying to do but as far as what you just posted goes, try this instead:
PHP Code:

$db mysql_connect('localhost','root');
mysql_select_db("duckwear"$db) or die(mysql_error());
// the first 2 lines connect to mysql and select the database for you
$query="SELECT * FROM tablename";
$result mysql_query($query$db) or die(mysql_error());
//the 3rd and 4th lines set up your query and execute it
while($row mysql_fetch_assoc($result))
     echo 
$row[column_name];
/*The last 2 lines use a while loop to get the results out of the database and echo them one at a time until they have all been echoed*/ 

Now you just need to replace the column_name in $row[column_name] with whatever the name of the column in your database table is that you want to display. This loop will go ahead and display ALL results that match the $query so change the query if you need to. Right now the query is selecting everything in your table.

Good lUck.
Post your whole script and I can help you get it done!

fvs 11-24-2004 08:24 AM

I stiil get an error;

Warning: Access denied for user: 'root@localhost' (Using password: NO) in /Library/WebServer/Documents/linktomysql.php on line 2

Warning: MySQL Connection Failed: Access denied for user: 'root@localhost' (Using password: NO) in /Library/WebServer/Documents/linktomysql.php on line 2

Warning: Supplied argument is not a valid MySQL-Link resource in /Library/WebServer/Documents/linktomysql.php on line 3
Access denied for user: 'root@localhost' (Using password: NO)

It seems I have to enter a password, I don't know how to enter one for php, I have one for mysql in my terminal and it works, How do I enter one that accesses web?
Can we start from begining to get link from mysql/php? Thanks Ezra

ezmobius 11-24-2004 11:56 AM

You need to pass the password for mysql in the mysql_connect statement. I assumed that you had not set a password for mysql from your code that you entered above. The way to do it like this:
PHP Code:


$db 
mysql_connect('localhost','root','password'); 

Thats all you need to change. So lets start from the beggining. For demonstrations sake , lets say we are storing some quotes in the database and want to pull a different one every day. Lets assume that your root password for mysql is password and you are connecting on localhost. Lets say that your database table is called quotes and this table has 3 columns 'id', 'quote', and 'weekday'. 'id' will be our primary key to the database and should be set to 'not null primary key auto increment'. Quotes should be varchar 255(unless you need more room than 255 chars, then use text) and weekday will also be varchar 12. This way every entry in the db will have a unique key that automaticaly increments itself so you never need to enter anything there it does it for you. The other 2 columns should contain the quotes and the day of the week. So lets make it happen.
PHP Code:

<?php
$day 
date(l); /* This will assign the day of the week to $day like this: Wednesday 
     with the first letter caps and the whole day spelled out. Make sure that the weekday column
     in you db table has the days of the week formatted in the same way*/
$dbconnect mysql_connect('localhost','root','password');//connects to db as root with password of 'password'
mysql_select_db('quotes'); // select the quotes table
$query="SELECT quote FROM quotes WHERE weekday='$day' ORDER BY RAND()";/*creates the query that says select the 
quote column from the quotes table where the weekday column matches the current day of the week. 
    Then it randomizes the results so you can have as many quotes per day of the week as you want*/
$result mysql_query($query$dbconnect) or die(mysql_error()); /* query the db with $query and the db
    connection $dbconnect and place it in $result or die and print the mysql error*/
$row mysql_fetch_assoc($result); // fetch the first matching result and put it in $row
echo $row[quote];//prints the quote!!
?>
Now if you save this php file as 'quotes.php' then the easy way to use it is 
to build your html page just like you normally would except don't use a .html extension
 use a .php extension instead. Then decide where you would like the quote to 
appear on a page. Lets assume you want it inside a table cell:
<html>
<body>
<table width="450" border="1">
  <tr>
    <td><?php include 'quotes.php'?></td>
  </tr>
</table>
</body>
</html>

Thats it!! Hopefully it all works for you. Let me know if you still have problems or you need to access more than 1 result at a time. Good Luck!!


jeffo 11-24-2004 12:21 PM

by default mysql has a root password and you would need to reset it to your own password. I think the reason why you are getting rejected is because you are trying to access the database with no password being used and one should be required so it is failing. have you been using mysql all along on this computer, or is it a new install?

edit: sorry aparently i should have refreshed the page before i posted. (I had it open for a while before i went back to reply)

fvs 11-24-2004 02:43 PM

jeffo, I have been using mysql right along, I do have a password for mysql and it works fine in the terminal and I'm using it. I did enter a password into $db = mysql_connect('localhost','root','password'); But it didn't work.
I still can't access the mysql data from php code.

ezmobius 11-24-2004 05:32 PM

When you log in to mysql from the terminal, do you use this command?
mysql -u root -p
and then mysql asks for your password? If you're just typing mysql by itself then you are not using the root user.
Heres how you can change the password and be sure it will work. Type the following command:
mysqladmin -u root -p password secret
This command will run and ask for your CURRENT root password, after you type your password correctly it will change the password to 'secret' so replace 'secret' with whatever you want to change the password to. If you complete the command succesfully it will dump you back to the command prompt with no errors and your new password will be set.

fvs 11-24-2004 06:45 PM

Hi Ezra, Your script worked with html, But with [No Data Selected] message your box [] was there and I think I got the password in there now to get the data into the web. I guess the html part is working, but no data from php? Thanks for staying with me. fvs

ezmobius 11-24-2004 06:57 PM

I'm a little unclear as to what you are talking about in your last post. Can you explain it a bit better? What kind of data are you trying to get out of the database? Will you need more than 1 result? Please post ALL of the code you are using and explain what you are trying to do. Also tell me what your database is called and what the name of the column in the db is called that you are trying to get information out of. If you post all this info I will write the script for you and you will just have to replace the password with the correct one to get it to work. Then once your script is working you can start to see how and why things are working and move on from there.

I'm about to get on the road to drive to my families house for thanksgiving. So I won't be able to get back to you until later tonight or tommorow. But I will help you get this script up and running.

Also you might try removing the ORDER BY RAND() part of the query until you can get the data to print and the rest of the script to work, then we'll get the random part to work.

fvs 11-25-2004 07:15 AM

Ezra Happy Thanksgiving, I'm also traveling and will be back Friday night.
The data code is from a book "Mysql Essential Skills" The database is called "duckwear"
the tables are duck_cus, duck_sales the tables have name,address, city, state, zip,
the sales report have design,winter ,spring,summer,fall., I tried following from the book
but lost a step or two, Learning from a book is kind of hard for me when I start to stumble I need to talk it out that's why I'm on this forum to get a different view of how I'm looking at it. Your php code you posted above didn't work. But I did straighten out the "localhost, user, password" that's taken care of. Thanks again, Happy Holiday.
regards, fvs

ezmobius 11-28-2004 03:04 PM

PHP Code:

<html>
<body>
<table width='450'>
<?php 
$dbconnect 
mysql_connect('localhost','root','password');
mysql_select_db('duckwear');
$query="SELECT * FROM duck_cus";
$result mysql_query($query$dbconnect) or die(mysql_error());
while(
$row mysql_fetch_assoc($result)); 
    {
    echo 
"<tr>";
    echo 
"<td>$row[name]</td><td>$row[address]</td><td>$row[city]</td><td>$row[state]</td><td>$row[zip]</td>";
    echo 
"</tr>";
    }
?>
</table>
</body>
</html>

So this script should select ALL the records in your duck_cus table and make a html table that will loop through ALL of the records in the table and place them in their own table cells. You just need to replace 'password' with your own password, but the rest should work without any change according to your description of your database that you are working on. Save the file with a .php extension and give it a try!

fvs 11-29-2004 11:17 AM

Hello Ezra,
Thanks for staying, I tested it out and the first time I got an error message that said that there was no db duckwear, so I tried another db that is work_2004 and to select * from current_cus; I got a blank web page? I'm really trying to get it, but I think I'm missing something. Here is the code without my name and pw, Check it out.

<html>
<body>
<table width='450'>
<?php
$dbconnect = mysql_connect('localhost','','');
mysql_select_db('work_2004');
$query="SELECT * FROM current_cus";
$result = mysql_query($query, $dbconnect) or die(mysql_error());
while($row = mysql_fetch_assoc($result));
{
echo "<tr>";
echo "<td>$row[name]</td><td>$row[address]</td><td>$row[city]</td><td>$row[state]</td><td>$row[zip]</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>

ezmobius 11-29-2004 11:35 AM

Heya-
OK lets see if we can fix this for you. Actually getting a blank web page is good. It probably means that everything is working but the query is not yielding any results. So lets take a look at this line:
PHP Code:

echo "<td>$row[name]</td><td>$row[address]</td><td>$row[city]</td><td>$row[state]</td><td>$row[zip]</td>"

OK see all the calls to $row[city] or $row[state] ? $row[] is an ARRAY. When the script runs it queries the db and stores the temporary results in $row[]. This happens inside of a while loop. So every time it loops, it gets 1 row of results from the db. Then it starts over at the beggining of the loop. Until there are no more rows left in your result set. If you changed the db then you also need to change the 'INDEXES' of the $row[] array. It works like this: Say that I have 2 rows in a db table, 'id' & 'name' . To get those rows to echo to the web page I need to call them with the $row[] array and there index. The name of the column in the db table is the index so for our example you would need to do this:
echo "<td>$row[id]</td<td>$row[name]</td>
Do you follow me? You can use the script that got you a blank web page for a different db but you need to change the indexes inside of the $row[] array in order for it to work.
You also need to change the $query line. Since you changed to the db 'work_2004' then you need to change the name of the table in the SELECT statement. So if you tanle in 'work_2004 was called 'customer' then you would change the SELECT line to"
PHP Code:

$query "SELECT * FROM customer"

Replace customer with the name of the table you want to get info from.
Try this and get back to me!

fvs 11-29-2004 04:56 PM

Hi Ezra, I followed you exactly and it still wont open web page, Here it is, I'm missing something? Your very clear and I do understand the code now that you've explained it to me.

<html>
<body>
<table width='450'>
<?php
$dbconnect = mysql_connect('localhost','','');
mysql_select_db('work_2004');
$query="SELECT * FROM current_cus";

$result = mysql_query($query, $dbconnect) or die(mysql_error());
while($row = mysql_fetch_assoc($result));
{
echo "<tr>";
echo
"<td>$row[id]</td><td>$row[Customers_name]</td><td>$row[First_name]</td><td>$row[Last_name]</td><td>$row[address]</td><td>$row[city]</td><td>$row[state]</td><td>$row[zip]</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>

ezmobius 11-29-2004 05:56 PM

So it just loads an empty page? What happens when you view the source on that empty page?
Lets try to break it down and find out where the error is. Add something like this after every line of code so we can see where it makes it to.

PHP Code:

<html>
<body>
<table width='450'>
<?php
$dbconnect 
mysql_connect('localhost','','');
echo 
'made it here 1';
mysql_select_db('work_2004');
echo 
'made it here 2';
$query="SELECT * FROM current_cus";
echo 
'made it here 3';
$result mysql_query($query$dbconnect) or die(mysql_error());
echo 
'made it here 4';
while(
$row mysql_fetch_assoc($result));
{
echo 
"<tr>";
echo 
'made it here 5';
echo
"<td>$row[id]</td><td>$row[Customers_name]</td><td>$row[First_name]</td><td>$row[Last_name]</td><td>$row[address]</td><td>$row[city]</td><td>$row[state]</td><td>$row[zip]</td>";
echo 
'made it here 6';
echo 
"</tr>";
echo 
'made it here 7';
}
?>
</table>
</body>
</html>

Try that out and see where you make it to. Also make sure that the permissions are set correctly on your script. for now just do this in the terminal:
chmod 777 scriptname.php
If none of these things work lets try something else. get yourself a terminal window and do the following command:
mysqldump -u root -p work_2004 >> sqldump.txt
It will ask for your password and then exit. You will then be the proud owner of a backup of you work_2004 database. Post here again and attach the sqldump.txt text file so that I can load it in my database and run the script to see if it works on my end or not. If you don't want to post it up here then just email it to me at ez@yakima-herald.com
I can't think of much else that could be wrong here without being able to try it out myself, so send me the mysqldump and we'll get this puppy running for you!

fvs 11-29-2004 07:14 PM

Ezra that worked; What did I do wrong on other code? Where am I entering the wrong things to get mysql db into the web page?

made it here 1made it here 2made it here 3made it here 4made it here 5made it here 6made it here 7

Ezra, I also tried to enter mysqldump, check it out;

on:fvs % mysqldump -u fvs -p work_2004 >> sqldump.txt
Enter password:
Mon:fvs % mysql -u fvs -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 57 to server version: 4.0.15

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;
+-----------+
| Database |
+-----------+
| duckwear |
| mysql |
| sample |
| test |
| work_2004 |
+-----------+
5 rows in set (0.01 sec)

mysql>

ezmobius 11-29-2004 07:29 PM

So all that showed up on the web page was the made it here 1-7 ? Is there data in your database? It seems that if it is making it through all of the echoes then the only thing I can think of is that it is not SELECTing anything in the query. Open up your terminal and login to mysql with this line:
mysql -u root -p
Use root unless you are using a different username in your script. After it opens do this command:
SELECT * FROM current_cus;
Do you get a results table? If you do then I can't fiigure oput what the heck is going wrong. The only way I can go any further is to try it out on my end. So send me a mysqldump file of the database so I can see whats going wrong. Short of that, if we can't get it working then I will make a small test db and send you a mysqldump[ file of it and a script that works to make a web page of all the data in the db and have you load the data into your install of mysql and run the php script to see if it works the same on your end as it does on my end.

ezmobius 11-29-2004 07:36 PM

I didn't read your whole post before I replied. It looks like your mysqldump worked fine. Its not supposed to go into mysql mode, it just dumps a text file and then exits back to your normal command line. So there should now be a file called sqldump.txt in your home folder or whatever your current working directory was when you ran the mysqldump command. If you can send me that file I will be able to find out once and for all what the heck is going on...

fvs 11-30-2004 06:39 AM

Hello Ezra,
Here it is;

-- MySQL dump 9.10
--
-- Host: localhost Database: work_2004
-- ------------------------------------------------------
-- Server version 4.0.15

--
-- Table structure for table `current_cus`
--

CREATE TABLE current_cus (
id int(11) NOT NULL auto_increment,
Customers_Name char(35) default NULL,
First_name char(15) default NULL,
Last_Name char(20) default NULL,
Address char(30) default NULL,
City char(18) default NULL,
State char(5) default NULL,
Date date default NULL,
zip int(8) default NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;

--
-- Dumping data for table `current_cus`
"sqldump.txt" 30L, 821C

mysql> use work_2004;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from current_cus;
+----+-----------------+------------+-----------+----------------+------------+-------+------------+-------+
| id | Customers_Name | First_name | Last_Name | Address | City | State | Date | zip |
+----+-----------------+------------+-----------+----------------+------------+-------+------------+-------+
| 3 | Koch Tom | Tom | Koch | 128 Belview Rd | Philpsburg | NJ | 2004-11-20 | 8865 |
| 4 | Dallinger Frank | Frank | Dallinger | NULL | Palmer | Pa | NULL | 18045 |
+----+-----------------+------------+-----------+----------------+------------+-------+------------+-------+
2 rows in set (0.00 sec)

Sorry if I can't get back as soon as I like, I have to go in and out freqently, Thanks again. fvs

JayBee 11-30-2004 07:01 AM

Just a quickie - have you tried removing the ";" from the end of the "while" line in your code?

fvs 11-30-2004 10:13 AM

Hi Jay, I just removed ";" from the while line, It showed me only the [id] [First_name]
[zip] and nothing else? As you can see I have other columns that have more data that don't show? Thanks for helping. fvs

ezmobius 11-30-2004 12:24 PM

Hehe- Jay was right... Man sometimes it's the smallest things that get overlooked. And since I wasn't running the code on my end, just posting it I never noticed the ; after my while loop. So after much bumbling about here it is: And the reason why some of your fields were not displaying fvs is because mysql is case sensitive. So some of your fields were like Last_Name and you were calling them like this: Last_name so heres the final script... it works just fill in your name and pass.
PHP Code:

<html> 
<body> 
<table width='450' border='1'> 
<?php 
$dbconnect 
mysql_connect('localhost','root','reversal'); 
mysql_select_db('work_2004'); 
$query="SELECT * FROM `current_cus`"
$result mysql_query($query$dbconnect) or die(mysql_error());
while(
$row mysql_fetch_assoc($result))

echo 
"<tr>"
echo 
"<td>$row[id]</td><td>$row[Customers_Name]</td><td>$row[First_name]</td><td>$row[Last_Name]</td><td>$row[Address]</td><td>$row[City]</td><td>$row[State]</td><td>$row[Date]</td><td>$row[Zip]</td>";  
echo 
"</tr>"

?> 
</table> 
</body> 
</html>


fvs 11-30-2004 04:14 PM

Hello Ezra, That worked GREAT! and I want to THANK YOU very much for all your patience and tolerence, I'm on my way to getting started on the right foot, I'll practice practice and practice, Thanks once more. fvs

ezmobius 11-30-2004 04:24 PM

Hey your very welcome-
I've been helped out a lot by people on this and other forums in the past so I'm just giving back. Good luck, php is a great loanguage to learn programming concepts with especially if you are already a web designer. Just stick with it and you'll go far.

fvs 12-02-2004 07:37 AM

Ezra, I downloaded phpMyAdmin-2.2.7 and I thought it would help, but I don't know where to start, I got this message on my web page;

The $cfgPmaAbsoluteUri directive MUST be set in your configuration file!

Can this phpMyAdmin help me?

ezmobius 12-02-2004 11:15 AM

Yes, PHPmyAdmin is awesome. I don't recall having any trouble installing it myself. So maybe you need to start over and install it again. Do you have any instructions? If you stilll have trouble installing phpmyadmin then you should get a free program called yourSQL, google for it. I think you can get it at sourceforge or something. Anyway let me know if you need more help with phpmyadmin. I use it EVERY day myself. It's an indespensable tool if you use mysql. But yoursql works good too.

JayBee 12-02-2004 11:47 AM

Heh - I feel like a NINJA now :) Glad I could help!

btw, if you're looking for a quick bit of code to debug mysql connections, instead of doing the table rendering thing, just try this with your result set

Code:

while ( $row = mysql_fetch_assoc($result) ) {
    print_r($row);
}

This will look terrible in a browser, but view source and you'll see a nicely formatted note of each record. The advantage here is that this bit of code will work for whatever query you poll the database with - you don't have to remember to change column names, watch cases etc. The downside is that it's pretty much useless for production deployment :(


All times are GMT -5. The time now is 06:00 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.