RE: [PHP] Questions from a Newbie - Please Help
| From: | Ethan Rosenberg | Date: | Tue, 19 Oct 2010 16:46:30 +0000 |
| Subject: | RE: [PHP] Questions from a Newbie - Please Help | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-308845@lists.php.net to get a copy of this message | ||
Dear List -
I've checked the php.ini file [again] and cannot find any errors.
I wrote a PHP script to open a non-existent data base, and receive no error.
At this point, I am out of options.
Let's all look at the code, and tell me 1]where the error is and 2]any corrections or additions to the ini file.
For personal reasons, which I cannot explain in a public forum, I am under extreme pressure to learn PHP ASAP.
Thank you.
Ethan
+++++++
At 08:31 AM 10/19/2010, Tommy Pham wrote:
-----Original Message----- From: Ethan Rosenberg [mailto:ethros@earthlink.net] Sent: Tuesday, October 19, 2010 12:05 AM To: php-general@lists.php.net Subject: Re: [PHP] Questions from a Newbie Tamara - Thanks. No error_log. The error log only exists if he configures it properly and the script has error. IE: log_errors & error_log. Like I said, Ethan should start from the beginning of the manual. It covers the configuration of PHP in addition to the fundamentals of PHP. This works ... <html><body> <?php phpinfo(); ?> </body></html> Ethan ++++++++++ At 02:23 AM 10/19/2010, Tamara Temple wrote:On Oct 18, 2010, at 11:01 PM, Ethan Rosenberg wrote:Odd you should still get a blank screen and nothing in the error_log... Does phpinfo() work?I've added the code you suggest, and I still get a blank screen. Should I be explicitly be using mysqli functions; eg mysqli_connect?Ethan ++++++++ At 11:00 PM 10/18/2010, you wrote:I cannot get the following to work. In my Firefox [Iceweasel]Where do you set $host, $user and $password? You should add the following after the new mysqli statement: if ($mysqli->connect_error) { die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error); }Tamara Temple -- aka tamouse__ <mailto:tamara@tamaratemple.com>tamara@tamaratemple.com "May you never see a stranger's face in the mirror." On Oct 18, 2010, at 4:09 PM, Ethan Rosenberg wrote:At 05:37 PM 10/17/2010, Tamara Temple wrote:gah, i botched that up. For the first part, you want the following:$cxn = new mysql($host, $user, $password); $res = $cxn->query("create database test22:); if (!$res) { die("Failed to create database test22: " .$cxn- >error());}Then, reopen the connection with the new data base:$cxn = new mysql($host, $user, $password, "test22");Then the following code will work. Tamara Temple-- aka tamouse__<mailto:tamara@tamaratemple.com>tamara@tamaratemple.com "May you never see a stranger's face in the mirror." On Oct 17, 2010, at 4:26 PM, Tamara Temple wrote:On Oct 17, 2010, at 1:22 PM, Ethan Rosenberg wrote:At 01:41 AM 10/17/2010, Tommy Pham wrote:enter the following URL: [w/ the http]browser, IThe code contained in the file CreateNew.php is: /* * Create Database test22 */ <html><body> <?php $cxn = mysqli_connect("$host",$user,$password);Whenever you get a blank screen running a php application, the place to look is the http server's error_log. This is frequently found in / var/log/httpd/error_log or /var/log/apache2/error_log. (If your system is hosted someplace else, it could very easily be in a different place). Typically you need root permission to read this file. Tail the file after you run your PHP script to see the most recent errors.Better to use the OO approach:$cxn = new mysqli($host, $user, $password);echo "Create database test22;"Instead of echo statements (which would just echo the contents to the output, i.e., your browser, you want to assign them to a variable, such as: $sql = "create database test22; use test22"; Then you need to execute the sql statement:$res = $cxn->query($sql); if (!$res) { die("Could not create database test22: " .$cxn- >error());}
echo "Create table Names2
$sql = "create table Names2(
RecordNum Int(11) Primary Key Not null default=10000auto_increment,
FirstName varchar(10),
LastName varchar(10),
Height decimal(4,1),
Weight0 decimal(4,1),
BMI decimal(3,1)
Date0 date
);"
error());; // to close off the php statement $res = $cxn->query($sql); if (!$res) { die("Could not create table Names2: " . $cxn-
error());echo" Create table Visit2}($sql = "create table Visit2Indx Int(7) Primary Key Not null auto_increment, Weight decimal(4,1) not null, StudyDate date not null, RecordNum Int(11));"; // again, to close off the php statement $res = $cxn->query($sql); if (!$res) { die("Could not create table Visit2: " . $cxn-
?> </body></html>}$sql= "SHOW DATABASES";This doesn't work in a programmatic setting. Terminate the database connection:$cxn->close();
I would also like to be able to add data to a table, using
in MySQL as: load data infile '/home/ethan/Databases/tester21.dat.' replacePHP, which I can do
Names fields escaped by '\\' terminated by '\t' linesinto table
error());Tamara - Excellent!! Can you [or someone else] help w/ this .... I followed the instructions to create the database and the tables. No errors appear on the screen, which is also blank, and the error logfile was never written. I cannot find the database tester22 or the tables Names2 and Visit2. Where should I look, or did I make a mistake? Here is the code: /* * Create Database test22 */ <html><body> <?php-- PHP General Mailing List (http://www.php.net/ ) To unsubscribe, visit: http://www.php.net/unsub.phpThat's a specific feature of the mysql program. You'd have to write something in php to be able to parse the file and insert the data. There are examples all over the net. Then you would need to set up sql insert or replace statements to actually get the data into the data base using mysqli::query. There are numerous examples of this as well. Here's one example: <?phpterminated by '\n' ;$host = "localhost"; $user = "root"; $pwd = "rootpassword"; $db = "test22"; $table = "table_to_insert_into";$cxn = new mysql($host, $user, $pwd, $db);$filename = "tab-delimited.txt"; $contents = file($filename); // returns the contents ofthe file into an array, one line of file per array$columns = explode("\t", $contents[0]); // get the columnnames from the first line of the file$sql = "insert into $table set "; for ($i=1; $i<count($contents) ; $i++) { $data = explode("\t", $contents[$i]); $j = 0; foreach ($columns as $column) { $insertdata[] = "$column='" .$cxn- >real_escape_string($data[$j+ +]) . "'"; // this assumes the column names in the tsv file match the column names in your data base table exactly. It also assumes that all your data are strings, not numerics.} $sql .= implode(",",$insertdata); $res = $cxn->query($sql); if (!res) die ("Error inserting data: " .$cxn- >error());}?> <html><head><title>Imported data</title></head> <body> <p>Data just imported:</p> <table border="1" cellpadding="2px" cellspacing="2px"> <thead> <tr style="color: white; background-color: black; text-align: center"> <?$res = $cxn->query("select * from $table limit 1"); // getone row from table for generating column namesif (!res) die ("Query failed for table $table: " .$cxn- >error());$row = $res->fetch_assoc(); foreach ($row as $column => $value) { echo "<th>" . $column . "</th>"; }?> </tr> </thead> <tbody> <?$res = $cxn->query("select * from $table"); if (!res) die ("Query failed for table $table: " .$cxn- >error());while ($row = $res->fetch_assoc()) { echo "<tr>"; foreach ($row as $column => $value) { echo "<td>" . $value . "</td>"; } echo "</tr>\n"; }?> </tbody> </table> </body> </html>As I stated, I am a newbie. 1] I am trying to shorten the learning curve by asking some questions, which I understand are probably trivial. A whole MySQLi list of functions at this point is to much for me. I have to break the problem into manageable parts.Important, most used mysqli functions: Creating the database connection: $cxn = new mysqli( host, user, password, database ); (mysqli::__construct function) Submitting queries: $result = $cxn->query( sql ); (mysqli::query function) Working with results: $row = $result->fetch_assoc(); -- returns an associative array of a sql select statement result, subsequent calls return the next row in the result until the results are exhausted. Syntax errors: $cxn->error() -- reports the mysql error of the last query. These will get you started quite well.2] It has been my experience that using a GUI does not teach the whole subject. Linux, which is the OS I use cannot be run from a GUI. In the code being discussed, I wish to create a database and add two tables. I also note a MySQL statement that can be used to add data to an existing table, and wish to be able to execute this statement using PHP.mysql(1) is an interactive program that does a lot of things to help you interact with mysql databases. It's functions are not all duplicated in the programmatic interface to mysql(i) -- i.e. you have to write them yourself.So, therefore...... Let us try to answer the following two(2) questions: a] What changes [other than moving the simicolons] have to be made to correct the code.See aboveb] What books can you suggest to help w/ MySQL and PHP? I already have the SQL, MySQL & PHP, and HTML books in the ..... for Dummies series. I need something with a little more depth and detail.Personally, Dummies books don't provide the necessary information to actually become proficient at any particular topic. My personal favourite books are in the O'Reilly catalog, although some are dated: - Head First PHP and MySql: http://oreilly.com/catalog/9780596006303/ - PHP Cookbook: http://oreilly.com/catalog/9780596101015/ - Learning PHP, MySQL, and JavaScript: http://oreilly.com/catalog/9780596157135/ - MySQL Cookbook (oreilly.com is currently returning a server error on this title, unfortunately) Tamara{ $cxn = new mysql($host, $user, $password); $res = $cxn->query("create database test22"); if (!$res) { die("Failed to create database test22: " . $cxn-
error());}$cxn = new mysql($host, $user, $password, "test22"); $sql = "create table Names2"; $res = $cxn->query($sql); if (!$res) die("Could not create table Names2: " . $cxn-
error());$sql = "create table Visit"; $res = $cxn->query($sql); if (!$res)die("Could not create table Visit2: " . $cxn-
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php$cxn->close();?> </body></html> Thanks. Ethan