1

I need your help about the php that i create. it got a warning and, i dont know where the mistake is.

This is the warning that i get:

Warning: mysql_query(): Access denied for user ''@'localhost' (using password: NO) in /home/dweetcom/public_html/admin_kepuasan.php on line 10

Warning: mysql_query(): A link to the server could not be established in /home/dweetcom/public_html/admin_kepuasan.php on line 10

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/dweetcom/public_html/admin_kepuasan.php on line 27

This is my php file:

admin_kepuasan.php

  <?php
include("connection1.php");
?>
<html>
<link rel="stylesheet" type="text/css" href='https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css'/>

<?php

$sql="select * from borang_kepuasan";
$result=mysql_query($sql);

echo "<table id='example' class='display' cellspacing='1' width='100%'>";
echo "<thead>";
echo "<tr>";
echo "<td>Soalan 1</td>";
echo "<td>Soalan 2</td>";
echo "<td>Soalan 3</td>";
echo "<td>Soalan 4</td>";
echo "<td>Soalan 5</td>";
echo "<td>Soalan 6</td>";
echo "<td>Soalan 7</td>";
echo "<td>      Nota / Testimoni       </td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";

while ($row = mysql_fetch_assoc($result)) {
    echo "<tr>";
    echo "<td>$row[soalan1]</td>";
    echo "<td>$row[soalan2]</td>";
    echo "<td>$row[soalan3]</td>";
    echo "<td>$row[soalan4]</td>";
    echo "<td>$row[soalan5]</td>";
    echo "<td>$row[soalan6]</td>";
    echo "<td>$row[soalan7]</td>";
    echo "<td>$row[nota]</td>"; 
    echo "</tr>";
}
echo "</tbody>";
echo "</table>";
 echo "<center>";
 echo "<table>";
 echo "<tr>";
 echo "<td>";
     echo "<button style=height:30px width:80px value=Kembali onClick=location.href='admin1.php'>";
     echo "Kembali";
     echo "</button>";
 echo "</td>";
 echo "<td>";
     echo "<button style=height:30px width:80px value=Log Out onClick=location.href='logout.php'>";
     echo "Log Out";
     echo "</a>";
     echo "</button>";
echo "</center>";
?>
</html>
<script src='//code.jquery.com/jquery-1.12.4.js'></script>
<script src='https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js'></script>
<script>
$('#example').DataTable();
</script>

connection1.php

<?php
$servername = "localhost";
$username = "dweetcom";
$password = "8weF5yaMow";

// Create connection
$conn = new mysqli($servername, $username, $password)  or die ("cannot connected");

@mysql_select_db("dweetcom_borang",$conn);
?>
Akshay
  • 700
  • 9
  • 23
Emma
  • 7
  • 4
  • You have connected SQL Connection using MySQLi while querying in admin_kepuasan.php, you are writing mysql_query, it should be mysqli_query. – Naveed Ramzan Jan 18 '17 at 04:35
  • Duplicate of http://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc-mysqli-fetch-row-expects-parameter-1 . and by the way why didn't you ___mask___ your password ? –  Jan 18 '17 at 04:36
  • Yes Agreed, but in admin_kepuasan.php, he is using mysql_query while it should be mysqli_query function. – Naveed Ramzan Jan 18 '17 at 04:38
  • @adelowo actually that password is fake, i'm not using the real password of mine – Emma Jan 18 '17 at 04:42
  • @NaveedRamzan i did figure out, that was why i deleted the comment –  Jan 18 '17 at 04:47
  • Ah ok right. Good luck – Naveed Ramzan Jan 18 '17 at 04:48

4 Answers4

0
$result=mysqli_query($conn, $sql);

In admin_kepuasan.php file, you need above change.

First parameter is the connection variable (which you created in connection file)

Second is the query string.

Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30
  • i have change it but then other warning come out : Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/dweetcom/public_html/admin_kepuasan.php on line 10 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in /home/dweetcom/public_html/admin_kepuasan.php on line 27 – Emma Jan 18 '17 at 04:39
  • Ah ok, you need pass connection variable too. – Naveed Ramzan Jan 18 '17 at 04:40
  • what do you mean ,i need pass connection variable? sorry i'm not good in this php subject – Emma Jan 18 '17 at 07:01
  • Well, you can pass connection manually or maybe define $conn as global then you can use in whole application. – Naveed Ramzan Jan 18 '17 at 07:12
