please ignore that it is old code im am testing
Having an issue where by I am unable to show specific user data from my db yet I can show the row of data. I can show the username using <?php echo htmlentities($_SESSION['username']); ?>but if I try and throw out email for example it dosnt like it. but if I do this....
<?php
$con = mysql_connect("localhost","***","***");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dmh", $con);
$result = mysql_query("SELECT * FROM members");
while($_SESSION = mysql_fetch_array($result))
{
echo $_SESSION['email'];
}
mysql_close($con);
?>
Like I say it chucks up the row - in regards to the above mention of pulling data from the DB via <?php echo htmlentities($_SESSION['username']); ?> this is handled by pulling in by db_connect.php file and also my functions file.
Try the following, unsure if that's the result you wish to get.
Using mysqli_* functions instead of mysql_*.
<?php
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_NAME', 'xxx');
$mysqli = #mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("could not connect");
$result = mysqli_query($mysqli,"SELECT * FROM members ");
session_start();
while($row = mysqli_fetch_array($result))
{
$_SESSION['email']=$row['email'];
}
echo $_SESSION['email'];
echo "<hr>";
echo "TEST";
echo "<hr>";
var_dump($_SESSION['email']);
mysqli_close($mysqli);
?>
I wouldn't access the $_SESSION like that. You could try:
$result = mysql_query("SELECT * FROM members");
while($meh = mysql_fetch_array($result))
{
$_SESSION['email']=$meh['email'];
echo $meh['email'];
}
That way your other session variables don't get overridden when you go to this page. The only other thing that I can suggest is trying print_r($meh); to see what data is actually getting put in the array. If it's attaching to numbers, there's a high chance you need to access it like echo $meh[0].
Related
When I was trying to make website for school project then I make a registration page so I make a html page and a php and a database.
But when I tried to enter anything in form then the result after submitting the information is blank page. When I opened php file in browser then a blank page opened. There should be either connected to the database or failed to connect to database. My coding of php file is given below:
Please help me as less time is remaining for last date of submission.
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'practice');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db = mysql_select_db(DB_NAME, $con) or die("Failed to connect to MySQL: " . mysql_error());
function NewUser()
{
$fullname = $_POST['name'];
$userName = $_POST['user'];
$email = $_POST['email'];
$password = $_POST['pass'];
$query = "INSERT INTO websiteusers(fullname,userName,email,pass) VALUES ('$fullname','$userName','$email','$password')";
$data = mysql_query($query) or die(mysql_error());
if ($data) {
echo "YOUR REGISTRATION IS COMPLETED...";
}
}
function SignUp()
{
if (!empty($_POST['user'])) //checking the 'user' name which is from Sign-Up.html, is it empty or have some text
{
$query = mysql_query("SELECT * FROM websiteusers WHERE userName='$_POST[user]' AND pass = '$_POST[pass]'") or die(mysql_error());
if (!$row = mysql_fetch_array($query) or die(mysql_error())) {
newuser();
} else {
echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
}
}
}
if (isset($_POST['submit'])) {
SignUp();
}
?>
MySQL is depreciated. Use MySQli instead
Connect to your database this way
$connect = mysqli_query(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
then check this way
if($connect){
echo "Connected";
}else{
echo "Not connected";
}
and any query you run should be in this format
$query = mysqli_query($connect,$Sql);
I tried many source codes from google but I am unsuccessful everytime. Everytime I clicked on submit button it shows a blank page. Please provide me help I want to create a signup page and in this I want to connect html coding to mysql database I have knowledge about html. So please provide an easy way to interconnect the database and html.
I also came to know about the php so I tried to copy source code from google but everytime I failed as php preview in browser is blank page.
I have a MySQL database on my local machine and am trying to query it with PHP.
I've written out the following HTML/PHP code. When I open this HTML file in Chrome, this is what is displayed. Why does the browser treat this code as text?
ID Project Code'; while ($row = mysqli_fetch_array($response)){ echo '' . $row[Id] . '' . $row[Project_Code] . ''; echo ''; } echo ''; } else { echo "couldn't connect do it"; echo mysqli_error($dbc); } mysqli_close($dbc); ?>
code:
<HTML>
<body>
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'root');
DEFINE ('DB_HOST', 'my_local_ip');
DEFINE ('DB_NAME', 'my_db');
$dbc = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("couldn't connect");
$query = "SELECT Id, Project_Code FROM database.projects";
$response = #mysqli_query($dbc, $query);
if ($response){
echo '<table>
<td>ID</TD>
<td>Project Code</TD></table>';
while ($row = mysqli_fetch_array($response)){
echo '<tr><td>' .
$row[Id] . '</td></td>' .
$row[Project_Code] . '</td></td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "couldn't connect do it";
echo mysqli_error($dbc);
}
mysqli_close($dbc);
?>
</BODY>
</HTML>
It is Because you are not running your code with apache/php/mysql.
If you are using windows package like wamp put it in www directory and access it through localhost, not directly open it in the browser, otherwise browser will treat it like plain html.
i've opened it as you were opening in chrome:
You should access your script using localhost and make sure your wamp (or whatever server package your are using) should be running.
PHP unlike javascript/HTML is a server side language. It runs on the server and outputs the generated content to the browser.
So for example
<?php
if(empty($a)) {
echo ':)';
} else {
echo ':(';
}
?>
The only part that makes it to the browser would be :).
The issues you are running into are
a) your file is .html. PHP won't run as such unless your server configuration is modified to do so.
b) your files are being executed on your local machine which doesn't have PHP installed.
Solutions:
a) Install PHP on your local machine; http://php.net/manual/en/install.php
b) Move to a hosted server (this might be easier since they will have support for configuration issues)
(lettering unrelated to issues lettering)
To be more specific your whole PHP code is outputted to the browser but the
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'root');
DEFINE ('DB_HOST', 'my_local_ip');
DEFINE ('DB_NAME', 'my_db');
$dbc = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("couldn't connect");
$query = "SELECT Id, Project_Code FROM database.projects";
$response = #mysqli_query($dbc, $query);
if ($response){
echo '<table>
Is all read as one element <?php until the > of the <table>.
Also while learning try to never use the #. That suppresses errors and those might (95% of the time, will) be useful to you.
After you installed Apache,MySQL and PHP
(You can try XAMPP or WAMP as mentioned in other answers.)
you can test this code:
(For XAMPP put your code in xampp\htdocs folder ,
name like something.php
to run. browse to localhost/something.php)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'database');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("couldn't connect");
$query = "SELECT Id, Project_Code FROM database.projects";
$response = mysqli_query($dbc, $query);
?>
<?php
if ($response){
echo '<table>
<td>ID</td>
<td>Project Code</td>';
while ($row = mysqli_fetch_array($response)){
echo '<tr>'.'<td>'.$row['Id'].'</td><td>'.$row['Project_Code'].'</td></tr>';
}
echo '</table>';
} else {
echo "couldn't connect do it";
echo mysqli_error($dbc);
}
mysqli_close($dbc);
?>
</BODY>
</HTML>
Im trying to create a login for my website and i need to store emails, usernames, passwords, ect in a database i have created already using phpMyAdmin. I have gone through article after article and nothing seems to be working. i have my connect.php like this:
<?
$hostname = "localhost";
$username = "username";
$password = "password";
$databaseName = "_mySiteUserDataBase";
mysql_connect($hostname, $username, $password) or die("Cannot connect to server");
mysql_select_db($databaseName) or die("Cannot select database");
?>
And my main.php like this:
<?
include("connect.php");
$tableName = "myUsers";
$sql = "SELECT * FROM $tableName";
$result = mysql_query($sql);
?>
And i have created a simple form in my html like this:
<html>
<head></head>
<body>
<form>
<input type = "submit" action = "main.php" method = "post" value = "Login">
</form>
</body>
</html>
After submitting the form it says cannot connect to server. I am new to php and mysql and i dont understand what each parameter in the mysql_connect is, and i dont know what they do therefore im not sure what im supposed to enter in but everyone i keep reading about seems to be inputing random values? I could use a brief explanation on that, because i am stuck at connecting and cant even get past this point sadly enough. Also i have been reading that mysql_connect is deprecated and isnt valid anymore but i dont understand what im supposed to use as an alternative. I know its mysqli but thats it and im unclear of the syntax.
mysqli:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
echo "start<br/>";
try {
$mysqli= new mysqli('localhost', 'myusername', 'mypassword', 'dbname');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
echo "I am connected and feel happy.<br/>";
$mysqli->close();
} catch (mysqli_sql_exception $e) {
throw $e;
}
?>
If you need to know how to create users, what the heck the hostname is, how to grant access (often useful after the connect :>), just ask.
Try this code in 'connect.php'
<?php
error_reporting(0);
$con=mysql_connect('localhost','root','');// here 'root' is your username and "" is password
if(!$con)
{
echo 'not connect';die;
}
mysql_select_db('dbname',$con);// here 'dbname' is your database name
?>
And also try following code to include sql connection in your other php file(main.php)
<?php
include 'connect.php';
$sql = "SELECT * FROM myUsers";
$result=mysql_query($sql);
?>
Let me convert it to mysqli for you and maybe that will fix the problem. Also, make sure the username, password, and database name are correct.
Try this code. At very least, it will provide a better error message for debugging.
<?
$hostname = "localhost";
$username = "username";
$password = "password";
$databaseName = "_mySiteUserDataBase";
$con = mysqli_connect($hostname, $username, $password, $databaseName) or die(mysqli_error($con));
?>
Main.php
<?
include("connect.php");
$tableName = "myUsers";
$sql = "SELECT * FROM $tableName";
$result = mysqli_query($con,$sql);
?>
I've just started learning php and I'm having trouble finding the problem when I get it to connect to the database and run an 'INSERT' query. I think the problem is in my connection.php file.
insert.php:
<?PHP
echo "into";
include("connection.php");
echo "connected";
$table = blog;
$sql = "INSERT INTO $table SET
Title = '$_POST[title]',
Description = '$_POST[description]',
Content = '$_POST[content]'";
$query = #mysql_query($sql);
echo "complete";
echo $_POST[title];
echo $_POST[description];
echo $_POST[content];
?>
connection.php:
<?php
echo "connection file reached";
$conn = mysql_connect(localhost, user, pass);
mysql_select_db(test, $conn);
echo "connected";
?>
Any advice on my code, or how I could solve this problem would be much appreciated.
EDIT: I'm thinking that it never gets to the connection.php file because it shows the "into" at the top of insert.php but not the "connection file reached" at the top of connection.php. Is that call correct to that file?
Thanks
Readup on php constants, variables and strings!
define('foo','bar'); // foo == 'bar';
$foo = 'bar'; // $foo == 'bar'
Please use mysqli_* as mysql_* is depreciated.
$conn = mysql_connect(localhost, user, pass);
probably should be
$conn = mysql_connect('localhost', 'user', 'pass');
you have written
#mysql_query($sql)
as well as a lot other problem it's structure is also wrong the problem might be from version of your language as recommendation of php you should not use mysql any more use mysqli or PDO
I'm in a bit of a pickle with freshening up my PHP a bit, it's been about 3 years since I last coded in PHP. Any insights are welcomed! I'll give you as much information as I possibly can to resolve this error so here goes!
Files
config.php
database.php
news.php
BLnews.php
index.php
Includes
config.php -> news.php
database.php -> news.php
news.php -> BLnews.php
BLnews.php -> index.php
Now the problem with my current code is that the database connection is being made but my database refuses to be selected. The query I have should work but due to my database not getting selected it's kind of annoying to get any data exchange going!
config.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "test";
?>
database.php
<?php
class Database {
//-------------------------------------------
// Connects to the database
//-------------------------------------------
function connect() {
if (isset($dbhost) && isset($dbuser) && isset($dbpass) && isset($dbname)) {
$con = mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect: " . mysql_error());
$selected_db = mysql_select_db($dbname, $con) or die("Could not select test DB");
}
}// end function connect
} // end class Database
?>
News.php
<?php
// include the config file and database class
include 'config.php';
include 'database.php';
...
?>
BLnews.php
<?php
// include the news class
include 'news.php';
// create an instance of the Database class and call it $db
$db = new Database;
$db -> connect();
class BLnews {
function getNews() {
$sql = "SELECT * FROM news";
if (isset($sql)) {
$result = mysql_query($sql) or die("Could not execute query. Reason: " .mysql_error());
}
return $result;
}
?>
index.php
<?php
...
include 'includes/BLnews.php';
$blNews = new BLnews();
$news = $blNews->getNews();
?>
...
<?php
while($row = mysql_fetch_array($news))
{
echo '<div class="post">';
echo '<h2> ' . $row["title"] .'</h2>';
echo '<p class="post-info">Posted by | <span class="date"> Posted on ' . $row["date"] . '</span></p>';
echo $row["content"];
echo '</div>';
}
?>
Well this is pretty much everything that should get the information going however due to the mysql_error in $result = mysql_query($sql) or die("Could not execute query. Reason: " .mysql_error()); I can see the error and it says:
Could not execute query. Reason: No database selected
I honestly have no idea why it would not work and I've been fiddling with it for quite some time now. Help is most welcomed and I thank you in advance!
Greets
Lemon
The values you use in your functions aren't set with a value. You likely need to convert the variables used to $this->dbName etc or otherwise assign values to the variables used.
Edit for users comment about variables defined in config.php:
You really should attempt to get the data appropriate for each class inside that class. Ultimately your variables are available to your entire app, there's no telling at this point if the variable was changed by a file including config.php but before database.php is called.
I would use a debugging tool and verify the values of the variables or just var_dump() them before the call.
Your Database class methods connect and selectDb try to read from variables that are not set ($dbhost, $dbname, $con, etc). You probably want to pass those values to a constructor and set them as class properties. Better yet, look into PDO (or an ORM) and forget creating your own db class.