How to fix non response mysql connection in PHP - php

I am trying to get a variable from a mySQL table into my PHP code. I am using a mysqli_query command. When I try to echo the result, it echos nothing, not even other echo statements.
I have ensured that the $conn is correct and working, and that the Sql query is correct by running it directly in PHPmyAdmin.
<?php
session_start();
include_once("includes/dbh.inc.php");
$UID = $_POST['userIdVariable'];
$UID = $_SESSION['u_uid'];
$balance = "SELECT account_balance FROM `users` WHERE user_uid = \"$UID\"";//this line probably works
$result = mysqli_query($conn, $balance);
echo "$result";
When I run this code it would output nothing. The right side of the $balance line is a good sql query according to PHPmyadmin. The $UID variable is read in correctly.

You need to use mysqli_fetch function to get result columns out of $result object.
while ($data = mysqli_fetch_assoc($result)) {
printf("%s \n", $data["account_balance"]);
}

Don't use quotation in a field name or table name inside the query.
$balance = "SELECT account_balance FROM users WHERE user_uid = '$UID'";

please check your database connectivity.there could be something missing like database name.
and print your $UID. may be there is no id getting.
to print data use print_r();
and try this:
session_start();
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password,"databasename");
$UID=1;
$balance = "SELECT account_balance FROM users WHERE user_uid =$UID";
$result = mysqli_query($conn, $balance);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['account_balance'];
}
}

Related

How to use variables in mysql table with PHP

