I have looked in every possible forum and question page and still cant understand why my sql search is not returning any values. It is written in a PHP script which is designed to search for the name that user has entered on the HTML page and return all the columns in the database.
Please note that i am fairly new to learning PHP and SQl so please dont hate if this is just me being stupid haha.
Thanks in advance,
Chris
<!DOCTYPE html>
<html>
<body>
<?php
$ServerName = "****";
$LogInUN = "*****";
$LogInPW = "******";
$DBName = "********";
$Connection = mysqli_connect($ServerName, $LogInUN, $LogInPW, $DBName);
if(!$Connection){
die("<p>Connection error!</p>" . mysqli_connect_error());
}
$Name = mysqli_real_escape_string($Connection, $_POST['Name']);
$Query = "SELECT Firstname FROM Students WHERE Firstname LIKE '%{%Name}%'";
$result = $Connection->query($Query);
echo $result;
echo $Name;
?>
</body>
</html>
Use $Name
$Query = "SELECT Firstname FROM Students WHERE Firstname LIKE '%{$Name}%'";
After $result = $Connection->query($Query); Line write below lines
while($row = mysqli_fetch_assoc($result)) {
echo $row["Firstname"];
echo $Name;
}
Remove $result = $Connection->query($Query); and use
$result = mysqli_query($Connection, $Query);
$Query = "SELECT Firstname FROM Students WHERE Firstname LIKE '%{%Name}%'";
use this
$Query = "SELECT Firstname FROM Students WHERE Firstname LIKE '%{$Name}%'";
Related
This code only shows "1 User(s) Online" no matter how many are online. How do I fix that?
<?php
$con = mysqli_connect($host, $username, $password , $database)
or die('Error connecting to MySQL server.');
$online = "1";
$query = "SELECT * FROM `users` WHERE online = '$online'";
$data = mysqli_query($con, $query);
$row = mysqli_fetch_array($data);
$online=$row['online'];
echo '<div id="online-me" class="mydiv3"><center><span id="stats">'.$online.' User(s) Online!</span> </center></div>';
?>
use the count() of MySql
also use mysqli_fetch_assoc instead of mysqli_fetch_array
something like this
<?php
$con = mysqli_connect($host, $username, $password , $database)
or die('Error connecting to MySQL server.');
$online = "1";
$query = "SELECT count(id) as 'total' FROM `users` WHERE online = '$online'";
$data = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($data);
$online=$row['total'];
echo '<div id="online-me" class="mydiv3"><center><span id="stats">'.$online.' User(s) Online!</span> </center></div>';
?>
So with
$query = "SELECT * FROM `users` WHERE online = '$online'";
You are getting the row where online = 1. Then you set $online=$row['online']; which just sets $online to 1 no matter what.
That should give you an idea of how to fix it.
Cheers! Good luck!
I need only to display the last values in the row. Now its displaying
Message for: Rocha : gff
Message for: Rocha :
Message for: Rocha : hi my name is kenny
I only need it to display Message for: Rocha : hi my name is kenny.
Thank you
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "company";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, className, lastname, messages FROM Mymesages";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if("CPS210-CompSci-I (4)"==$row["className"] && $lastname== $row["lastname"]){
echo "Message for: " . $row["lastname"]. " : " . $row["messages"]. "<br>";
}
}
}
$conn->close();
?>
If you're looking for only one record, that too the last one, you just need to modify your query a little. Also, there's no need for the loop in that case.
$sql = "SELECT id, className, lastname, messages FROM Mymesages ORDER BY id DESC LIMIT 1";
Replace this line:
while($row = $result->fetch_assoc()) {
With simply:
$row = $result->fetch_assoc();
If you want to display the last row, then your query should be like this:
$sql = "SELECT id, className, lastname, messages FROM Mymesages ORDER BY id DESC LIMIT 1";
And later, instead of while loop simply fetch the row like this:
$row = $result->fetch_assoc();
This question already exists:
PHP KEYWORD SEARCH ENGINE NO RESULTS [duplicate]
Closed 7 years ago.
I have a simple MySQL database keyword search that is functional. However results for the search are not being returned if the keywords are not in the same order as entered in the database. For example searching for "dog cat lion" will return a result, but searching for "dog lion cat" will return no results.
Any help on how to fix this issue would be greatly appreciated. Here is my code.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Part Search</title>
</head>
<body>
<?php
$username = "xxxx";
$password = "xxxx";
$hostname = "localhost";
//connection to the database
mysql_connect($hostname, $username, $password);
mysql_select_db("wesco");
$search = mysql_real_escape_string(trim($_POST['searchterm']));
$find_parts = mysql_query("SELECT * FROM `parts` WHERE `keywords` LIKE '%$search%'");
while($row = mysql_fetch_assoc($find_parts))
{
$name = $row['name'];
echo "$name<br />";
}
?>
</body>
</html>
You could use something like this
$searchArray = explode(" ", $_POST['searchterm']);
$query = "";
foreach($searchArray as $val) {
$search = mysql_real_escape_string(trim($val));
if (!empty($query)) {
$query = $query . " OR "; // or AND, depends on what you want
}
$query = $query . "`keywords` LIKE '%$search%'";
}
if (!empty($query)) {
$find_parts = mysql_query("SELECT * FROM `parts` WHERE $query");
}
Also, you should stop using the mysql_* functions and start using the object oriented implementation of mysqli.
You need to order your Query.
if you have an auto_incremented Id you could go:
$find_parts = mysql_query("SELECT * FROM `parts` WHERE `keywords` LIKE '%$search%' ORDER BY row_id asc");
you can order by Name or really anything:
$find_parts = mysql_query("SELECT * FROM `parts` WHERE `keywords` LIKE '%$search%' ORDER BY name asc");
Two options:
use the build in mysql search functionality.
https://dev.mysql.com/doc/refman/5.6/en/innodb-fulltext-index.html
this is probably the better solution.
if you want to continue to use LIKE, then split the words as follows.
select *
from parts
where
(
keywords LIKE '%$word1%'
AND
keywords LIKE '%$word2%'
AND
keywords LIKE '%$word3%'
)
Im new to programming and I'm trying to create a login form using php/mysql and I have faced some problems. I cannot understand why my sql code cannot being executed, Im using localhost. Could you guys help me?
Thanks
Here is the code that i wrote
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "login";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
if (isset($_POST['username'])){
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username'".$username."' AND password='".$password."' LIMIT 1";
$res = mysql_query($sql);
if (mysql_num_rows($res) == 1){
echo "You have successfully logged in.";
exit();
}else{
echo "Invalid password information.";
exit();
}
}
?>
I think you forgot =
$sql = "SELECT * FROM users WHERE username='".$username."' AND password='".$password."' LIMIT 1";
Simple Typo:
Change:
$sql = "SELECT * FROM users WHERE username'".$username."' AND password='".$password."' LIMIT 1";
To:
$sql = "SELECT * FROM users WHERE username = '".$username."' AND password='".$password."' LIMIT 1";
Another thing, you don't need to fetch all fields from database table if you don't need them.
This slows down speed of page load.
You can fetch only id field.
You are fetching number of rows, so, your query should be:
SELECT id FROM users WHERE username ...
I want to count the rows in the users table with specific name and pwd which should be 1 if existed.
but the result always return null(not 0),no matter whether the user existed or not.
I even change the query simple to "SELECT * FROM users", and it ended with the same result.
And I am pretty sure that the name of the DATABASE and TABLE are true,and the table is not empty!
By the way,why I have to use "#" symbol before "mysqli_query" in order to get rid of error?
thx!
enter code here
<?php
#$mysql_db_hostname = "localhost";
$mysql_db_hostname = "127.0.0.1";
$mysql_db_user = "root";
$mysql_db_password = "";
$mysql_db_database = "smartFSUsers";
$con = mysqli_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password,$mysql_db_database);
if (!$con) {
trigger_error('Could not connect to MySQL: ' . mysqli_connect_error());
}
$name = $_GET["name"];
$password = $_GET["password"];
$query = "SELECT * FROM users WHERE name='$name' AND password='$password'";
$result =#mysqli_query($query,$con);
echo($result);
$row=#mysqli_num_rows($result);
echo"the row num is $row \n";
?>
RTM: http://php.net/mysqli_query
$result =#mysqli_query($query,$con);
You've got your parameters reversed. $con MUST come first:
$result = mysqli_query($con, $query) or die(mysqli_error());
If you had bothered adding error correction to your code, you'd have been told about this. But nope, you opted for # to hide all those error messages.