Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 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.
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
Improve this question
i need to change this mysqli to to working PDO:
$user = "name";
$pass = "password";
try {
$dbh = new PDO('mysql:host=host;dbname=dbname', $user, $pass);
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
$sth = $dbh->prepare("SELECT * FROM table");
$sth->execute();
while ($user = $sth->fetch(PDO::FETCH_ASSOC)) {
echo $user[1];
}
Using this fetch i dont get the expected result..
but this doesnt work, anybody could check it?
greetings
Why are you setting $dbh to null?
$dbh = new PDO('mysql:host=host;dbname=dbname', $user, $pass);
$dbh = null; <--remove this line.
It should be either:
while ($user = $sth->fetch(PDO::FETCH_ASSOC)) {
echo $user['username']; // Put the actual column name here, I'm just guessing
}
or:
while ($user = $sth->fetch(PDO::FETCH_NUM)) {
echo $user[1];
}
PDO::FETCH_ASSOC is like mysqli_fetch_assoc, and PDO::FETCH_NUM is like mysqli_fetch_row.
Related
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
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.
Improve this question
I am trying to make an update statement with PDO and i found out it doesn´t work.
I have testet the SQL statement in phpMyadmin and it works if i put '' arround the passkey, but why wont it work with this ?
INFO:
The passkey is a md5 string
<?php
include('../mysql/pdoconn.php');
$passkey = $_GET['passkey'];
$stmt = $conn->prepare("UPDATE user SET com_code='' WHERE com_code = :passkey");
$stmt->bindParam(':passkey', $passkey , PDO::PARAM_STR);
$stmt->execute;
$error = "Jon Snow";
$stmt1 = $conn->prepare("SELECT com_code from user where com_code =''");
$stmt1->execute;
$result = $stmt1->fetchColumn();
if($result === "")
{
$error = 'Your account is now active. You may now Log in';
$conn = null;
} else
{
$error = $passkey;
$conn = null;
}
?>
i have tested that it gets the passkey, and it does, but it dont update the table...
I have tried anything, but i cant get it to work
$stmt = $conn->prepare("UPDATE user SET com_code='' WHERE com_code = :passkey");
$stmt->bindParam(':passkey', $passkey , PDO::PARAM_STR);
$stmt->execute();
execute() is a function
You don't need to quote bound parameters
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
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 getting a strange error with PDO:{"error":{"text":SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens}}
I tried this sql query and i didn't find,if someone could help me;
My code:
$sql = "UPDATE feeds SET status=:statuschosen WHERE idUser=:id AND id:idfeed";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("statuschosen", $post->statuschosen);
$stmt->bindParam("idfeed", $post->idfeed);
$stmt->bindParam("id", $id);
$stmt->execute();
$db = null;
echo json_encode($post);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
My table have this structure:
id URL idUser status
Thank you for your help!!!
You were missing an equal sign in your $sql string. Also while binding the params you have used wrong placeholders,See below:
$sql = "UPDATE feeds SET status=:statuschosen WHERE idUser=:id AND id=:idfeed";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam(":statuschosen", $post->statuschosen);
$stmt->bindParam(":idfeed", $post->idfeed);
$stmt->bindParam(":id", $id);
$stmt->execute();
$db = null;
echo json_encode($post);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
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 have new to PHP programming and looking to create access a database I created. I have been able to get a successful connection going between PHP and my database, but the problem arises when I try to run a simple query.
I get the dreaded message mysqli_query() expects parameter 1 to be mysqli. I have seen numerous issues on this throughout the internet. I still am unable to resolve my situation. Can someone please address my code here:
$mysqli= mysql_connect($hostname,$username,$password,'japanesewords')
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
mysqli_query($mysqli, 'SELECT * FROM japanesedefinition') or die(mysql_error($mysqli));
Why don't you use PDO?
<?php
try {
// config
$dsn = 'mysql:dbname=japanesewords;host=127.0.0.1;charset=utf8';
$username = 'root';
$password = '';
$options = array(
// necessary for rowCount() on SELECT
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
// for catching SQL errors as PDOException
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
// default fetch mode is used for iterating PDOStatement by foreach()
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
);
// connect
$pdo = new PDO($dsn, $username, $password, $options);
// execute SQL
$stmt = $pdo->query('SELECT * FROM japanesedefinition');
// check row count
if (!$stmt->rowCount()) {
throw new Exception('no data');
}
// fetch results and display
echo "<p>\n";
foreach ($stmt as $row) {
printf("foo: %s; bar: %s;<br />\n", $row->foo, $row->bar);
}
echo "</p>\n";
} catch (Exception $e) {
printf("<p>%s</p>\n", $e->getMessage());
}
I don't know whether you're japanese or not, remark the summary in Japanese for connectiong to MySQL with PHP.
http://qiita.com/mpyw/items/b00b72c5c95aac573b71