0

Use below code

$conn = new mysqli($servername, $username, $password)  or die ("cannot connected");
mysqli_query($conn,"select * from borang_kepuasan");
Nishant Nair
  • 1,999
  • 1
  • 13
  • 18
0

Try this :

$con = mysqli_connect("localhost","dweetcom","8weF5yaMow","dweetcom_borang");

$query = "Enter your query";

mysqli_query($con,$query);
Sujal Patel
  • 2,444
  • 1
  • 19
  • 38
0

Try this code: <----admin_kepuasan.php---->.

    <?php
require_once 'connection1.php';
?>
<html>
<link rel="stylesheet" type="text/css" href='https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css'/>

<?php

$sql="select * from borang_kepuasan";
$result=mysqli_query($conn,$sql);

echo "<table id='example' class='display' cellspacing='1' width='100%'>";
echo "<thead>";
echo "<tr>";
echo "<td>Soalan 1</td>";
echo "<td>Soalan 2</td>";
echo "<td>Soalan 3</td>";
echo "<td>Soalan 4</td>";
echo "<td>Soalan 5</td>";
echo "<td>Soalan 6</td>";
echo "<td>Soalan 7</td>";
echo "<td>      Nota / Testimoni       </td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr>";
        echo "<td>$row[soalan1]</td>";
        echo "<td>$row[soalan2]</td>";
        echo "<td>$row[soalan3]</td>";
        echo "<td>$row[soalan4]</td>";
        echo "<td>$row[soalan5]</td>";
        echo "<td>$row[soalan6]</td>";
        echo "<td>$row[soalan7]</td>";
        echo "<td>$row[nota]</td>"; 
        echo "</tr>";
    }
} else {
    echo "0 results";
}
echo "</tbody>";
echo "</table>";
 echo "<center>";
 echo "<table>";
 echo "<tr>";
 echo "<td>";
     echo "<button style=height:30px width:80px value=Kembali onClick=location.href='admin1.php'>";
     echo "Kembali";
     echo "</button>";
 echo "</td>";
 echo "<td>";
     echo "<button style=height:30px width:80px value=Log Out onClick=location.href='logout.php'>";
     echo "Log Out";
     echo "</a>";
     echo "</button>";
echo "</center>";
?>
</html>
<script src='//code.jquery.com/jquery-1.12.4.js'></script>
<script src='https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js'></script>
<script>
$('#example').DataTable();
</script>

And this is the connection. <----connection1.php----->

<?php
$servername = "localhost";
$username = "dweetcom";
$password = "8weF5yaMow";

// Create connection
$conn = new mysqli($servername, $username, $password) ;
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

@mysqli_select_db("dweetcom_borang",$conn);
?>

Also don't mix MySQL and MySQLi syntax. MySQL has been depreciated from PHP 7 versions.

Adi
  • 186
  • 9
  • 1 warning i get : Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/dweetcom/public_html/admin_kepuasan.php on line 27 – Emma Jan 18 '17 at 06:45
  • @Emma $result=mysqli_query($conn,$sql); or die(mysqli_error($conn)); Try putting this below your query. It seems that there is an error in your query. – Adi Jan 18 '17 at 06:50
  • Yes put that under it. – Adi Jan 18 '17 at 07:02
  • hmm.. it come out this :--> Parse error: syntax error, unexpected 'or' (T_LOGICAL_OR) in /home/dweetcom/public_html/admin_kepuasan.php on line 10 – Emma Jan 18 '17 at 07:08
  • I have given you the connection properly. The issue is with the logic used to create the HTML table with database values. You refer topics related to table creation with database values. Your connection issue is resolved, its working fine. Otherwise you have to put a new question for creating a table in html and database values. Stackoverflow has some policies regarding the questions asked. – Adi Jan 18 '17 at 08:48