How to get stuff from database using PDO? [closed] - php

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 8 years ago.
Improve this question
I'm new at PHP and just learning.
<?php
// database connection
$dbhost = "localhost";
$dbname = "pdo";
$dbuser = "root";
$dbpass = "7777777";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass)
// echo from database
$select = $conn->query('SELECT username FROM users');
while($row = $select->fetch(PDO::FETCH_ASSOC))
{
$username = $_GET['username'];
echo $username;
}
?>
This gives error:
Parse error: syntax error, unexpected T_PUBLIC in line : $select = $conn->query('SELECT username FROM users');

The connection needs to be changed
From:
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass)
To:
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
The connection is missing a ;

Related

Problem Trying to get property 'num_row' of non-object in and Undefined variable: stmt_result in Php [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 1 year ago.
Improve this question
I am facing a problem when i am try to check user and password from database while login it keep
reply an error message :
Notice: Trying to get property 'num_row' of non-object in /Applications/XAMPP/xamppfiles/htdocs/studyact/login.php on line 27
User name or Password is incorrect, please check and try again.
i type user and password correct! enter image description here
php file :
<?php
//html
$user_staff = $_POST["user_staff"];
$pass_staff = $_POST["pass_staff"];
// Create connection
$servername = "localhost";
$username = "root";
$password = "";
$db ="studyact";
$con = new mysqli($servername, $username, $password,$db);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}else{
$stmt =$con-> prepare("select * from loginstaff where user_staff = ?");
$stmt->bind_param("s",$user_staff);
$stmt->execute();
$stmtresult = $stmt->get_result();
if($stmtresult-> num_row > 0){
$data = $stmtresult-> fetch_assoc();
if($data["pass_staff"] === $pass_staff){
echo "<h2>Login Successfully</h2>";
}
else{
echo "<h2> Sorry User name or Password is incorrect.</h2>";
}
}else{
echo "<h2> User name or Password is incorrect, please check and try again.</h2>";
}
}
?>
You've got a typo on line 27
if($stmtresult->num_rows > 0)
mysqli_stmt::$num_rows — Returns the number of rows fetched from the server

ParseError syntax error, unexpected 'while' (T_WHILE) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "jaka_crud_ci";
mysqli_connect($server, $username, $password, $database);
$query = "SELECT * FROM pembeli";
$result = mysqli_query($query)
while ( $buyer = mysqli_fetch_assoc($result)){
echo $buyer ["nama_pembeli"];
echo $buyer ["nama_barang"];
echo $buyer ["nama_retribusi"];
}
?>
code above displaying syntax error, unexpected 'while' (T_WHILE). How it will be free from error? Help me
Put a semicolon at the end of
$result = mysqli_query($query);
And you got the syntax wrong for mysqli_query, the correct one is,
mysqli_query($connection_variable, $query)
So for your case it will be like,
$con = mysqli_connect($server, $username, $password, $database);
and then use it like,
$result = mysqli_query($con, $query);
Please update below line
$result = mysqli_query($query)
to
$result = mysqli_query($query);
; is missing in that line. That's why the error occurred.
You are missing ; after mysqli_query($query) . Updated code is as below.
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "jaka_crud_ci";
mysqli_connect($server, $username, $password, $database);
$query = "SELECT * FROM pembeli";
$result = mysqli_query($query);
while ( $buyer = mysqli_fetch_assoc($result)){
echo $buyer ["nama_pembeli"];
echo $buyer ["nama_barang"];
echo $buyer ["nama_retribusi"];
}
?>
its pretty simple syntax error,You are missing ; after mysqli_query($query)
You can also use the php command line options to check the sytax errors
php -l filename
-l Provides a convenient way to perform only a syntax check on the given PHP code. On success, the text No syntax errors detected in <filename> is written to standard output
it will show if there any sytax error in the give filename
More Detail :http://php.net/manual/en/features.commandline.options.php
i hope this is helpful

I got the json data false. [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 5 years ago.
Improve this question
Here Is my PHP code to encode the JSON data from MySQL database. And this is my URL http://fwtest.ga/appoint.php; I tested on the online website JSONLint to validate my JSON data. It is valid, but I got the result [false] instead of the data in JSON format. Can anybody tell what am I doing wrong?
<?php
$host = "my_host";
$user = "user";
$password = "pass";
$db = "db_name";
$con = mysqli_connect($host, $user, $password, $db);
$sql = "select time, date from table_name;";
$result = mysqli_query($con, $sql)
or die("Error: ".mysqli_error($con));
$response = array();
while ($row = mysqli_fetch_array($result))
{
array_push($response, array("time" >= $row[1], "date" >= $row[2]));
}
echo json_encode(array("server_response">= $response));
echo (json_last_error()=== JSON_ERROR_UTF8);
mysqli_close($con)
?>
Probably because you're returning a single boolean here:
json_encode(array("server_response">= $response));
↑
That's not the array operator.

print a column from Mysql database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
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.
Improve this question
I am trying to print out one column called Language1 from my Table that is called Mull, in a database called v6e.
At the moment i am getting a blank white screen.
<?php
session_start();
$servername = "localhost";
$user = "xxxx";
$password = "xxxx";
$dbname = "v6e";
// Create connection
$conn = mysqli_connect($servername, $user, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
else
{
$query = "SELECT Language1 FROM Mull WHERE username = 'Mull'";
$result = mysqli_query($query);
$row = mysqli_fetch_arrary($result);
echo $row['Language1'];
}
mysqli_close($conn);
?>
You have a typo issue. Change the line:
$row = mysqli_fetch_arrary($result);
With:
$row = mysqli_fetch_array($result);
Plus, you're also not connecting to DB with your query
$result = mysqli_query($conn, $query);
Reference:
http://php.net/manual/en/mysqli.query.php
You should also check for errors:
http://php.net/manual/en/mysqli.error.php

Undefined variable: password [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 9 years ago.
Improve this question
Trying to signup with new username and password and get this output - "Undefined variable: password"
<?php
require_once "lib/user_class.php";
$user = User::getObject();
if (isset($_POST["reg"])) {
$login = $_POST["login"];
$login = $_POST["password"];
$reg_success = $user->regUser($login, $password);
}
?>
There's no password variable defined, that's why it says $password is undefined.
Perhaps you meant $login = $_POST["password"]; as $password = $_POST["password"];? I'm no mind-reader, but I don't think you wanted to create duplicate variables.
The variable password is not defined..
i have edited your code.
<?php
require_once "lib/user_class.php";
$user = User::getObject();
if (isset($_POST["reg"])) {
$login= $_POST["login"];
$password= $_POST["password"];
$reg_success = $user->regUser($login, $password);
}
?>

Categories