$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)) {
Related
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 3 years ago.
I cannot find out what's wrong in this code. Since the query executes and insert also works but the error msg shows:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\kurinchi\kkmalar.php on line 41
$q = intval($_GET['q']);
$availability = isset($_GET['availability']);
$con = mysqli_connect('localhost','root','','kurinchi');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="UPDATE rooms SET availability=(availability-1) WHERE ID = (SELECT ID from (SELECT * from rooms WHERE ID = '".$q."')AS innerResult)" ;
$result = mysqli_query($con,$sql);
if($result === FALSE) {
die(mysql_error());
}
echo "<table>
<tr>
<th>ROOM TYPE</th>
<th>AC TYPE</th>
<th>PRICE</th>
<th>AVAILABILITY</th>
<th>BOOK </th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['room_type'] . "</td>";
echo "<td>" . $row['ac_type'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['availability'] . "</td>";
echo "<td>" . $row['booking_status'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
Try this after mysqli_query()
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
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);
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