How can I get data from my database to show. I am not very experienced with PHP or MySQL.
I do not get an error message but no data shows so what am I doing wrong?
PHP
<?php
if(strlen(trim($_POST['search'])) > 0) {
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
mysql_connect ("cust-mysql-123-03", "", "");
mysql_select_db ("weezycouk_641290_db1");
if (!empty($_POST["search_string"]))
{
}
$query = "SELECT name,lastname,email FROM contact WHERE name LIKE '%$search%' AND
lastname LIKE '%$searchterm%'";
$result = mysql_query ($query);
echo mysql_error();
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
echo $row["name"];
echo $row["lastname"];
echo $row["email"];
} ?>
<?php echo $row["name"]; ?>
<br>
<?php echo $row["lastname"]; ?>
<br>
<?php echo $row["email"]; ?>
<?php
}
}
?>
It should be like this:
<?php
if(strlen(trim($_POST['search'])) > 0) {
mysql_connect ("cust-mysql-123-03", "", "");
mysql_select_db ("weezycouk_641290_db1");
$query = "SELECT name,lastname,email FROM contact WHERE name LIKE '%" . mysql_real_escape_string($_POST['search']) . "%' AND lastname LIKE '%" . mysql_real_escape_string($_POST['searchstring']) . "%'";
$result = mysql_query ($query);
echo mysql_error();
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
echo $row["name"];
echo $row["lastname"];
echo $row["email"];
} ?>
<?php echo $row["name"]; ?>
<br>
<?php echo $row["lastname"]; ?>
<br>
<?php echo $row["email"]; ?>
<?php
}
}
?>
The mysql_real_escape_string is to prevent mysql injection which is a serious risk.
Make sure the query you are executing returns record(s). You can check this by adding an echo statement which will print the query in your screen. Copy that and run it againist the database.You can use any mysql front end tools(php myadmin,mysqlyog to run the query. If there is any error in the query, you can see that then.
$query = "SELECT name,lastname,email FROM contact WHERE name LIKE '%$search%' AND
lastname LIKE '%$searchterm%'";
//the below line will print the query on the screen
echo $query;
$result = mysql_query ($query);
Related
I'm connected to phpmyadmin but I can not get any data from phpmyadmin.
my php version is 7.2.9 , I made everything that I wanted in database but php can't show the data in site ( I'm using localhost ).
here is the code:
<?php
$key = $_GET['key'];
$terms = explode(" ", $key);
$query = "SELECT * FORM search WHERE ";
foreach ($terms as $each){
$i++;
if($i == 1){
$query .= "keywords LIKE '%$each%' ";
} else{
$query .= "OR keywords LIKE '%$each%' ";
}
echo $query;
}
//connection
mysql_connect("localhost", "root", "");
mysql_select_db('search');
$query = mysqli_query($query);
$numrows = mysqli_num_rows($query);
if($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h2><a href='$link'>$title</h2></a>
$description<br /><br />";
}
}
else{
echo "No results found for \"<b>$key</b>\""; }
//disconnect
mysql_close();
?>
You have a couple of mistakes in your PHP/HTML. I'm gonna sum them up here so you can take a look at them:
<h2><a href='$link'>$title</h2></a>$description<br /><br /> This is wrong HTML. Close your a tag inside the h2.
You are connecting to you database through mysql, but querying through mysqli. Connect to your database with mysqli. Mysql_ family of functions have been removed in PHP 7
You have a typo in your query. you have written FORM instead of FROM.
You are exploding your $_GET variable on spaces. But i doubt if a $_GET variable has any spaces to begin with... Check if this is true.
first of all mysql_connect() is not anymore available after php 5. Instead of using mysql use mysqli_connect(). Please how to make connection and query database using php7 here.
https://www.w3schools.com/php/func_mysqli_fetch_row.asp
If still you have problem. Ask for help in comments.
<?php
$query = "SELECT * FORM search WHERE ";
foreach ($terms as $each){
$i++;
if($i == 1){
$query .= "keywords LIKE '%$each%' ";
} else{
$query .= "OR keywords LIKE '%$each%' ";
}
echo $query;
}
//connection
$conn = mysqli_connect("localhost", "root", "","search");
if(!$conn)die("Connection Error");
$query = mysqli_query($conn,$query);
if(!query)die("query error");
$numrows = mysqli_num_rows($query);
if($numrows > 0){
while ($row = mysqli_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h2><a href='$link'>$title</h2></a>
$description<br /><br />";
}
}
else{
echo "No results found for \"<b>$key</b>\"";
}
You are mixing up mysqli and mysql. I have edited your stuff. Please try. Don't forget to fix the portion marked [missing]
<?php
$key = $_GET['key'];
$terms = explode(" ", $key);
foreach ($terms as $each){
$i++;
if($i == 1){
$query .= "keywords LIKE '%$each%' ";
} else{
$query .= " description OR keywords LIKE '%$each%' ";
}
echo $query;
}
//connection
$conn=mysqli_connect("localhost", "root", "");
mysqli_select_db($conn, 'search');
if($result= mysqli_query($conn, $query)){
$numrows = mysqli_num_rows($result);
}
if($numrows > 0){
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
//echo "<h2><a href='$link'>$title</h2></a>
//$description<br /><br />";
echo '<h2><a href="' . $link . '">' . $title .
'</h2></a>' . $description . '<br /><br />';
}
}
else{
echo "No results found for \"<b>$key</b>\""; }
//disconnect
mysqli_close();
?>
Here's the Full working Example TESTED at my end (Last night I wasn't at my work machine and couldn't test the code.Later I created a small db and Tested it. I had searched with the dummy key 'ram mary albert' in my example
<?php
$key = $_GET['key'];
$terms = explode(" ", $key);
$qu1 = "SELECT * FROM search WHERE ";
$qu2 = "order by id ASC";
$conn=mysqli_connect("localhost", "root", "");
mysqli_select_db($conn, 'search');
for($i=0; $i< count($terms); $i++){
$query = $qu1 . " keywords LIKE '%$terms[$i]%' " . $qu2;
echo( $query . "<br>" );
$resulter= mysqli_query($conn, $query);
while($row = mysqli_fetch_array($resulter)){;
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
$EscLink='\'' . $link . '\'';
echo ('<a href="javascript:void(0)" onClick="alert(' .
$EscLink . ')">' . $title . '</a><br>' . $description .
'<br /><br />');
} // Close While
} // Close for
//disconnect
mysqli_close($conn);
?>
I have this code, i try to call my data from table database mysql , but didn't see any result. always go to else , not go to the process. what would i do?
<?php
require('connectDB.php');
$nama = $_GET['nama'];
echo $nama;
$query = "SELECT * FROM pesan
WHERE nama = '%" . mysqli_real_escape_string($connection, $nama) . "%'
";
$results = mysqli_query($connection, $query);
$baris = mysqli_num_rows($results);
if (!$results) {
die('Invalid query: ' . mysql_error());
}
if ( $baris > 0) {
while($row = mysqli_fetch_assoc($results)) {
?>
<h3>Nama Mobil : <?php echo $row['mobil'] ?></h3>
<h3>ID Pembelian : <?php echo $row['id']; ?></h3>
<h3>Nama anda : <?php echo $row['nama']; ?></h3>
<h3>Alamat : <?php echo $row['alamat']; ?></h3>
<h3>Tanggal Masuk : <?php echo $row['tgl_masuk']; ?></h3>
<?php
}
}else{
echo "error";
}
?>
What wrong with my code?
Thanks!
im sorry , this is my ConnectDB.php , i include in my html.
<?php
$connection = mysqli_connect('localhost', 'root', '', 'dealermobil');
if (!$connection){
die("Database Connection Failed" . mysqli_error());
}
// $db = new PDO ('mysql:host=localhost;dbname=db_login;charset=utf8mb4','root','');
?>
Your connection check and result check is incorrect.
$connection = mysqli_connect('localhost', 'root', '', 'dealermobil');
if (!$connection){
die("Database Connection Failed" . mysqli_connect_error());
}
Also
$results = mysqli_query($connection, $query);
$baris = mysqli_num_rows($results);
if (!$results) {
die('Invalid query: ' . mysqli_error($connection));
}
use this you are using = it should be LIKE when you are trying to search a field in database.
$query = "SELECT * FROM `pesan` WHERE `nama` LIKE '%". mysqli_real_escape_string($connection, $nama) ."%'";
Hi I am trying to do a Registration that the users will put their name password and their answers to some questions and then an admin will manually answer to it if it's accepted.I did the system that loads their name password and answers in the database,and I also ran the things that will show the answers to the admin,but I can't figure a way to change a value just for one user not for all of them,I will leave you my codes and everything over here.
Here is my admin.viewapplications.php code
(Here,it shows everything fine,but I can't figure a way that the button to act just for one id not for all)
<?php
//include(__DIR__ . "/signup.php");
include("../resources/config.php");
//$name = $_POST['Name'];
//$mg = $_POST['MG'];
//$pg = $_POST['PG'];
//$rk = $_POST['RK'];
$sql = "SELECT id, name, tutorial, MG, PG, RK FROM rp_users WHERE tutorial = 2";
//$tutorial = "SELECT tutorial FROM rp_users";
$result = mysql_query($sql);
//$result2 = mysql_query($tutorial);
//$value = mysql_fetch_object($result2)
/*if($result)
{
echo "Succes";
}
else
{
die(mysql_error());
}*/
//if($value > 1)
//
while($row = mysql_fetch_array($result))
{
//$tutorial = row["tutorial"];
//f($tutorial == 2)
//}
$id = $row["id"];
$name = $row["name"];
$mg = $row["MG"];
$pg = $row["PG"];
$rk = $row["RK"];
echo "ID: " . $id."<br> <br>";
echo "Nume: " . $name."<br> <br>";
echo "MG: " . $mg."<br> <br>";
echo "PG: " . $pg."<br> <br>";
echo "RK: " . $rk."<br> <br>";
echo '<form action="./?p=applicationaccept" method="POST">';
echo '<input type="submit" name="accept" value="Accepta">';
echo '</form><br>';
echo '<form action="./?p=applicationdeny" method="POST">';
echo '<input type="submit" name="deny" value="Respinge">';
echo '</form><br> <br> <br>';
}
//}
//
?>
And here is my applicationaccept.php
<?php
include("../admin/admin.viewapplications.php");
include("../resources/config.php");
$iduser = $id;
$sql = "UPDATE rp_users SET tutorial=0";
$result = mysql_query($sql);
if($result)
{
echo "Succes";
}
else
{
die(mysql_error());
}
/*while($row = mysql_fetch_array($result))
{
}*/
?>
I think what you want to do is a simple UPDATE to your MySQL database..
but make sure you format the PHP code you're using otherwise it'll give you an ERROR!
Also you have to use 'mysqli' now in PHP!
<?php
$someID = '1';
$sql = "UPDATE `rp_users` SET `tutorial`= '0' WHERE `id` = $someID";
$result = mysqli_query($link, $sql);
if($result)
{
echo "Success";
}
else
{
echo ("Error");
}
?>
BTW I forgot to mntion the '$link' is the connection to your database!
As of my understanding of your question if your form action is applicationaccept.php and you are trying to update for one user in applicationaccept.php file, try this:
<?php
include("../admin/admin.viewapplications.php");
include("../resources/config.php");
$iduser = $_POST["id"]; // pass id as parameter in form
$sql = "UPDATE rp_users SET tutorial=0";// change this line to following line
$sql = "UPDATE rp_users SET tutorial=0 where id=$iduser";
$result = mysql_query($sql);
if($result)
{
echo "Succes";
}
else
{
die(mysql_error());
}
?>
Be aware your code is vulnerable
I'm trying to learn php and mysql, I was trying to read data from my database where i was following something online and encounter an error(s) here is my code
<?php
include 'includes/conn.php';
$query =sprintf("Select * from customer");
$result = mysql_query($query);
if (!$result)
{
$message ='from you see this then it's not connecting!'.mysql_error() ."\n";
$message .= 'everything' .$query;
die($message);
}
while ($customer = mysql_fetch_assoc($result))
{
echo $row['cust_id'];
echo $row['fname'];
echo $row['lname'];
echo $row['gender'];
echo $row['dob'];
}
mysql_free_result($result);
?>
//this is where i was trying to make a connection with the database
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db ='telmar_php';
$con = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
?>
Replace
$message ='from you see this then it's not connecting!'.mysql_error() ."\n";
here your string is single quotation marks in it's is causing the problem
with
$message ="from you see this then it's not connecting!".mysql_error() ."\n";
...and to display your results change this:
while ($customer = mysql_fetch_assoc($result))
to this:
while ($row = mysql_fetch_assoc($result))
Please review the below example for reading data my mysql using php.
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
I am trying to make the different the different rows have line breaks but its not working.
How is this done!? Please check my code below
Thanks guys!
James
<?php
$conn = mysql_connect("", "", "");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
{
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
}
if (!mysql_select_db("")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT name,lastname,email
FROM test_mysql
WHERE name LIKE '$search%' AND lastname LIKE '$searchterm'";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["name"];
echo $row["lastname"];
echo $row["email"];
}
mysql_free_result($result);
?>
<?php echo $row["name"];?>
<br>
<?php echo $row["lastname"];?>
<br>
<?php echo $row["email"];?>
Beats me what you find so hard about it:
while ($row = mysql_fetch_array(...)) {
echo ...
echo '<br>';
}