PHP mysqli SELECT query won't execute - php

$dc=fopen("numeu.txt","rb");
$row1=fgets($dc,120);
$row2=fgets($dc,120);
$row3=fgets($dc,120);
fclose($dc);
$servername="localhost";
$username="root";
$password='';
$database="Stildev";
$conn = new mysqli($servername, $username, $password, $database);
$sql="SELECT poza1,poza2,poza3,poza4,poza5,poza6 FROM postari WHERE utilizator='".$row2."' AND subiect='".$row1."' AND id2='".$row3."'";
$resultat=$conn->query($sql);
echo $resultat->num_rows;
This piece of code returns 0 rows
There is no error and sql query works just fine in MySQL console.
Also i discovered that withought WHERE clause the code works.How you explain that?

Is the password empty on purpose?
Try adding
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
Also, according to https://php.net/manual/ro/mysqli.query.php, this may help:
if ($result = $mysqli->query($sql)) {
echo $result->num_rows;
/* close result set */
$result->close();
}
If you are using several result-returning queries, close previous results with ->close();

Related

mysqli_connect executing for every host input

<?php
/* die/exit operation*/
mysqli_connect('localhost','root','') or die ('The connection is lost');
echo 'connected';
?>
mysqli_connect or die functions working together fine in case of the both correct and incorrect host names.But no matter what username I am using,it is always showing 'connected'.Can anyone please tell me why it is happening?
I don't think your die statement will ever be reached.
mysql_connect is the alias for mysqli::connect and the object will be made.
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
To get a connection failure:
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}

basic mysql data extraction using php

I've looked all over here. Please be patient as I am new to php and mysql.
I got WAMPP installed & seems to be working OK. I created a simple "test" database from phpMyAdmin and "firsttable" in that. I can do a simple connect using example from w3schools, but trying to select & display data I entered only throws back errors.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Connect
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT reference, firstname, lastname, room FROM firsttable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["reference"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "room:" . $row["room"]. "<br>";
}
} else {
echo "0 results";
}
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
First off, I get a parse error on line 17. The one that reads:
if ($result->num_rows > 0) {
The error says: Trying to get property of non-object.
I tried wrapping the whole php code in tags and saving it as html, but then it appeared that no row data was ever found.
I am able to use very simple code that connects successfully. I can confirm the database is in there, so is the table, and the contents I added to it.
Please, what am I doing wrong?
You need to specify the database when you connect:
$database = 'test';
$conn = mysqli_connect($servername, $username, $password, $database);
where $database is the name of your database (test in this case). MySQL doesn't know which database your table resides in without you telling it.
In addition, you should always include error checking for your database connection (you have two of these, you don't need the last one) as well as any queries. Sans this, you can check your error logs for more information when something fails.

cant show the results of query in php from mysql

Hello I am new at php and mysql and I don't know what is wrong.
I cant show the results from query and the connection with mysql is successfully connected.
I don't use wampserver I just install php,mysql and Apache separately.
Thanks in advance.
Code
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql="select * from `books`;";
$result=mysqli_query($conn,$sql);
if (!$result){
echo "query cannot execute";
};
?>
its only show me "query cannot execute"
You need to pass fourth parameter database name in mysqli_connect()
It would be
$conn = mysqli_connect($servername, $username, $password,"YOUR_DATABASE");
Read http://php.net/manual/en/mysqli.error.php to check error in query.
Read http://php.net/manual/en/mysqli-result.fetch-array.php
To fetch data from query result

Why do I connect to database no matter what

Newish to php. I have been trying to query a database, and I keep getting the exception thrown that the query could not be completed. I checked to make sure I was connecting to the database, and everything looked fine, until I dug deeper. It appears that the my code tells me that I am connecting to the database regardless of what I put in for a password, username, or even if I do not have this data defined. I don't get it. Originally I had the following code in a function, but I put it no its own page to debug:
<?php
echo'this is working so far <br>';
/*$db = 'fake';
$host = 'localhost';
$password = 'wrong';
$user = 'root';
*/
$result = new mysqli($host, $user, $password, $db);
if(!$result){
echo 'did not connect to database';
throw new Exception('Could not connect to database');
}
else{
echo'connected to database';
return $result;
}
It always tells me I am connected to the database..
Because you are mixing Object oriented style with Procedural style To check database connection
Procedural style
<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link);
?>
Object oriented style
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
?>
Read http://php.net/manual/en/mysqli.construct.php
Note:
OO syntax only: If a connection fails an object is still returned. To check if the connection failed then use either the mysqli_connect_error() function or the mysqli->connect_error property as in the preceding examples.
Source
That means if($result) check is always true no matter what. So no, you don't have that database connection but you are verifying it incorrectly leading you to believe you do.
Your check should be
if($result->connect_error)
// no luck
else
// game on
You should check connect_errno property which stores the error code from last connect call.
$mysqli = new mysqli($host, $user, $password, $db);
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}

MySQLi issue: connecting but query not working

so i'm not sure what the issue is but i'm pretty basic with mysql and i'm still learning. However I have been searching on the internet for about two hours now and I cant figure out what i've done wrong.
<?php
$id = 0;
// Create connection
$conn = new mysqli($servername, $dbname, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed" . $conn->connect_error);
}
$user_qry = "SELECT * FROM users WHERE id = $id";
$result = $conn->query($user_qry);
echo("<pre>");
print_r($result);
$conn->close();
?>
I am not receiving any connection error, but i'm
You have to fetch the result after executing the query:
$result = $conn->query($user_qry);
while ($row = $result->fetch_assoc()) {
print_r($row);
}
Regarding comment, stolen from another Stack Overflow post:
$result = $conn->query($user_qry)
if (!$result) {
throw new Exception("Database Error [{$this->database->errno}] {$this->database->error}");
}
In your connection, make sure you have selected a database name
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->select_db('test');
And one more thing, to see what is the error you should enable display_errors = On in php.ini

Categories