mysqli SELECT query not working, for unknown reason [closed] - 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 7 years ago.
Improve this question
Here is my php for connecting to a database and querying the table "userActivityTime", in which there is one row. I am not having trouble connecting to the database (i.e. no errors), but my query is not working, and despite looking all over the internet, I cannot figure out why. Hoping you all can help. Thanks so much in advance!
<?php
// ESTABLISH TABLE AND COLUMN NAMES
$mysqli = new mysqli("*****", "****", "*****", "****");
// MAKE SURE CONNECTION SUCCEEDED
if ($mysqli_connection->connect_error) {
echo "Not connected, error: " . $mysqli_connection->connect_error;
exit();
} else {
echo "connected";
}
$query = "SELECT 'userDailyTime' FROM 'userActivityTime'";
if ($mysqli->query($query)) {
echo $mysqli->error;
}
exit();
?>

use back-ticks if necessary not single quotes:
SELECT `userDailyTime` FROM `userActivityTime`
or just
SELECT userDailyTime FROM userActivityTime
Edit:
An example from the internet, worm your stuff into the concept.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

You don't need to put the table and column names in quotes. You are telling it to echo $mysqli->error when the query is successful (prepend it with an exclamation mark) . Also, you create $mysqli then refer to it as $mysqli_connection, so I am not sure how your code works.

Try this:
<?php
// ESTABLISH TABLE AND COLUMN NAMES
$mysqli = new mysqli("*****", "****", "*****", "****");
// MAKE SURE CONNECTION SUCCEEDED
if ($mysqli->connect_errno) {
echo "Not connected, error: " . $mysqli->connect_error;
return false;
} else {
echo "connected";
}
$query = "SELECT userDailyTime FROM userActivityTime";
if (!$mysqli->query($query)) {
echo $mysqli->error;
}
return true;
?>

Related

