Having trouble calling a stored procedure in php [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 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...";
}
?>

Related

i want get data from table sql [closed]

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 2 years ago.
Improve this question
i have a table register with email and username colums.
> ex - username email
> test test#gmail.com
> new new#gmail.com
SELECT * FROM Register WHERE email='test#gmail.com'
I can get this colom ,but i cant select username.i want assign username to variable
I think you want something like this if you use MySQL :
<?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 username, email FROM MyTable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "username: " . $row["username"]. " - email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
In my opinion you're probably a beginner with web development with databases.
I recommend you w3chools. You can reed various examples in this site:
https://www.w3schools.com/php/php_mysql_select.asp
Your question is not clear, but if you are new with database I recommend you to start with PDO instead of mysqli.
<?php
//Db connection
function pdo_connect_mysql() {
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'phpcrud';
try {
return new PDO('mysql:host=' . $DATABASE_HOST . ';dbname=' . $DATABASE_NAME . ';charset=utf8', $DATABASE_USER, $DATABASE_PASS);
} catch (PDOException $exception) {
// If there is an error with the connection, stop the script and display the error.
exit('Failed to connect to database!');
}
}
// User Input
$username = 'john';
$email = 'john#gmail.com';
$sql = 'SELECT * FROM register WHERE username = ? email = ?';
$stmt = pdo_connect_mysql() ->prepare($sql);
$stmt->execute([$username, $email]);
$register = $stmt->fetchAll();
?>

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.

php mysqli SELECT doesnt return row [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
I have this code:
$check_user = "SELECT * FROM privateChats WHERE name = '$category'";
$result = mysqli_query($conn,$check_user);
$row = mysqli_fetch_assoc($result);
if($row["user1"] == $login_session or $row["user2"] == $login_session){
$sql = "SELECT * FROM privateChat WHERE name = '$category' ORDER BY position DESC";
}
// it goes on
It gets stuck at $result = mysqli_query($conn,$check_user); it doesn't give an error either.
There is a table called privateChats and there is a record in it.
My connection code:
$servername = "localhost";
$username = "thewhateverclub";
$password = "password";
$dbname = "my_thewhateverclub";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_error($conn)) {
die("Connection failed: " . mysqli_connect_error($conn));
}
Try this -
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM privateChats WHERE name = '$category'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Data1 " . $row["column1"]. " - Data2: " . $row["column2"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
Also add this line at the start of your php script.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
However, this doesn't make PHP to show parse errors - the only way to show those errors is to modify your php.ini with this line:
display_errors = on
You need to check mysqli Errors by below way:-
$conn = mysqli_connect("localhost", "my_user", "my_password", "my_db");
//check connection error
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// check variable is set or not
$category = isset($category) ? $category : 'notSet';
$check_user = "SELECT * FROM privateChats WHERE name = '$category'";
$result = mysqli_query($conn,$check_user);
// Check for query errors
if(!$result){
printf("Error: %s\n", mysqli_error($conn));
}
$row = mysqli_fetch_assoc($result);
Hope it will help you :)

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;
}

mysqli SELECT query not working, for unknown reason [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
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;
?>

Categories