PDO: make $dbh available and maintain across all php files [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
seem that I can't make it right.
Basically, three PHP files are used: - login.php, testconnect.php and numrows.php
numrows.php is the main file that first start played.
login.php and testconnect.php are good.
numrows.php:-
<?php
global $dbh1;
require_once "testconnect.php";
try
{
$stmt = $dbh1->prepare("select count(distinct mfg_code) from test");
$stmt->execute();
}
catch(PDOException $err)
{
$alertmsg = $err->getMessage();
}
$num = $stmt->fetch("PDO::FETCH_ASSOC:");
$num = json_encode($num);
echo $num;
?>
The log error from apache showed ""GET /testnumcards.php HTTP/1.1" 500 -". Again the error I encountered while debugging is "NetworkError: 500 Internal Server Error".
What is the right way to do?

Your problem not in making $dbh available but in inconsistent code and wrong syntax.
At least make your file this way, without all the useless and wrong code
<?php
require_once "testconnect.php";
$stmt = $dbh1->prepare("select count(distinct mfg_code) from test");
$stmt->execute();
$num = $stmt->fetch(PDO::FETCH_ASSOC); // note the proper syntax
$num = json_encode($num);
echo $num;
then look into error_log for the details

Related

how do i fix this error PHP log in error user not found [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 days ago.
Improve this question
Im new to php and im building a login and registration form.
Everything works except when i click log in with a user credentials that are in my database my error for "user not found is showing.
I've included the code snippet for my error to see if i have typed something wrong.
protected function getUser($email, $pwd)
{
$stmt = $this->connect()->prepare("SELECT pwd FROM web WHERE lName = ? OR Email = ?;");
if (!$stmt->execute(array($email, $pwd))) {
$stmt = null;
header("location: ../index.php?error=stmtfailed");
exit();
}
if ($stmt->rowCount() == 0) {
$stmt = null;
header("location: ../index.php?error=usernotfound");
exit();
}
$pwdHashed = $stmt->fetchAll(PDO::FETCH_ASSOC);
$checkPwd = password_verify($pwd, $pwdHashed[0]["pwd"]);
if you need any more info let me know!

Same connection file works for 1 script but not the other [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a directory with 2 php files in it. One is using an old way of querying a DB and the other can't even tell me if the connection exists.
the connection file: (I've left out the actual credentials for obvious reasons)
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_student_dirR = "some_hostname";
$database_student_dirR = "some_db_name";
$username_student_dirR = "some_username";
$password_student_dirR = "some_password";
$student_dirR = new mysqli($hostname_student_dirR, $username_student_dirR, $password_student_dirR, $database_student_dirR) or trigger_error(mysqli_error(),E_USER_ERROR);
?>
file that successfully connects:
<?php require_once('../Connections/student_diriR.php'); ?>
<?php
mysqli_select_db($student_dirR, $database_student_dirR);
$query_rs_get = "SELECT some_column FROM directory";
$rs_get = mysqli_query($student_dirR, $query_rs_get) or die(mysqli_error($student_dirR));
$row_rs_get = mysqli_fetch_assoc($rs_get);
$totalRows_rs_get = mysqli_num_rows($rs_get);
?>
file that does NOT connect: (in the exact same directory as the file that does connect, so that path can't be the issue)
<?php require_once("../Connections/student_dirR.php"); ?>
<?php
if($student_dirR) echo "we're connected!";
else echo "we're NOT connected!"; exit;
?>
The error message I get says "Fatal error: Uncaught Error: Call to undefined function mysql_connect() in Connections/student_dirR.php:9"
Line 9 uses new mysqli() not mysql_connect(), so I don't know what that's talking about.
See you have the spelling mistake in your directory name student_diriR.php and
student_driR.php that's why the error says Call to undefined function mysql_connect().
see the difference below
file that successfully connects:
<?php require_once('../Connections/student_diriR.php'); ?>
file that does NOT connect:
<?php require_once("../Connections/student_dirR.php"); ?>

site visit counter gives no output [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a website which logs the ip,time and date of every visit. I am trying to write a function that displays how many times a certain ip address has accessed the site. It will say something like you have visited this site .... times.
Here is the php:
<?php
function visitor_counter()
{
require_once 'php_scripts/log_in_script_2.php';
$conn = new mysqli($hn,$un,$pw,$db);
if ($conn->connect_error) die($conn->connect_error);
$ip_var = $_SERVER['REMOTE_ADDR'];
$query = "SELECT COUNT(ip) FROM visitors WHERE ip = '$ip_var'";
$result = $conn1->query($query);
if (!$result) die ($conn->error);
echo $result;
mysqli_close();
}
?>
it is included in the webpage with
<?php require_once "php_scripts/visit_counter.php"; ?>
and the function is called in the code with
You have visited my site: <?php visitor_counter(); ?> times.
It does not output anything, and the rest of the webpage doesn't load after this line. I know the query is correct, I've tested it in phpmyadmin, and I know the login for the function has the correct privilages. Any ideas?
I added the changes from the comments above, but a problem was still there. The problem was that I was trying to echo the query result directly. I changed the query to $query = "select count(ip) as total_visits from visitors where ip = '$ip_var'"; and changed the echo to echo $result->fetch_object()->total_visits;, and it works.

Error with mysql_select_db [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I use Linux Debian
Local Lamp Server
i tried to make connection between Database and php :
<?php
$tf_host = 'localhost' ;
$tf_dbname = 'tinyforum' ;
$tf_username = 'root';
$tf_password = '' ;
$tf_handle = mysql_connect($tf_host , $tf_username , $tf_password);
if (!$tf_handle)
die('connection problem ..');
$tf_db_result = mysql_select_db($tf_dbname);
if($tf_db_result)
{
mysql_close($tf_handle);
die('selection problem');
}
die('OK');
mysql_close($tf_handle);
?>
The result :
http://www.foxpic.com/V0HrSdgb.png
pic of phpmyadmin:
http://www.foxpic.com/V0AlRhi6.png
the place where i save the php file :
/var/www/html/
Unless you copied your code incorrectly, your check is not going to result in what you want.
$tf_db_result = mysql_select_db($tf_dbname);
if($tf_db_result)
your selection seems fine so either you should change your code to
if(!$tf_db_result) //note the !
or restructure your code
if($tf_db_result) {
//Do the stuff you want to do when you are connected
} else {
//Die connection
}

Create website with a page to show online users including username and picture [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am in the stages of the class diagram, I was creating the class diagram for a website I am planning to create. It was going fine until I reached the stage of wanting to have a web page that displayed the online users e.g. showing their username and profile picture of all online users. I am not sure on how I would do this, the image is of what I have so far. I would appreciate any help or guidance.
Here is my current class diagram http://imgur.com/sgjJwkc
You could also set up a column for user's status (logged in, logged out) and make it toggle between 0 (logged out) and 1 (logged in). You could update this information every 5 seconds (in the background of course) using an AJAX call. Something like this:
//JAVASCRIPT
<script>
$(document).ready(function() {
setInterval(function() {
$.post('Path To PHP File', {x : Pass Variables, y: If You Want}, function(res)
//Do something with the result (res)
);
}, 5000);
});
</script>
//PHP FILE
<?php
//If you passed any variables to the script:
$x = $_POST['x'];
$y = $_POST['y'];
//Connect to your database
$dbConn = "I hope you're using PDO for this.";
//Create your query
$sql = "SELECT * FROM users WHERE status=1";
$res = $dbConn->prepare($sql);
$res->execute();
//Return/echo results
foreach($res as $x) {
echo "<div id='useTheIdToStyleTheResults'>".$x['name']."</div>";
}
?>
res is whatever your php script returns. You can simply run an SQL query on your database in that script to get all users who are logged in and use a foreach() loop to return each item as an html div element. Style those elements to your liking and there you go. If you have questions, just ask!
EDIT:
After reading a little more of your question, SQL JOIN and UNION are a couple of concepts you might want to look into.
http://www.w3schools.com/sql/sql_join.asp
EDIT #2:
//Define Variables
$hostname = '127.0.0.1';
$username = 'userName';
$password = 'passWord';
$dbname = 'database in use';
//Create Connection
try {
$con = new PDO("mysql:host=$hostname;dbname=$dbname",$username,$password);
$con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//echo "Connected to database"; //Uncomment statement to the left to check for connection
} catch (PDOException $e) {
print "Unable to connect: " . $e->getMessage();
mysql_close($con);
die();
}
?>
I would update a timestamp in the user's row every time they load a page and on the load of the online users I would check for timestamps that are fairly recent.
Pseudocode:
SELECT username,avatar FROM users WHERE last_active >= time()-900;

Categories