php mysqli SELECT doesnt return row [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
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 :)

Related

How to use PHP SQL with where Clause? [duplicate]

This question already has answers here:
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 2 years ago.
I want to select an email from the DB which needs to return the related Account_ID
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "connected accounts details";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT acc_id FROM users
WHERE
email = $user_email";
$result = $conn->query($sql);
echo "$result";
$conn->close();
This code returns nothing, just blank.
While using the same connection/db data is being inserted in db successfully.
Try this code
// Php Sql Select From DB....
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "connected accounts details";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `acc_id` FROM `users` WHERE `email` = ?";
$result->$conn->prepare($sql);
$result->bind_param('s',$user_email);
$result->execute();
$data = $result->get_result();
// Fetch all
$data->fetch_all(MYSQLI_ASSOC);
print_r($data);
$mysqli -> close();
// #link http://php.net/manual/en/mysqli-result.fetch-all.php
$sql = "SELECT acc_id FROM users
WHERE
email LIKE '%$user_email%' ";
$result = mysqli_query($conn, $sql);
$row = $result->fetch_assoc();
$id = $row['acc_id'];
echo "$id";
$conn->close();
Got the wanted Record, trying this way.
Thanks everyone answering on this post.

SQL database connection in PHP successful, but I can't query it [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "test";
$tablename = "mapcoords";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
echo "Failure";
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
echo "ok";
}
$conn->close();
?>
Here is the code. So like I said, it can connect successfully, but the code won't successfully query. What's weird is that if I copy and paste the same code, which seems to be EXACTLY the same, it works. It makes no sense. I can't find a single difference between their code and my code besides the way they space things and the way I space things. Here is their code:
<?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 (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["lat"]. " " . $row["lng"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The problem is that this query:
SELECT (lat, lng) FROM mapcoords
returns the folloowing error:
[21000][1241] Operand should contain 1 column(s)
You have to change the query to
SELECT lat, lng FROM mapcoords

converting from MySQL to MySQLi Object-oriented

Ok, so I've been informed that it would be best practice to convert over to the new mysqli
So I've been working on this on a new site, so far so good, but I ran into a problem where I can't figure out how to convert it for my search query
I have a search feature added to my site, but now I can't get it to work.
This was my old code:
$query = "SELECT * FROM snippet_tools WHERE `db_title` LIKE ".sql_val('%'.$_GET['search'].'%')." OR `db_body`=".sql_val('%'.$_GET['search'].'%');
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());
$anymatches = mysql_num_rows($result);
if ($anymatches == 0 ) {
I've upgraded my code to user oo
this is what I have:
$servername = "localhost"; $username = "xxxxxxxxx"; $password = "xxxxxxxxx"; $dbname = "xxxxxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql_search = "SELECT * FROM `questions` WHERE `q_title` LIKE ".sql_val('%'.$_GET['search'].'%')." OR `q_answered` LIKE ".sql_val('%'.$_GET['search'].'%');
$result = $conn->query($sql_search);
$anymatches = $result->num_rows;
if ($anymatches == 0 ) {
but every time I run it to perform a search I keep getting this error message:
Notice: Trying to get property of non-object in H:\root\site5\questions.php on line 611
You can refer W3 School site and get basic idea about the variations of MySQL and MySQLi and their functions. It is really helpful to go ahead with your project.
Ex Database connection example in both ways
So you can solve your problem based on that concept. Try to extract concepts.
Try this approach!
<?php
error_reporting(E_ALL);
$servername = "localhost"; $username = "root"; $password = "root"; $dbname = "cities";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$get = array('search' => "Lon");
$sql_search = "SELECT * FROM cities WHERE (city_name LIKE '%$get[search]%')";
$result = $conn->query($sql_search);
$anymatches = $result->num_rows;
if ($anymatches == 0 ){
echo "No matches!";
}
else
{
echo $anymatches . " match found!";
}
?>

How do I import info from a database to my website? [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
Using the code below I am trying to import users table into my php page and only want to show the last ten entries in a table, what else do I need to add to my code to achieve this
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM dispenses";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Amount: " . $row["amount"];
}
} else {
echo "0 results";
}
$conn->close();
?>
$query = "SELECT * FROM 'users'";
<?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 * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"];
}
} else {
echo "0 results";
}
$conn->close();
?>
This should help:
<?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();
?>
http://www.w3schools.com/php/php_mysql_select.asp

php unable to select 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 7 years ago.
Improve this question
I'm trying to select a SINGLE value from the mysql database. I have run the query in phpmyadmin and it work great. But when I echo the $result, I get nothing... by the way,for the database and password I use xxx because I don't want to show it... My insert query works very well
Thanks
<?php
//Create Connection
$servername = "localhost";
$username = "root";
$password = "xxx";
$dbname = "xxx";
//Connect
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT StartPriceUnder FROM YJ_Value";
$result = $conn->query($sql);
echo hi;
echo $result;
echo ya;
$conn->close();
?>
Try this:
<?php
$servername = "localhost";
$username = "root";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT StartPriceUnder FROM YJ_Value";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "StartPriceUnder:" . $row["StartPriceUnder"];
}
}
else {
echo "0 results";
}
$conn->close();
?>
You have to fetch your result, so do something like this:
$row = $result->fetch_array(MYSQLI_ASSOC);
After this you can echo it like this:
echo $row["StartPriceUnder"];
For more information about fetch_array() see the manual: http://php.net/manual/en/mysqli-result.fetch-array.php

Categories