|
JingHan Liu - 2016-10-02 13:14:03
An apology for my bad English!
I'm new in PHP, and I saw your "Cursor" class, I think it's useful,
but when I setup the 4 'define' statements, and use:
include 'cursor.php';
$cursor = new Cursor;
$result = $cursor->select("users");
foreach($result as $user){
echo $user->Name.",".$user->Tel.",".$user->Cell.",".$user->email."<br>";
}
$cursor->close();
It can not work! (I have a "users" table in my database)
Could you please give a simple sample, How to use your class,
Thank you very much!
Best Regards
JingHan
Ore Richard Muyiwa - 2016-10-07 17:18:46 - In reply to message 1 from JingHan Liu
Your code is correct but note that Cursor returns an object which is case-sensitive. Please code indicates to me that "Name", "Tel", "Cell" may have been "name", "tel", "cell" or email is "Email".
Cross check to make sure you are referencing your database columns correctly.
Please let me know if you need further assistance or send a copy of your database schema.
JingHan Liu - 2016-10-08 04:12:43 - In reply to message 2 from Ore Richard Muyiwa
Dear Sir,
thank you very much for the reply.
I checked my code, the column name is exactly the same as the database definition,
and I noticed that you have Changed file of class, and I use the new version of cursor.php,
but still not work!
the code stop execution at the line: $cursor = new Cursor;
the 'users' table is very simple, defined as follows:
CREATE TABLE IF NOT EXISTS `users` (
`uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(10) NOT NULL,
`Sect` varchar(10) DEFAULT NULL,
`Tel` varchar(20) DEFAULT NULL,
`Cell` varchar(20) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`Pass` varchar(32) DEFAULT NULL,
`Title` varchar(10) DEFAULT NULL,
PRIMARY KEY (`uid`),
KEY `Name` (`Name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
another question, where to put the 4 'define' statements?
in the cursor.php or in my test code?
Thanks again.
Best Regards
JingHan
Ore Richard Muyiwa - 2016-10-08 22:42:13 - In reply to message 3 from JingHan Liu
I see. You seem new to using PHP Classes that contains definitions.
The four define statements above must be filled in with details to enable connection to your database.
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_NAME", "");
define("DB_PASS", "");
By default, apache/ngix server host name is "localhost" (I don't know much about others), and my database (mysql, mariadb etc.) user name is "root" and I left the password blank.
Example:
My database name is "MysqlDb" and my database username is "root" (default) and say for example I had the set the password for my privileged user as "richard", I will have the following.
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_NAME", "MysqlDb");
define("DB_PASS", "richard");
In few words, just replace the empty strings with your database authentication setup.
JingHan Liu - 2016-10-09 06:22:37 - In reply to message 4 from Ore Richard Muyiwa
Thank you so much for your kindly directions,
Yes, I am the first time using PHP Classes that contains definitions.
I had followed your instruction to fill in the four define statements (in cursor.php) with details,
but the code still stop execution at the line: $cursor = new Cursor;
if I use the following code:
try { $pdo = new PDO('mysql:host=localhost;dbname=mydbname;charset=utf8', 'myusername', 'mypassword');}
catch (PDOException $e)
{ echo 'Unable to connect to the database server - ' . $e->getMessage(); exit(); }
$sql = "select * from users";
$result = $pdo->query($sql);
foreach($result as $user){
echo $user[Name].",".$user[Tel].",".$user[Cell].",".$user[email]."<br>";
}
the above code returns just fine, but if I use exactly the same 'mydbname', 'myusername' and 'mypassword' in the four define statements,
the code stop execution at the line: $cursor = new Cursor;
I got stuck here, do you have any idea?
I am so sorry to bothering you again and again, and appreciate your patient, thank you
Best Regards
JingHan
Ore Richard Muyiwa - 2016-10-09 22:03:11 - In reply to message 5 from JingHan Liu
Kindly show me a copy of your define lines. I mean the four line of define statement with a dummy mydatabase, myusername, mypassword. You shouldn't be facing any problems.
For a quick debug of what the error might be (This is mysql error not php error), do this:
Under the line:
$this->errors[] .= $e->getMessage();
Add this:
echo $e->getMessage();
This will display the specific mysql error you are getting.
Let me know what error you are getting. Don't forget to remove the new line once you have a noted the error. Keeping it will not help in production builds.
JingHan Liu - 2016-10-10 04:36:17 - In reply to message 6 from Ore Richard Muyiwa
Dear Sir,
Thanks for your reply.
I have add the line: echo $e->getMessage();
under the line: $this->errors[] .= $e->getMessage();
but nothing displayed!
The four define statements are as follows:
define("DB_HOST", "localhost");
define("DB_NAME", "mydbname");
define("DB_USER", "myusername");
define("DB_PASS", "mypassword");
The following is my test code:
echo "0";
include '../cursor.php';
echo "1";
$cursor = new Cursor;
echo "2";
$result = $cursor->select("users");
echo "3";
foreach($result as $user){
echo $user->Name.",".$user->Tel.",".$user->Cell.",".$user->email."<br>";
}
echo "4";
$cursor->close();
echo "5";
it only displays [01] in the browser, [2] never show up!
BTW, if I use [include 'cursor.php';], it only display [0],
if I use [include '../cursor.php';], it displays [01]
and the added line [echo $e->getMessage();] done nothing!
thank you very much for your time
Best Regards
JingHan
JingHan Liu - 2016-10-10 04:51:45 - In reply to message 7 from JingHan Liu
Dear Sir,
one supplementary explanation for the previous message:
the cursor.php is locate in the same folder as my test code
Best Regards
JingHan
Ore Richard Muyiwa - 2016-10-13 15:21:06 - In reply to message 8 from JingHan Liu
I see where the problem lies. Your server may be set to passive include files.
Try using require or require_once instead of include.
let me know if it still doesn't solve your problem.
JingHan Liu - 2016-10-14 03:02:20 - In reply to message 9 from Ore Richard Muyiwa
Dear Sir,
I used require or require_once instead of include, but this time it displays only "0", no matter "cursor.php" or "../cursor.php".
I think the "Cursor" class is excellent, and it meet my need, so I am very eagerly to use this class,
but I do'nt know what the problem is!
The following is my real test code with real username/password: (tstcursor.php)
<?php
try { $pdo = new PDO('mysql:host=localhost;dbname=jinghani_boo;charset=utf8', 'jinghani_testboo', 'pass4boo');}
catch (PDOException $e)
{ echo 'Unable to connect to the database server - ' . $e->getMessage(); exit(); }
$sql = "select * from users";
$result = $pdo->query($sql);
foreach($result as $user){
echo $user[Name].",".$user[Tel].",".$user[Cell].",".$user[email]."<br>";
}
echo "0";
require 'cursor.php';
echo "1";
$cursor = new Cursor;
echo "2";
$result = $cursor->select("users");
echo "3";
foreach($result as $user){
echo $user->Name.",".$user->Tel.",".$user->Cell.",".$user->email."<br>";
}
echo "4";
$cursor->close();
echo "5";
?>
The four define statements are as follows:
define("DB_HOST", "localhost");
define("DB_NAME", "jinghani_boo");
define("DB_USER", "jinghani_testboo");
define("DB_PASS", "pass4boo");
and the url is:
jinghan.info/tst/tstcursor.php
Plaease help, Thank you so much.
Best Regards
JingHan
|