I've been trying for days to create a simple search engine to search for information in my database. The table is myisam and I get search results when using mysql to search directly in phpmyadmin. So the problem seams to be with the PHP.
When searching all I get is an empty page. I've tried serveral varieations of code I've found in online tutorials, but nothing seems to work. I hope there is a simple solution that I'm too dumb to see, and I hope someone can explain to me how to do this.
if(!empty($_POST['search'])){
$search = $_POST['search'];
$sqlString = "SELECT * FROM test WHERE MATCH (title, about) AGAINST ('$search')";
$result = mysqli_query($dbLink, $sqlString) or die("Could not search.." . mysqli_error($dbLink));
$row = mysqli_fetch_assoc($result);
if($result-> num_rows > 0){
while($row = mysqli_fetch_assoc($result)){
$title = $row['title'];
echo $title;
}
}else{
echo 'No results';
}
}
if(!empty($_POST['search'])){
$search = $_POST['search'];
$sqlString = "SELECT * FROM test WHERE MATCH (title, about) AGAINST ('$search')";
$result = mysqli_query($dbLink, $sqlString) or die("Could not search.." . mysqli_error($dbLink));
$row = mysqli_fetch_assoc($result); // remove it
if($result-> num_rows > 0){
while($row = mysqli_fetch_assoc($result)){ // already exist
$title = $row['title'];
echo $title;
}
}else{
echo 'No results';
}
}
Change
if($result-> num_rows > 0)
To
if(mysqli_num_rows($result) > 0)
Related
I have a sql database that when a key is passed from the web page input box back to the sql database (server)
this generates the encoded key and passes it back to the web page.
If the wrong key is sent then it returns wrong key. If it is correct it will return the generated key.
This works perfectly.
However when i try to add an additional check in the name of payment 1 for true 0 for false i can not combine the two. Meaning i would like both conditions to be met to make this work. Here is the code that works.
$sql = "SELECT decrypt_key FROM keys WHERE software_key=" . "'" . $software_key_encoded . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["decrypt_key"];
}
} else {
echo "Failed";
}
$conn->close();
}
else {
echo "Failed";
}
followed by what i am trying to do, This throws the error of
Trying to get property of non-object in /---/ Failed
$sql = "SELECT decrypt_key FROM keys WHERE software_key=" . "'" . $software_key_encoded . "'";
$eql = "SELECT payment FROM new_keys WHERE payment =1";
$result = $conn->query($sql) && $result1 = $conn->query($eql);
if ($result->num_rows > 0 && $result1->num_rows > 1) {
while($row = $result->fetch_assoc()) {
echo $row["decrypt_key"];
#echo $row["payment"];
}
} else {
echo "Failed";
}
$conn->close();
}
else {
echo "Failed";
}
$result = $conn->query($sql) && $result1 = $conn->query($eql);
change this line to:
$result = $conn->query($sql);
$result1 = $conn->query($eql);
and in if statement change $result1->num_rows > 1 to $result1->num_rows > 0;
Split the result variables in 2 different statements like below
$result = $conn->query($sql);
$result1 = $conn->query($eql);
If you still face any issue please post the error message too.
I wrote simple code to fetch data from MySQL using PHP.
This is the code:
<?php
$mangkal = $_POST['mangkal'];
$lat = $_POST['lat'];
$long = $_POST['long'];
$mysqli = new mysqli("localhost", "root", "", "mad");
$query = "SELECT * FROM kendaraan WHERE mangkal LIKE '%$mangkal%' ORDER BY id DESC";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_BOTH);
{
echo "<p>";
echo "$row[id_kendaraan]";
echo "<p>";
echo "$row[mangkal]";
}
?>
The script is working, if the data is returned by the db call, I can see the results displayed. But, if the query result has no data, the script just displays blank. I want to show a message that says - 'Data not found'. How can I do that?
I have more than one record for a query but the script displays just one data. Please help me to show all records.
Use a while loop like following:
if($result->num_rows > 0)
{
while($row = $result->fetch_array(MYSQLI_BOTH))
{
echo "<p>".$row[id_kendaraan]."</p><br><p>".$row[mangkal]."</p>";
}
} else {
echo "No Record Found.";
}
You need to run a loop to iterate through each result. Something similar to this -
<?php
$mangkal = $_POST['mangkal'];
$lat = $_POST['lat'];
$long = $_POST['long'];
$mysqli = new mysqli("localhost", "root", "", "mad");
$query = "SELECT * FROM kendaraan WHERE mangkal LIKE '%$mangkal%' ORDER BY id DESC";
$result = $mysqli->query($query);
while($row = $result->fetch_array(MYSQLI_BOTH)) {
echo "<p>";
echo "$row[id_kendaraan]";
echo "</p><p>";
echo "$row[mangkal]";
echo "</p>";
}
?>
In the code above, a while loop is used to iterate through results one by one. Each time $row will be updated with new data and will be printed.
I would like to see where my code is incorrect. I want to store values from my database as a php array. Then I'd like to store the individual parts of the array as separate variables. Here is my code:
<?php
$result = mysqli_query($db, "SELECT column FROM table");
if (!$result) {
echo 'Could not run query';
exit;
}
$comments = mysqli_fetch_row($result);
$comment0 = $comments[0];
$comment1 = $comments[1];
$comment2 = $comments[2];
$comment3 = $comments[3];
$comment4 = $comments[4];
$comment5 = $comments[5];
$comment6 = $comments[6];
$comment7 = $comments[7];
$comment8 = $comments[8];
$comment9 = $comments[9];
?>
This will run your mysql query and add each comment to an array of comments, then print the array.
<?php
$result = mysqli_query($db, "SELECT column FROM table");
if (!$result) {
echo 'Could not run query';
exit;
}
$comments = array();
while($comment = mysqli_fetch_row($result)){
$comments[] = $comment;
}
print_r($comments);
?>
not sure if you are in console or through web server.
<?php
$result = mysqli_query($db, "SELECT column FROM table");
if (!$result) {
echo 'Could not run query';
exit;
}
$comments = mysqli_fetch_row($result);
foreach($comments as $comment){
echo print_r($comment,1).'--------------\r\n<br>\r\n';
}
?>
this is called a loop. loop is your friend.
So I have a database table, named todolist, and I want to display the whole to do list in a box. What I have right now is this:
$db = (INFO HERE :));
$sql = "SELECT * FROM todolist";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) == 0) {
echo "There is nothing else to do! :)";
} else {
}
What do I put in the else, for it to display everything on a list? Thanks
try something like below:
<?php
$db = (INFO HERE :));
$sql = "SELECT * FROM todolist";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) == 0) {
echo "There is nothing else to do! :)";
}
else{
?>
<ul>
<?php
while($row = mysqli_fetch_assoc($result)){
?>
<li><?php echo $row["yourColumnName"] ?></li><?php
}?>
</ul><?php
}
?>
$sql = "SELECT * FROM todolist";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) == 0) {
echo "There is nothing else to do! :)";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
// To see all data
// print_r($row);
// to print single column value
//echo $row['id];
}
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
//Do something here with $row array , for example print_r
print_r($row);
}
I am trying to create a PHP loop to show the players the tournaments they are partaking in. I tried the function on phpMyAdmin, and I got the result that I wanted. But when I try to run this simple script on PHP, it outputs as 0 rows.
index.php
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
if($mybb->user['uid']) {
$sql = "SELECT * FROM players, tourneys WHERE players.forumname = 1 AND players.tid = tourneys.id";
$result = mysqli_query($conn, $sql);
// output data of each row
if (mysqli_num_rows($result) > 0) {
// output data of each ro
while($row = mysqli_fetch_assoc($result)) {
$name = $row['name'];
$date = $row['date'];
$time = $row['time'];
echo $name;
echo $date;
echo $time;
}
} else {
echo "0 rows";
}
}
The problem is not in "if ($mybb->user['uid']) {" as suggested by #Dagon. Try to leave the well indented code to avoid this kind of confusion. Run the code below and post the the DB error output.
Try this:
<?php
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
if ($mybb->user['uid']) {
$player_forumname = '1';
// Return only the necessary fields
$sql = "SELECT tourneys.name, tourneys.date, tourneys.time
FROM tourneys
INNER JOIN players ON players.tid = tourneys.id
WHERE players.forumname = {$player_forumname}";
$result = mysqli_query($conn, $sql);
// If the query fails display the error
if ($result === FALSE) {
die('MySQLi Error: ' . mysqli_error($conn));
}
if (mysqli_num_rows($result) == 0) {
die('0 rows');
}
// Returns only the associative array
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$name = $row['name'];
$date = $row['date'];
$time = $row['time'];
echo 'Name:',$row['name'],'Date:',$row['date'],'Time:',$row['time'];
}
}