I am using LAMP stack to develop a PHP application. I am using require_once to include class files. I need to use the functions in those class files in more than one PHP page. So, I am including those class files in all the required PHP pages using require_once. But, if I include those class files in more than one page, the PHP file goes blank. It displays nothing. View source also displays nothing.
Files: test.php, process.php and class.test.php
test.php has
<?php
session_start();
require_once 'classes/class.test.php';
.
Few more classes
.
.
?>
<html>
<form name = "myForm" method="POST" action="process.php">
<input type = "text" name="username" value=""/>
<input type = "submit" value="Submit" />
</form>
</html>
process.php
<?php
session_start();
require_once 'classes/class.test.php';
$obj_test = new test();
$obj_test->test();
?>
class.test.php
<?php
session_start();
require_once 'class.misc.php';
require_once 'config.php'; //DB connection details
function test()
{
$obj_misc = new misc();
$id = $obj_misc->random_ID();
$username = $_POST['username'];
$query = "INSERT INTO test_table VALUES ('$id','$username',NOW());
mysql_query($query);
}
?>
Now, it returns a blank page. If I comment out the require_once in process.php, the test.php page displays the form, but on submitting the form the process.php throws an error "class test not found".
I am struggling with this problem for the past 2 weeks. :( It was working fine before that. I don't understand what went wrong. Please help.
You have an error in the PHP code for process.php; you are missing a semicolon:
require_once 'classes/class.test.php'
should be:
require_once 'classes/class.test.php';
If that doesn't fix it, then there is probably some other error somewhere in your code. Without access to the full source, we won't be able to do much.
For future reference, if a page goes blank, there is usually a problem with the PHP source code (ie, some type of interpreting error). As part of good debugging tactics, look into display_errors and error_reporting
Try out with
error_reporting(E_ALL);
Sounds like your error reporting is turned off. You should check your error logs to see what exception is being thrown when it's failing silently (that will give you a little more insight).
Additionally, you may want to add this at the top of your process script:
error_reporting(E_ALL);
Require once is a function ...
try adding the parens ...
require_once( 'classes/class.test.php' );
looks like you are missing a closing qoute on the sql query
$query = "INSERT INTO test_table VALUES ('$id','$username',NOW());
mysql_query($query);
should be
$query = "INSERT INTO test_table VALUES ('$id','$username',NOW())";
mysql_query($query);
Related
This might seem stupid, but I have this simple php code, and it is not echoeing the message to the screen, neither is redirecting the page. I am not sure why. I tested the db connection and it is working, also the user and password used for test exist in the db (of course info changed here for security). What am I doing wrong? What am I missing?
<?php
session_start();
include_once("C:/webroot/connect.php");
if (isset($_POST['submit'])){
//$user=$_POST["httpd_username"];
$user="usernameXYZ";
//$pass=$_POST["httpd_password"];
$pass="passXYZ";
$query= "SELECT * FROM regtrack_users WHERE user_name='$user' and password='$pass'";
$result =pg_query($query) or die ("Unable to connect to db");
$numrows=(pg_num_rows($result));
if($numrows>0){
$row=pg_fetch_assoc($result);
$dbuser=$row['user_name'];
$dbpass=$row['pass'];
echo "$dbuser and $dbpass";
}
header("Location:login.php");
}
?>
It might because of this part:
if($numrows==1){
$row=pg_fetch_assoc($result);
$dbuser=$row['user_name'];
$dbpass=$row['pass'];
echo "$dbuser and $dbpass";
}
You should check if the $numrows variable really contains 1, or you should try $numrows>0 instead of $numrows==1
Root of the problem lies in 3rd line, you are providing a path to a file to include in wrong way. Read here about how properly you should be providing paths to files in php code:
http://yagudaev.com/posts/resolving-php-relative-path-problem/
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
require_once('dbConnect.php');
$result = mysql_query("SELECT count(*) from TableName;");
echo mysql_result($result, 0);
mysqli_close($con);
}
?>
In the above code sample, I can see that require_once is using older values of the file "dbConnect.php". The login credentials present in dbConnect.php is of older values, and not the latest one I updated recently. I can see this in the error message.
Please suggest how to overcome this error.
The code below was not working:
public function __construct($thread_id)
{
require_once('../private/mysqli_connect.php');
require_once('../php_classes/class_message.php');
$this->thread_id=$thread_id;
$q="SELECT *
FROM message_thread_name
WHERE thread_id=2";
if($r=#mysqli_query($dbc, $q))
{
while($row=mysqli_fetch_array($r, MYSQLI_ASSOC))
{
$this->poster=$row['name'];
$this->subject=$row['thread_subject'];
}
}
$qm="SELECT message_id
FROM message
WHERE thread_id=$this->thread_id";
if($rm=#mysqli_query($dbc, $rm))
{
while($rowm=mysqli_fetch_array($rm, MYSQLI_ASSOC))
{
$message=new message($rowm['message_id']);
array_push($this->messages, $message);
}
}
}
I ran out of ideas for fixing it, so I changed require_once('../private/mysqli_connect.php'); to require('../private/mysqli_connect.php');, and much to my surprise, it worked. Any ideas as to why that may be?
Thanks.
When you use require_once PHP will check if the file has already been included, and if so, not include (require) it again. You can read it here.
So whats the problem here?
From your problem statement, I am assuming you have created another instance of this class before, or you have already included mysqli_connect.php somewhere else. And in your mysqli_connect.php you have created the connection link something like.
$dbc= mysqli_connect( ... );
So when you using require_once as the mysqli_connect.php is not loading again, your $dbc variable remain undefined. and your query does not work. But when you use require instead of require_once the file loaded again, and new connection created again, and your code works.
NB: Though changing to require, solved your problem, you should not use it like this. It will create new connection to your database, each time you create new instance of your class. For your thought: you can try creating something like this
I have created a game in using impact game engine called Staroids.
I have created a database to store the highscores.
I have done what it says on this previous answer and looked at a few other solutions, but it doesn't seem to work!
I am trying to run a php file which contains the following code:
<?php
$connect = mysql_connect('localhost', 'root', 'password');
mysql_select_db('staroids', $connect);
$sql = 'INSERT INTO staroids (score) VALUES ("'.$_POST['score'].'")';
$result = mysql_query($sql);
?>
Here is the code that I run in the JavaScript file:
$.post('scripts/register.php', {score: score}, function(){}).error(function(){
alert('error... ohh no!');
});
It comes up with the following error in console when it reaches this code:
Uncaught ReferenceError: $ is not defined
You're probably not loading jQuery into your page correctly. Make sure you have the script tag in the head that loads jQuery and troubleshoot to make sure it's actually loading jQuery at all.
I solved it with the help from the comments above and using a lot of trial and error, here is the working code:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
//make sure you run this otherwise the $post request wont work!
Then where in the JavaScript function where I wanted to run the code I put:
$.post('register.php', {score: this.score}, function(){}).error(function(){
alert('error... ohh no!');
});
//this runs the php file parsing through the score variable
Here is the PHP code which I then run to add the parsed through variable to the table in the database:
<?php
$score = $_POST['score'];
mysql_connect("localhost", "root", "password");
mysql_select_db("staroids");
mysql_query("INSERT INTO scores (score) VALUES ('$score')");
?>
I am trying out sqlite with php. On the server when I execute the command sqlite3, it shows the version and help option suggesting that sqlite is installed there. My folder structure is like this: /username/public_html/
Now I created a db called "chatuser.db" with a table "Users" and two columns "Username" and "Password" using sqlite3 command at /username
I then copied the db using WinSCP to my windows folder and then copied it back to the server here: /username/public_html/ (I know it is not a good idea to keep db file in public_html, but am just trying out an example). Now I have the following php file:
add.php:
<?php
$password = $_POST['password'];
$username = $_POST['username'];
$name_es = sqlite_escape_string($username);
$password_es = sqlite_escape_string($password);
if (!empty($username)) {
$dbhandle = sqlite_open('chatuser.db', 0666, $error);
if (!$dbhandle) die ($error);
$stm = "INSERT INTO Users(Username, Password) VALUES('$name_es', '$password_es')";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok) die("Error: $error");
else echo "Success";
}
?>
index.html:
<html>
<head>
<title>Login Trial</title>
</head>
<body style="font-size:12;font-family:verdana">
<form action="add.php" method="post">
<p>
Name: <input type="text" name="username"><br>
Password: <input type="text" name="password"><br><br>
</p>
<p>
<input type="submit">
</p>
</form>
</body>
</html>
On clicking the submit button, it loads add.php, but does not show me the success message nor any error message. I am not able to figure out why. As an added note, I have done chmod 777 of chatuser.db as well. This is my first introduction to sqlite, any help would be great. Thanks
[SOLVED]
Found the issue, it was version mismatch. sqlite_open will not work. Alternative from this solution:
Error: file is encrypted or is not a database
My guess would be that sqlite_open triggers a fatal error and your PHP isn't configured to show them.
Try placing one echo before the sqlite_open and one after - if only the first one is echo'ed, you know what the issue is.
You will probably want to load the sqlite3 module in PHP.
-edit-
Actually, that only applies to PHP 4, which you're probably not using anymore. Either way, make sure that your PHP outputs errors (go into php.ini and set display_errors to On and error_reporting to E_ALL).