I am a beginner of the program.
I am trying to use a PHP variable written in a MySQL table, but it is not recognized as a variable in PHP.
I made a table to test using this code
CREATE TABLE test_table (
'id' INTEGER,
'col_1' TEXT
);
And insert data;
INSERT INTO test_table VALUES('1', '$var');
I`ve tried to test using simple PHP code;
<?php
$server = 'localhost';
$user = 'root';
$password = '1111';
$database = 'test_database';
$conn = mysqli_connect($server, $user, $password, $database);
$sql = "SELECT * FROM test_table";
$table = mysqli_query($conn, $sql);
while($rows = mysqli_fetch_assoc($table)) {
$var = 1;
echo $rows['col_1'];
}
?>
I expected the result will be '1' but actual result was '$var'.
How can MySQL data be recognized as a PHP variable?
Instead of using
echo $rows['col_1'];
use like this
$result=$query->result_array();
echo $result;

Trouble querying database with php*fixed*

I am having trouble selecting my data from a database and displaying it. I have looked at tutorials and i still get the same error. Some help would be appreciated. The error i am getting is couldnt fetch result.
$sql = "SELECT * FROM data";
$result = mysql_query($sql) or die("couldnt fetch result");
if($result > 0){
while ($rows = mysql_fetch_array($result)){
$username = $rows['username'];
echo $username;
}
}
Just do that (assuming got it right connecting to DB, first thing to check !)
$sql = "SELECT * FROM `data`"; // data is a reserved keyword, protect it !!!
$result = mysql_query($sql) or die("couldnt fetch result"); // potentially diying here
if($result){
while ($row = mysql_fetch_assoc($result)){
$username = $row['username'];
echo $username;
}
}
If what you're getting is literally 'couldnt fetch result' it means your mysql_query() fails, and die statement takes over. Check your database connection.
I think the very simple problem is that you check if the $result is greater then 0. But you get an resource.
$conn = mysql_connect.......
$sql = "SELECT * FROM data";
$result = mysql_query($sql) or die("couldnt fetch result");
if($result){
while ($rows = mysql_fetch_array($result)){
$username = $rows['username'];
echo $username;
}
}
And if you see your die statement you have an error in your SQL Syntax. Its very short but its possible that your table doesn't exist in that database you're trying to connect. I hope you have a connect before and its not your complete code.
You use the old mysql functions. Its better to use MySQLi or PDO.
And DATA is a reserved keyword its possible that you get problems if you use it in your query. Rename your table in prefix_data for example.
https://dev.mysql.com/doc/refman/5.7/en/keywords.html

Double or more value inserted when insert query written inside another query result object

I have the code below which was written in php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * from table name";
$result = $conn->query($sql);
//the result count is 95142
while($row = $result->fetch_assoc()) {
$sql = "insert into table1 (column) values ('test')";
$result = $conn->query($sql);
}
$conn->close();
?>
Which inserting more than 100 000 data in table1 but if I try to limit the source query up to 30 000 it inserts correct count of data into table1.
i.e
$sql = "SELECT * from table name limit 10000";
Both the table are in same Database.
Even tried with mysql_connect & mysql_query() method also getting the same error. Also tried with another connection for insert query but issue exists.
If I try the code like this
$flag = 1;
while($row = $result->fetch_assoc()) {
$flag = $flag + 1;
}
echo $flag;
I am getting the result of 95142.
you must really use different variable names in the second query !!!
while($row = $result->fetch_assoc()) {
$sql2 = "insert into table1 (column) values ('test')";
$result2 = $conn->query($sql2);
}
and secondly, delete the previous rows before running script, and then check the count, it must work fine.
Change your variable name $result inside the while.
Ex:
while($row = $result->fetch_assoc()) {
$insertSQL = "insert into table1 (column) values ('test')";
$insertRow = $conn->query($insertSQL);
}
The value of $result has been changed when insert query is executed. Maybe this is main problem.

Mysql Fetch not working

i really dont know why this code isnt working.. database connection works, the timestamp is written to the database.
But i cant figure out why i get a blank page with this code here (i should see the timestamp as echo).
Anyone an idea about this ?
Thank you!
<?php
$user = "daycounter";
$password = "1234";
$database = "daycounter";
$host = "localhost";
$date = time();
// Create connection
$conn = new mysqli($host, $user, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Error: " . $conn->connect_error);
}
//Insert timestamp in database
$sql = "INSERT INTO datum (datum)
VALUES ('".$date."')";
//check if that worked
if ($conn->query($sql) === TRUE) {
echo "That worked!";
}
//get timestamp from db and display it as echo
$select = "SELECT 'datum' FROM 'daycounter'";
$result = mysql_query($select);
while($row = mysql_fetch_object($result))
{
echo "$row->datum";
}
?>
You're using a mysqli DB connection, but calling mysql to do your select. You cannot mix/match the database libraries like that. If you'd had even minimal error checking, you'd have been told that there's no connection to the db:
$result = mysql_query($select) or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^
Plus, your select query has syntax errors. 'daycounter' is a string literal - you cannot select FROM a string. 'datum' would be syntactically correct, you can select a string literal from a table, but most like you want:
SELECT datum FROM daycounter
or
SELECT `datum` FROM `daycounter`
Neither of those words are a reserved word, so there's NO need to quote them, but if you're one of those people who insist on quoting ALL identifiers, then they must be quoted with backticks, not single-quotes.
$select = "SELECT 'datum' FROM 'daycounter'";
$result = mysqli_query($conn, $select);
while($row = mysqli_fetch_object($result)) {
echo "$row->datum";
}

mysql_query() not taking php variables

I'm passing a variable to a mysql query, $name is a variable that is getting a decrypted string. It is later passed to the SEARCH query. $name has a name in it (which i have seen via an echo).
The SEARCH query just wont take this variable. If i quote the string that is present in the SQL table, i do get an output (count as 1). I cant see where the problem is, because the same code is working in another file (its taking a variable in its query from an HTML entry though), and its embarrassing!
$decrypted_text1 = mcrypt_ecb(MCRYPT_DES, $key_value, $encrypted_text1, MCRYPT_DECRYPT);
$name = $decrypted_text1;
$username = "root";
$password = "speaker1";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
$selected = mysql_selectdb("login", $dbhandle);
$query = "SELECT * FROM users WHERE Username='$name' ";
$result = mysql_query($query, $dbhandle) or die(mysql_error());
$count5 = mysql_num_rows($result);
Delete the quotation marks around the variableit should look like this:
$query = "SELECT * FROM users WHERE Username=$name ";

Categories