Im totally confised by this error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\le\login.php on line 10
Can any one help me? I'm new in php
Code :
<?php
$username = $_POST['username'];
$password = $_POST['password'];
if ($username&&$password)
{
$connect = mysql_connect("localhost", "root", "") or die("Could'nt Connect");
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
mysql_select_db("shit") or die("cant find db");
$numrows = mysql_num_rows($query);
echo $numrows;
} else
die("please enter username and a password");
?>
I suggest you using mysqli to interact with db.
mysql_query is deprecated. See documentation here: http://php.net/manual/en/function.mysql-query.php
To connect with DB in mysqli you can do:
$DBhost = "localhost";
$DBuser = "your-user";
$DBpass = "your-password";
$DBName = "your-db-name";
// Create connection
$mysqli = new mysqli($DBhost, $DBuser, $DBpass, $DBName);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
Related
I am trying to select data from a database. I do have a successful connection, but it seems like the query doesn't work even though I know for sure that the query is right. What am I doing wrong?
If I execute the code below, the result I get is: "Connected successfullyBad query". The 'Bad query' should mean that the query is wrong, but I checked it and it isn't wrong...
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql ="SELECT * FROM `producten`";
$result = mysqli_query($conn, $sql) or die("Bad query");
$conn->close();
?>
I expect to only see "connected successfully"
You are missing your database name. You can do it two ways, or in the connect statement:
$conn = new mysqli($servername, $username, $password,$database);
Or you can do it in your select statement:
$sql ="SELECT * FROM `yourdatabase`.`producten`";
If you don´t set your database your query is wrong
Your query is right just write your database name in mysqli constructor.
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
Visit: https://www.php.net/manual/en/mysqli.construct.php
Please give database name also, check below code.
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = ""; //Enter database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql ="SELECT * FROM `producten`";
$result = mysqli_query($conn, $sql) or die("Bad query");
$conn->close();
?>
I Cannot Check whether the username already exist in database. I gone through existing questions that were answered here. None of them solved my problem. When i executes, it displays "Cannot select username from table", which i given inside die block. Code Is given below.
<?php
$username = $_POST['user_name'];
$password = $_POST['pass_word'];
$host = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "my_db";
//create connection
$conn = #new mysqli($host, $db_username, $db_password, $db_name);
if (isset($_POST["submit"]))
{
# code...
//check connection established or not
if ($conn->connect_error)
{
die("Not Connected to DB");
}
else
{
$query = "SELECT 'usernamedb' FROM 'registration' WHERE usernamedb='$username'";
$result = mysqli_query($conn, $query) or die('Cannot select username from table');
if (mysqli_num_rows($result)>0)
{
$msg.="This username already exist. try Another !!";
}
else
{
$insert = "INSERT INTO 'registration'('id', 'usernamedb', 'password') VALUES ([$username],[$password])";
$insert_result = mysqli_query($conn,$insert) or die('INSERTION ERROR');
}
}
$conn->close();
}
?>
Hope someone will answer me.
First of all you should not use those unescaped queries.
But regarding your question you have an SQL error on your queries. You quoted table name. "FROM 'registration'" should be "FROM registration".
Trying to read the status of a user in my mysql database from php
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Find status for row with Username of the Url?username=
$sql = "SELECT Status FROM Users WHERE Username = '".$_GET["username"]."'";
$result = mysql_query($sql) or die('Error connecting to database');
$username = mysql_result($result, 0, "Status");
echo 'Username Status is ' . $Status;
mysqli_close($conn);
?>
In result I'm getting is this:
Fatal error: Uncaught Error: Call to undefined function mysql_query() in /storage/ssd4/269/2113269/public_html/teststat.php:11 Stack trace: #0 {main} thrown in /storage/ssd4/269/2113269/public_html/teststat.php on line 11
Your syntax for mysqli is not correct, because mysqli is different from mysql syntax.
MySQLi stands for MySQL improved. It's an object-oriented interface to
the MySQL bindings which makes things easier to use.
I suggest you to use the mysqli version and don't mixup mysql syntax with mysqli syntax.
$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 Status FROM Users WHERE Username = '".$_GET["username"]."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row or remove while loop if you wish
while($row = $result->fetch_assoc()) {
echo 'Username Status is ' . $row['Status'] ;
}
} else {
echo "0 results";
}
$conn->close();
You mixed up mysql and mysqli
$result = mysql_query($sql) or die('Error connecting to database');
$username = mysql_result($result, 0, "Status");
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!";
}
?>
Here is a PHP code
php
<?php
$servername = "server";
$username = "user";
$password = "password";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT idbn, infobn, datebn FROM `update`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td><a> ";
echo $row["idbn"];
echo "</a></td><td><a> ";
echo $row["infobn"];
echo "</a></td><td><a>" . $row["datebn"]. "</a></td></tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
But the problem is, it is not retrieving the Bengali data from mysql
mysqli_query('SET CHARACTER SET utf8');
mysqli_query(“SET SESSION collation_connection ='utf8_general_ci'") or die (mysql_error());
I also added this code here. But every time it lost the data. if I remove the UTF8 code then I got ?????. So I need help out of this.
I guess your problem is with utf8 connection to db.
1) make use your db column in utf8_general_ci
2) set mysqli connection to use utf8
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
$mysqli->set_charset("utf8")
Check here: http://php.net/manual/en/mysqli.set-charset.php
This one worked for me. Try this code below.
public function __construct()
{
$hostname = "server_address";
$username = "username";
$password = "password";
$database = "database_name";
$this->link = mysqli_connect($hostname, $username, $password, $database);
mysqli_set_charset($this->link, "utf8");
}