I want to get the data from my database. The page does not change when I upload the file. Where am I wrong?
verifycheck.php
<?php
$con=mysql_connect ("###", "###", "###");
mysql_select_db ("db_name", $con);
$result = mysql_query($con,"SELECT * FROM db_tablename");
echo "<table border='1'>
<tr>
<th>username</th>
<th>email</th>
<th>password</th>
<th>confirm_password</th>
</tr>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['confirm_password'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Connection object should be second parameter and query string should be first parameter.
Try this
$result = mysql_query("SELECT * FROM db_tablename",$con);
Instead of
$result = mysql_query($con,"SELECT * FROM db_tablename");
Mysql function is deprecated and will remove in future go for Mysqli or PDO for preventing sql injection
Remove $con, from the mysql_query
As mysql_query() expects first parameter to be the SQL Query, not the SQL connection.
$result = mysql_query("SELECT * FROM db_tablename");
Please Check with your connection parameters as localhost,username password, database and also changing the
$result = mysql_query($con,"SELECT * FROM db_tablename");
as
$result = mysql_query("SELECT * FROM db_tablename",$con);
Related
I want to show all data from my table.
But if I use/add ORDER BY id DESC or any code after $sql="SELECT * FROM $tbl_name, then last row is not showing.
<?php
include "db.php";
$tbl_name="report"; // Table name
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$ro = mysql_fetch_array($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count>=1) {
echo "<table border='1' align='center' cellpadding='10'>
<tr>
<th>Reporter</th>
<th>Message</th>
<th>Reporter Ip Address</th>
<th>Action</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['from'] . "</td>";
echo "<td>" . $row['msg'] . "</td>";
echo "<td>" . $row['to'] . "</td>";
echo "<td class='middle'>" . $row['ip'] . "</td>";
echo "<td><a class=\"confirmation\" href=\"report_delete.php?id=" . $row['id'] . "\">Delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
else {
print "<p align='center'>Nothing found.</p>";
}
?>
Of course when you used the DESC, it starts off the highest ID. Then the invocation of:
$ro = mysql_fetch_array($result); // this is the first row.
It fetches the first row.
Then your loop: while($row = mysql_fetch_array($result)) starts off with the second row.
So just remove this $ro = mysql_fetch_array($result); unneeded fetching line.
Obligatory Note:
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Sample PDO Usage:
<?php
$db = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');
$query = $db->query('SELECT * FROM report ORDER BY id DESC');
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
if(count($rows) > 0) {
echo "
<table border='1' align='center' cellpadding='10'>
<tr>
<th>Reporter</th>
<th>Message</th>
<th>Reporter Ip Address</th>
<th>Action</th>
</tr>
";
foreach($rows as $row) {
echo "<tr>";
echo "<td>" . $row['from'] . "</td>";
echo "<td>" . $row['msg'] . "</td>";
echo "<td>" . $row['to'] . "</td>";
echo "<td class='middle'>" . $row['ip'] . "</td>";
echo "<td><a class=\"confirmation\" href=\"report_delete.php?id=" . $row['id'] . "\">Delete</a></td>";
echo "</tr>";
}
echo '</table>';
} else {
echo "<p align='center'>Nothing found.</p>";
}
?>
You have an extra mysql_fetch_array($result); before the loop.
You must generete the Sql query String correctly. Like this:
$sql = "SELECT * FROM ".$tbl_name." ORDER BY id DESC";
In Php there are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side.
More info -> http://php.net/manual/en/language.operators.string.php
Hope it helps.
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','my_db');
if (mysqli_connect_errno()) {
echo "Fail to connect :".mysqli_connect_error();
}
mysqli_select_db($con,"my_db");
$sql="SELECT * FROM ajax WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row('Firstname') . "</td>";
echo "<td>" . $row('Lastname') . "</td>";
echo "<td>" . $row('AGE') . "</td>";
echo "<td>" . $row('Hometown') . "</td>";
echo "<td>" . $row('Job') . "</td>";
echo "</tr>";
echo "</table>";
}
mysqli_close($con);
I keep getting this error :Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\abc\getuser.php on line 22
I apologize in advance if there is already an answer to this simple problem, I am new at coding and I have spent quite some time reading other similar questions but still couldn't solve this problem. Thx in advance.
As specified, the argument $result is boolean, not a mysqli_result.
In case of error in your SQL request, the function mysqli_query returns false.
You should see why their is a problem with your request by calling mysqli_error just after your mysqli_query if $result is false.
you could try
$sql=mysqli_query("SELECT * FROM ajax WHERE id = '".$q."' ");
echo "<table border='1'>
.......
while($row = mysqli_fetch_array($sql)) {
I need to filter a session table, normally this is how I show the table list.
<?php
$con=mysqli_connect("localhost","root","","esc");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM sponsor ;");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>PIN</th>
<th>Author</th>
<th>Author ID</th>
<th>Entry Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['sid'] . "</td>";
echo "<td>" . $row['spin'] . "</td>";
echo "<td>" . $row['spuname'] . "</td>";
echo "<td>" . $row['suid'] . "</td>";
echo "<td>" . $row['sdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I was wondering if it's possible on this part...
$result = mysqli_query($con,"SELECT * FROM sponsor ;");
to add Where spuname = 'spuname'
I guess the way I'm doing it is wrong.
What I wanted to do is to show filtered tables, so the one that has the session will see only his logs.
mysqli_query("SELECT * FROM sponsor Where spuname = 'spuname'",$con);
try putting $con after your query
You can use where clause in your query. it would be
$result = mysqli_query($con,"SELECT * FROM sponsor Where spuname = 'spuname'");
I'm having a hard time figuring out how to get my table to display as a web page.
Everything else is working fine. I've been able to use a form to write records TO the table, but I'm simply unable to display the table.
Here's my code:
$host = "***";
$userName = "***";
$passWord = "***";
$db = "doctorWho";
mysql_connect($host,$userName,$passWord);
mysql_select_db($db) or die( "Unable to access database");
$query = "SELECT * FROM patients";
$result = mysql_query($query);
echo "<table border='1'>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Address</th>
<th>Age</th>
<th>Sex</th>
<th>Marital Status</th>
<th>Medication</th>
<th>Date Rx'd</th>
<th>Quantity</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['lastName'] . "</td>";
echo "<td>" . $row['firstName'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['sex'] . "</td>";
echo "<td>" . $row['maritalStatus'] . "</td>";
echo "<td>" . $row['medication'] . "</td>";
echo "<td>" . $row['medsWhen'] . "</td>";
echo "<td>" . $row['medsQuant'] . "</td>";
echo "</tr>";
}
echo "</table>";
You want mysql_fetch_array(). Currently, you are mixing two different extensions. Each takes something different, relevant to the extension.
That aside, you should consider either PDO or MySQLi, which is where the wrong function you attempted to use is from. This article should help you decide which you could possibly use.
From php ref.
mixed mysqli_fetch_array ( mysqli_result $result [, int $resulttype = MYSQLI_BOTH ] )
mysqli_fetch_array need to mysqli_result, but you are using mysql_query, you should try mysql_fetch_array
// I think your connection should look like this
$conn = mysql_connect($host,$userName,$passWord);
mysql_select_db($conn,$db) or die( "Unable to access database");
// another is your using mysql as connection so you can use mysqli functions on that rofl.
//Instead use mysql functions
mysql_fetch_array();
note: Another one is instead of using # sign to handle error. Its much more best practice to use.. ini_set() or configuring php.ini in handling displaying of errors.
Just change:-
while($row = mysqli_fetch_array($result))
to
while($row = mysql_fetch_array($result))
I have a PHP document opening a connection to a database. Everything works fine - but as soon as I add one additional mysql_query request to the document, the mySQL server responds with a 500 internal server error.
Any idea why? Is there a reason that I cannot have multiple mysql_query requests in the one document?
thanks very much!
The offending PHP code is here (most of the echos are there for debugging reasons)
<?php
$q=$_GET["q"];
$con = mysql_connect('address.com', 'database', 'password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sql01_5789willgil", $con);
$sql1="SELECT * FROM place WHERE title = '".$q."'";
$result1 = mysql_query($sql1);
$row = mysql_fetch_array($result1);
$id=$row+1;
$sql2="SELECT * FROM place WHERE title = '".$q."'";
$result2 = mysql_query($sql2);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['latitude'] . "</td>";
echo "<td>" . $row['longitude'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "ID equals " . $id;
echo "row equals " . $row;
mysql_close($con);
?>
$sql1="SELECT * FROM place WHERE title = '".
mysql_real_escape_string($q, $con)."'"; <-- please escape
$result1 = mysql_query($sql1, $con); <-- always specify link identifier
if ($result1)
{
$row = mysql_fetch_array($result1); <-- always check result exist before fetch
}
From this here:
$sql1="SELECT * FROM place WHERE title = '".$q."'";
$result1 = mysql_query($sql1);
$row = mysql_fetch_array($result1);
$id=$row+1;
You're adding '1' to an array. I tried this script my box:
<?php
$row = array("my", "name", "is", "foo");
$id = $row + 1;
?>
and received this error:
Fatal error: Unsupported operand types
Perhaps this is the issue the script is complaining about.
From this answer here: Apache Fall Back When PHP Fails it very well be why it's returning a 500