Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I've been at this for a while now and anything I do or research turns up more errors. I keep getting this error: Fatal error: Call to a member function query() on a non-object with code that looks like this. the pic column is for a profile picture directory they uploaded.
$newTarget_file = "uploads/picture.png"
....
$sql = "UPDATE users SET pic='$newTarget_file' WHERE username=:username";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
also this is a php that is submited to that saves the picture they upload and the directory is the $newTarget_file . This is realy frustrating my on how I do this. Thanks in advance for the help.
Basically I want to update the pic column where username=:username with a variable I have already $newTarget_file . what do I do?
As others have said, the error is because your connection object doesn't exist.
Fatal error: Call to a member function query() on a non-object
Based on the tutorial you are following, have you implemented this?
$conn = new mysqli($servername, $dbuser, $dbpw, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
You should have gotten a connection error before the query ever tried to run. Also, as Michael pointed out, you can't used named parameters with mysqli. It's only supported in PDO, but you can still use placeholders. Your new code should look something like this.
if (!($stmt = $conn->prepare("UPDATE users SET pic=? WHERE username=?"))) {
echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
exit;
}
if (!$stmt->bind_param("ss", $newTarget_file, $username)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
exit;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
exit;
}
$stmt->close();
Related
I want to be able to read the raw data from the URL path. The URL will look like:
https://newtest.000webhostapp.com/db.php?datainurl
The db.php is:
<?php
$postdata = file_get_contents("php://input");
$conn = new mysqli('localhost','2541','yhte','tg543');
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "insert into cloud set rfid='.$postdata'";
if ($conn->query($sql) === TRUE)
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
When I press enter in the URL the webpage echos: New record created successfully. I see the id in the database but the contents is not shown in the database.
Why the "datainurl" in the URL is not showing in the database?
The query string is available through $_SERVER['QUERY_STRING'].
php://input contains the request body.
I am trying to truncate a table in my database. The error I get is
PHP Fatal error: Uncaught Error: Call to a member function query() on boolean in /var/www/html/rubynetwork_servers/truncate.php:4\nStack trace:\n#0 {main}\n thrown in /var/www/html/rubynetwork_servers/truncate.php on line 4
I have tried allsorts including looking on stackoverflow for the answers. My code is
<?php
error_reporting(E_ERROR | E_PARSE);
$con = mysqli_connect("localhost","root","-snip-","ruby");
$sql = "truncate table ruby_servers";
if ($con->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
The issue is with the mode you are programming. The right working code will be:
<?
error_reporting(E_ERROR | E_PARSE);
$mysqli = new mysqli("localhost", "username", "password", "databasename");
$sql= "TRUNCATE TABLE tbl_name";
//$mysqli->query($sql) // this will execute query
// Your checking conditions here.
if ($mysqli->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $mysqli->error;
}
$mysqli->close();
?>
Please note the way I have used 'new'. It is required by object-oriented way of programming.
You are applying object-oriented way to code to procedural mode.
'->' can only be used when you have a "new" object. So, use new.
Don't forget to close the connection after using it.
If it's still hard for you to understand then you may still use procedural mode of mysqli extension. Please don't use old mysql extension in new projects.
Reference:
http://php.net/manual/en/mysqli.quickstart.dual-interface.php
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 can access localhost/phpmyadmin/ through my browser and create tables, insert tuples, etc but I can't connect to it in PHP code. Here's what I got:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$conn = new mysqli('localhost', 'root', '');
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success';
$mysqli->close();
?>
and i have an HTML form somewhere else with an action attribute that runs the above code. But whenever I test my code, it returns an Error 500. I won't tell me anything more specific, even though I included error reporting! Why?
You have error in creating connection:
mysqli is not a function, you need to use mysqli_connect
$conn = new mysqli_connect('localhost', 'root', ''); // here is change
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success'
$conn->close(); // close connection using connection resource
?>
CREATE DATABASE 'some database name' ;
Works as expecting using mysql's client.
The same query (different database name) from php/mysqli - fails kind of.
It writes to the INFORMATION_SCHEMA.SCHEMATA
But fails to write to the mysql.db table?
This is actually an issue of a larger problem.
What is going on. Why does my mysqli fail?
code:
$mysqli = new mysqli("p:127.0.0.1", "root", "showtech123", "mysql");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if(!$mysqli->query( "CREATE DATABASE $database_name;"))
{echo "DATABASE encountered error:" . $mysqli->error . "<br /";}
Errors:
db_name: terarydatabase3333, un:teraryuser5999
Warning: mysqli::query(): MySQL server has gone away in /www/admin.showtechllc.com/public_html/adddb.php on line 21
Warning: mysqli::query(): Error reading result set's header in /www/admin.showtechllc.com/public_html/adddb.php on line 21
USER encountered error:MySQL server has gone away
the fourth parameter in the constructor is the database you want to use.
you're actually trying to create a new database inside an existing one.
try this :
$mysqli = new mysqli("p:127.0.0.1", "root", "showtech123");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}else{
echo "connected";
}
if(!$mysqli->query( "CREATE DATABASE $database_name;")){
echo "DATABASE encountered error:" . $mysqli->error . "<br /";
}else{
echo $database_name . "created";
}
I have been banging my head against a wall for hours now, reading several answers on SO, searching the 'net, trying to find a solution. While I realize similar questions have been asked and answered here, my code is different so I can't extrapolate from previous answers what's wrong with mine.
I'm new to PHP/MySQL. I have a poll script that I patched together using some code I found online and my own knowledge; the MySQL is mine, standard stuff. Each time a person votes for one of two things, its corresponding value in the database in increased by one. I'm trying to retrieve and assign each value to its own variable and perform a calculation. I can retrieve the first one just fine, but I get the dreaded
Trying to get property of non-object in..." error and
"Fatal error: Call to a member function bind_param() on a non-object in...
for the second one. It's exactly the same as the first prepare, so I'm not sure what's wrong? This may not be the most efficient way to do this, but like I said, I'm new to this. I DO understand what the error is trying to convey; I just don't understand why I'm getting the error when the prepare statement is identical to the one before it except for the id in the WHERE clause. var_dump($query2) returns bool(false), so I can clearly see nothing is there. But there IS something in the database.
<?php
// Turn on error reporting
ini_set('display_errors', 'On');
// Connect to database
$mysqli = mysqli_connect('localhost', 'dbuser', 'dbpwd', 'dbname');
// Check connection
if (!$mysqli || $mysqli->connect_errno)
{
echo "Connection error: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
exit();
} else if (isset($action)) // This is set through a page redirect
{
// DISPLAY ITEM - THIS WORKS FINE
if (!($query = $mysqli->prepare("SELECT votes FROM influential WHERE id = 1")))
{
echo "Prepare failed: " . $query->errno . " " . $query->error;
}
if (!$query->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if (!$query->bind_result($infl1))
{
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if ($query->fetch())
{
echo $infl1;
}
// DISPLAY ITEM - THIS DOESN'T WORK
if (!($query2 = $mysqli->prepare("SELECT votes FROM influential WHERE id = 2")))
{
echo "Prepare failed: " . $query2->errno . " " . $query2->error;
} else echo "Prepare succeeded.";
$query2->bind_param("i", $votes);
if (!$query2->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
} else echo "Execute succeeded.";
if (!$query2->bind_result($infl2))
{
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
} else echo "Bind succeeded.";
if ($query2->fetch())
{
echo $infl2;
}
exit();
$mysqli->close();
}
?>
In mysqli, when you issue the command:
.bind_param()
you are binding parameters to placeholders in the original query, not in the result. Your select query should read:
SELECT `votes` FROM `influential` WHERE `id`=?;
Then, you use bind_param to bind an integer to the placeholder, thusly:
.bind_param('i', $id);
After calling .execute(), the result is bound using references by calling .bind_result(). Whatever variable you pass into .bind_result() will contain the data afterwards.
Cheers