2

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','');
?>

12
  • Where is $connection defined? Try printing '$connection' variable. Commented Jul 9, 2017 at 9:44
  • ... WHERE name = '%" ...? It should be ... WHERE name LIKE '%" ... Commented Jul 9, 2017 at 9:46
  • Still Error @RajdeepPaul Commented Jul 9, 2017 at 9:47
  • Im sorry , i just editted now. See Again @user3025122 Commented Jul 9, 2017 at 9:50
  • mysql_error() ? You're mixing mysql and mysqli APIs. RTM, http://php.net/manual/en/mysqli.error.php. Also, check how many rows are returned from the SELECT query, do var_dump($baris);. Commented Jul 9, 2017 at 9:51

2 Answers 2

1

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));
 }
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer Mr. Amir , but i got answer by myself. can i ask how to connect a table database to b table database (ex mobil table to pesan table)
You should use join command SELECT ... FROM table1 JOIN table2 on ... WHERE ... please refer to documents
0

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) ."%'";

1 Comment

i try it but no result mr. Kunal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.