How to search for a specific column data using a variable and update its column data [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 2 years ago.
Improve this question
What I'm trying to do: I'm trying to change a specific column under the 'username' row where the username is the same as $loginuser var and change the speedrunhighscore row in that column into a new speedruhighscore.
The problem:
in the code below there is a line in which I put in bold, and that's the line I'm trying to run to change the data in my database but nothing changes in my database but the echos are all running smoothly.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "smolgames";
$speedrunhighscore = $_POST["speedrunhighscore"];
$loginuser = $_POST["loginuser"];
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){
die("connection failed: " . $conn->connect_error);
}
$sql = "SELECT username FROM userinfos WHERE username = '" . $loginuser . "'";
$result = $conn->query($sql);
if($result->num_rows > 0){
**$sql3 = "UPDATE userinfos SET speedrunhighscore = (' . $speedrunhighscore . ') WHERE username = '" . $loginuser . "'";**
echo "updating your new highscore":
if($conn->query($sql3) === TRUE){
echo "your highscore have been updated successfully!";
}
else{
echo "Error: ". $sql3 . "<br>" . $conn->error;
}
}
else{
echo "no usernames found";
if($conn->query($sql2) === TRUE){
echo "new highscore send successfully";
}
else{
echo "Error: ". $sql2 . "<br>" . $conn->error;
}
}
$conn->close();
?>
note: the variable loginuser changes from a string I post using unity C#
For starters, you should be using prepared statements with bounded placeholders. This ensures your query is not vulnerable to SQL injection attacks, and ensures that even usernames such as O'Riley would work.
Next up, you don't need to check if the row exists before updating it -- you can just attempt to perform the update right away, and check how many rows was in fact updated.
Lastly, you should be configuring your MySQLi connection to throw exceptions on error, this means that you don't have to do individual error handling for each and every query.
<?php
// Configure MySQLi to throw exceptions on failure instead
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "smolgames";
$speedrunhighscore = $_POST["speedrunhighscore"];
$loginuser = $_POST["loginuser"];
try {
$conn = new mysqli($servername, $username, $password, $dbname);
$stmt = $conn->prepare("UPDATE userinfos
SET speedrunhighscore = ?
WHERE username = ?");
$stmt->bind_param("ss", $speedrunhighscore, $loginuser);
$stmt->execute();
$affectedRows = $stmt->affected_rows;
$stmt->close();
if ($affectedRows) {
echo "your highscore have been updated successfully!";
} else {
echo "no usernames found";
}
} catch (Exception $e) {
// Handle the exception
// Log it, send a message to the user "something went wrong"
}
You should be implementing some sort of authentication and authorization layer, as now you can just submit someone else's username with any arbitrary highscore, and you can basically update any scores in the table.

Having trouble calling a stored procedure in php [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 am new the PHP. All I am trying to do right now is call a stored procedure from a MySQL server. I was able to successfully query the database with a standard SELECT statement but when I try calling a stored procedure it never works. If I swap $sql out for just standard SELECT * FROM table, it works. I have looked everywhere. But can't figure out what to do. Where am I going wrong?
<?php
$servername = server;
$username = username;
$password = password;
$dbname = databasename;
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
echo "It worked!<br/><br/>";
}
if(mysqli_ping($conn) != null) {
echo "Connection is open!<br/><br/>";
$sql = "CALL GetUserTypes";
$result = $conn->query("$sql");
if ($result != null) {
$i = 0;
$row = mysqli_fetch_row($result);
while ($row = $result->fetch_assoc()) {
echo '<p>' . $row['id']. " " . $row['type'] . '</p>';
$i++;
}
}
elseif (is_null($result)) {
echo "Something broke...<br/>";
}
else {
echo "Query did not work...<br/>";
}
$con->close();
else {
echo "No connection...";
}
?>

php update record repeat region [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
As previously stated in my last question on WHAT a cron job is, I'm now trying to write one but seem to get a problem. This is the closest I've gotten. I want to update every record in the database by one more than what it already has.
Example if the field is 30, then when this script is run then it will become 31. But I have MULTIPLE fields and I want all of them to increment by one.
This is what I've currently wrote, but it doesn't work, but I DO get an echo of "Record Updated Successfully" but nothing changes. If I change it so that $Age = $Test and then I plug in a random number for the ID it will end up equaling Test. If I have it say $Age = $NewAge and the random ID, it changes the variable in my database to be 302. I have NO clue where it's pulling that random 302.
Any suggestions?
This is my code below:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "RR";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sqlSelect = "SELECT id, Age FROM Horse";
$result = $conn->query($sqlSelect);
do{
$id = $row['id'];
$Age = $row['Age'];
$NewAge = $Age + 1;
$Test = 200;
echo $id, ' ', $Age, ' ', $NewAge; ?> <br/>
<?php
$sqlUpdate = "UPDATE Horse SET Age='$NewAge' WHERE id='$id'";
}
while($row = $result->fetch_assoc());
if ($conn->query($sqlUpdate) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
The question is very vague and hard to understand, but as #rjdown said, this will update ALL rows in the database and increment Age by 1.
Note: there is no need for fetching the records from database, nor looping over them. Just one line of SQL will update ALL rows.
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "RR";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sqlUpdate = "UPDATE Horse SET Age = Age + 1";
if ($conn->query($sqlUpdate) === TRUE) {
echo "All records updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

Something is wrong with sql query [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
Please check this code and let me know if there is any mistakes in it.
// Create connection
$conn = mysqli_connect($servername, $username, $password, $shop_item);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$item_sql = "SELECT * FROM shop-items";
mysqli_query($conn, 'SET CHARACTER SET utf8;');
$result_item = mysqli_query($conn, $item_sql);
echo var_dump($result_item); //returns: bool(false)
if (mysqli_num_rows($result_item) > 0) { // doesn't execute. the error is "mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean";
As I mentioned shop-items is treated by mysql as shop minus items.
If your table name is really shop-items - you should use backticks to escape it:
$item_sql = "SELECT * FROM `shop-items`";
And for checking errors you can use mysqli_error() function:
$err = mysqli_error($conn);
echo $err;
It could be better to use structure similar to this:
$conn = new mysqli('localhost', 'root', 'password', 'your_database');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql="SELECT * FROM shop-items";
if ($result = $conn->query($sql)) {
// actions in case of success
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
It may be an issue with the dash. Have you tried
SELECT * FROM `shop-items`
I would try and use the following code for all my queries from PHP to you database because as someone mentioned before with what you're using errors can occur from not syntax:
$mysqli = new mysqli("localhost", "user", "password", "DB");
/* Check conection */
if (mysqli_connect_errno()) {
printf("Conection error: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM shop-items";
if ($stmt= $mysqli->prepare($query)) {
/* execute the query */
$stmt->execute();
/* Bind the results to variables */
$stmt->bind_result($col1, $col2); //as many variables as columns the query will return
/* obtener los valores */
while ($stmt->fetch()) {
//do something with the results
}
/* close the query */
$stmt->close();
}
/* close the DB connection */
$mysqli->close();
?>
Hope this helps!!

How to write PHP to Mysql page

Currently I just done HTML and I trying writing PHP Script on connection Mysql, But all web site on google is confuse, because now I need specify write connection by used PHP Only,
Please could you help explain step by step on code from HTML to PHP and write PHP to MySql, Thank you very much.
There is many docs about this, but you can start from here:
http://www.w3schools.com/php/php_mysql_select.asp
And here is the example code, what i suggest you:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>

Categories